Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

I need one tip. Polymorphism, JPanel, and shapes(objects) involved

$
0
0
I'm going to keep it simple. I need to be able to outlined the perimeter of each shape when its respective JComboBox spot is selected. For instance, if I select "rectangle" from the drop down menu, I want to be able to have all the shapes on the canvas but with rectangle either outlined or filled with a different color. I got three shapes total. Here is the code. Currently, if I select a shape from the drop down menu, one shape shows at the time except for the option "All" where all the shapes show but I did that on purpose.

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class testing{
   public static void main(String[] args){
       JFrame frame  = new JFrame("Moving Shapes");
	   Container content = frame.getContentPane();
	   content.add(new fun());
	   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	   frame.pack();
	   frame.setVisible(true);
   }
}
class fun extends JPanel{
    shape[] shapeList;
	private int count =0;
	private JComboBox<String> shapeNames;
	public fun(){
	  setLayout(new BorderLayout());
	  setBackground(Color.white);
	  setPreferredSize(new Dimension (300, 200)); 
	  
	  JPanel combo = new JPanel();
	  
	  shapeNames = new JComboBox<String>(new String[] { "Rectangle", "Circle", "Round Rect", "All" });
	  shapeNames.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
        repaint();
        }
       });
	   combo.add(shapeNames);
	  add(BorderLayout.SOUTH, combo);
	  shapeList = new shape[3];
	  oval i = new oval();
	  rect u =new rect();
	  roundrect x = new roundrect();
	  addShape(u);
	  addShape(i);
	  addShape(x);
	}
	public void addShape(shape q){
	  if(count<3){
	    shapeList[(count++)] = q;
	  }
	}
	public void paintComponent(Graphics g){
	 String option = (String) shapeNames.getSelectedItem();
	 if(option.equals("Rectangle")){
		selection();
	    shapeList[0].paintComponent(g);
		
	 }
	 if(option.equals("Circle")){
	    selection();
		
	    shapeList[1].paintComponent(g);
	 }
	 if(option.equals("Round Rect")){
	    selection();
	    shapeList[2].paintComponent(g);
	  }
	 
	 if(option.equals("All")){
	     for(int i=0; i<count; i++){
	     shapeList[i].paintComponent(g);
	  }
    }
}
	public void selection(){
	  if (shapeList != null)
      for (int i = 0; i < count; i++)
        if ((shapeList[i] instanceof shape)) {
          shape m1 = (shape)shapeList[i];
           m1.select(false);
		}
	}
}


abstract class shape extends JPanel{	
   protected boolean selecting = false;
   public void paintComponent(Graphics g){}
   public void select(boolean selected){
    selecting= selected;
  }
}
class oval extends shape{
  protected boolean selecting = false;
  private int x=30, y=30, h=90, w=90;
   public void paintComponent(Graphics g){
    super.paintComponent(g);
	 if(selecting){
	 g.setColor(Color.black);
	 g.drawOval(x,y,h,w);
	 g.setColor(Color.magenta);
	 g.fillOval(x,y,h,w);
	 }else{
	 g.setColor(Color.red);
	 g.fillOval(x,y,h,w);
	 }
   }
}
class rect extends shape{
    protected boolean selecting = false;
   private int x=20, y=20, h=240, w=130;
   public void paintComponent(Graphics g){
    super.paintComponent(g);
	if(selecting){
	 g.setColor(Color.black);
	 g.drawRect(x,y,h,w);
     g.setColor(Color.magenta);
	 g.fillRect(x,y,h,w);
	 }else{
	  g.setColor(Color.blue);
	 g.fillRect(x,y,h,w);
	 }
   }
}
class roundrect extends shape{
    protected boolean selecting = false;
   private int x=160, y=30, h=80, w=90, rh=10, rw=10;
   public void paintComponent(Graphics g){
    super.paintComponent(g);
	if(selecting){
     g.setColor(Color.black);
	 g.drawRoundRect(x,y,h,w,rh,rw);
     g.setColor(Color.magenta);
	 g.fillRoundRect(x,y,h,w,rh,rw);
	 }else{
	 g.setColor(Color.cyan);
	 g.fillRoundRect(x,y,h,w,rh,rw);
	 }
   }
}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>