Results 1 to 4 of 4
Like Tree2Likes
  • 1 Post By Fubarable
  • 1 Post By juhess88

Thread: My Shapes won't change color correctly

  1. #1
    juhess88 is offline Member
    Join Date
    Nov 2012
    Posts
    13
    Rep Power
    0

    Default My Shapes won't change color correctly

    Hey guys my assignment is to make something like this:

    My Shapes won't change color correctly-assignment.jpg

    So far my project looks like this:

    My Shapes won't change color correctly-mywork.jpg

    Unfortunately, my shapes don't change color correctly. If I want to add a new shape and color it red, all my previous shapes change color also! Can someone please tell me why.

    I feel bad asking for help because my project is very big but I don't know what else to do. I've been stuck on this for three days already. This assignment is do in 2 days and I can't get past this little section.

    But if someone is gracious enough to help here's all my code - thanks in advance:

    Class Frame - The JFrame

    Java Code:
    package model;
    
    import java.awt.BorderLayout;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    
    import java.awt.Dimension;
    
    import java.awt.Color;
    
    import java.awt.event.ActionEvent;
    
    import java.awt.event.ActionListener;
    
    import javax.swing.Box;
    import javax.swing.ButtonGroup;
    import javax.swing.JColorChooser;
    import javax.swing.JLabel;
    
    import javax.swing.BoxLayout;
    
    import javax.swing.JButton;
    
    import javax.swing.JFrame;
    
    import javax.swing.JPanel;
    
    import javax.swing.JRadioButton;
    
    public class Frame extends JFrame implements ActionListener{
    
    	Panel panel = new Panel();
    	
    	JPanel panel2 = new JPanel(new GridBagLayout());
    	
    	GridBagConstraints grid = new GridBagConstraints();
    	
    	JRadioButton c = new JRadioButton("Circle");
    
    	JRadioButton l = new JRadioButton("Line");
    
    	JRadioButton r = new JRadioButton("Rectangle");
    
    	JRadioButton fill = new JRadioButton("Fill");
    
    	JRadioButton border = new JRadioButton("Border");
    	
    	JButton color = new JButton("Color");
    	
    	JButton clear = new JButton("Clear");
    
    	JButton exit = new JButton("Exit");
    	
    	JLabel blank = new JLabel("");
    	
    	JLabel blank2 = new JLabel("");
    	
    	ButtonGroup once = new ButtonGroup();
    	
    	ButtonGroup alsoOnce = new ButtonGroup();
    
    
    	
    	public Frame() {
    		
    		super("Frame");
    		
    		setVisible(true);
    		
    		setSize(500, 500);
    				
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		add(panel);
    		
    		add(panel2, BorderLayout.EAST);
    		
    		grid.insets = new Insets(5, 0, 5, 0);
    		
    		grid.gridx = 0;
    		
    		grid.gridy = 0;
    		
    		panel2.add(l, grid);
    		
    		grid.gridx = 0;
    		
    		grid.gridy = 1;
    		
    		panel2.add(c, grid);
    		
    		grid.gridx = 0;
    		
    		grid.gridy = 2;
    		
    		panel2.add(r, grid);
    
    		grid.insets = new Insets(20, 0, 20, 0);
    
    		grid.gridx = 0;
    		
    		grid.gridy = 3;
    		
    		panel2.add(blank, grid);
    		
    		grid.insets = new Insets(5, 0, 5, 0);
    
    		grid.gridx = 0;
    		
    		grid.gridy = 5;
    		
    		panel2.add(fill, grid);
    		
    		grid.gridx = 0;
    		
    		grid.gridy = 7;
    		
    		panel2.add(border, grid);
    		
    		grid.insets = new Insets(20, 20, 20, 20);
    				
    		grid.gridx = 0;
    		
    		grid.gridy = 9;
    		
    		panel2.add(color, grid);
    				
    		grid.gridx = 0;
    		
    		grid.gridy = 11;
    		
    		grid.insets = new Insets(20, 0, 20, 0);
    
    		grid.gridx = 0;
    		
    		grid.gridy = 11;
    		
    		panel2.add(blank2, grid);
    		
    		grid.insets = new Insets(5, 0, 5, 0);
    		
    		grid.gridx = 0;
    		
    		grid.gridy = 16;
    		
    		panel2.add(clear, grid);
    		
    		grid.gridx = 0;
    		
    		grid.gridy = 18;
    		
    		panel2.add(exit, grid);
    
    		once.add(l);
    
    		once.add(r);
    
    		once.add(c);
    
    		alsoOnce.add(fill);
    
    		alsoOnce.add(border);
    		
    		clear.addActionListener(this);
    		
    		exit.addActionListener(this);
    		
    		color.addActionListener(this);
    
    		r.addActionListener(this);
    		
    		l.addActionListener(this);
    		
    		c.addActionListener(this);
    		
    		fill.addActionListener(this);
    		
    		border.addActionListener(this);
    	}
    	
    	public void actionPerformed(ActionEvent e) {
    		
    		if(e.getSource() == l) {
    			
    			panel.setTheShape(new Line(panel.getColor()));
    
    			panel.setShapeNum(1);
    
    		}
    		
    		
    		if(e.getSource() == c) {
    			
    			
    			panel.setTheShape(new Circle(panel.getColor(), Shape.fill));
    					
    			panel.setShapeNum(2);
    			
    		}
    		
    		if(e.getSource() == r){
    			
    			panel.setTheShape(new Rectangle(panel.getColor(), Shape.fill));
    			
    			panel.setShapeNum(3);
    			
    		}
    		
    		if(e.getSource() == color) {
    			
    			Color c = JColorChooser.showDialog(this, "", getBackground());
    			
    			if(panel.getShapeNum() == 1) {
    				
    				
    				panel.setTheShape(new Line(c));
    
    			}
    			
    			if (panel.getShapeNum() == 2) {
    				
    				
    				panel.setTheShape(new Circle(c, Shape.fill));							
    			}
    			
    			if(panel.getShapeNum() == 3) {
    								
    				
    				panel.setTheShape(new Rectangle(c, Shape.fill));								
    			}
    			
    			if (e.getSource() == fill) {
    				
    				if (panel.getShapeNum() == 1) {
    															
    					panel.setTheShape(new Line(Shape.color));
    
    				}
    				
    				if (panel.getShapeNum() == 2) {
    					
    					
    					panel.setTheShape(new Circle(Shape.color, true));
    								
    				}
    				
    				
    				if(panel.getShapeNum() == 3) {
    					
    									
    					panel.setTheShape(new Rectangle(Shape.color, true));
    									
    				}
    			
    			}
    			
    if (e.getSource() == border) {
    				
    				if (panel.getShapeNum() == 1) {
    															
    					panel.setTheShape(new Line(Shape.color));
    
    				}
    				
    				if (panel.getShapeNum() == 2) {
    					
    					
    					panel.setTheShape(new Circle(Shape.color, false));
    								
    				}
    				
    				
    				if(panel.getShapeNum() == 3) {
    					
    									
    					panel.setTheShape(new Rectangle(Shape.color, false));
    									
    				}
    			
    			}
    			
    		}
    			
    		
    		
    		if(e.getSource() == clear)
    		{
    			panel.getShapes().clear();
    			
    			repaint();
    		}
    		
    		else if(e.getSource() == exit)
    			
    			this.dispose();
    	}
    
    	
    	
    	public static void main(String[] args) 
    	{
    		Frame f =new Frame ();
    	}
    	
    
    }

    Panel - The JPanel

    Java Code:
    package model;
    
    import java.awt.Color;
    
    import java.awt.Graphics;
    
    import java.awt.event.MouseAdapter;
    
    import java.awt.event.MouseEvent;
    
    import java.awt.event.MouseMotionAdapter;
    
    import java.util.ArrayList;
    
    import javax.swing.JColorChooser;
    import javax.swing.JPanel;
    
    import javax.swing.JRadioButton;
    
    public class Panel extends JPanel{
    	
    	protected Shape theShape;
    	
    	protected int shapeNum;
    	
    	protected Color color;
    	
    	protected boolean fill;
    	
    
    
    	protected ArrayList<Shape> shapes = new ArrayList<Shape>();
    		
    	
    	public Panel() {
    		
    		shapeNum = 1;
    				
    		theShape = new Line(color);
    		
    		setBackground(Color.WHITE);
    		
    		
    
    		addMouseMotionListener(new MouseMotionAdapter() {
    			
    			public void mouseDragged(MouseEvent e) {
    				
    					theShape.setWidth(e.getX());
    					
    					theShape.setHeight(e.getY());
    					
    					
    					repaint();
    				
    				}
    			
    			}
    		
    		);
    		
    		
    		addMouseListener(new MouseAdapter() {
    			
    			public void mousePressed(MouseEvent e) {
    				
    				theShape.setX(e.getX());
    				
    				theShape.setY(e.getY());
    				
    				repaint();
    			
    			}
    		 
    		 
    			public void mouseReleased(MouseEvent e) {
    				
    				theShape.setWidth(e.getX());
    				
    				theShape.setHeight(e.getY());
    				
    				shapes.add(theShape);
    				
    				repaint();
    				
    				
    				if(shapeNum == 1) {
    					
    					theShape =  new Line(Shape.getColor());
    				
    				
    				}
    				else if(shapeNum == 2) {
    				
    					theShape =  new Circle(Shape.getColor(), fill);
    				
    				}
    				
    				else if(shapeNum == 3) {
    				
    					theShape = new Rectangle(Shape.getColor(), fill);
    								
    				}
    				
    			}
    			
    			
    			
    		}
    		);
    		
    		
    	}
    	
    	 public void paintComponent(Graphics g) { 
    		 
    	        super.paintComponent(g);
    	        
    	        for(Shape s : shapes) {
    	        	
    	 		   s.paint(g);
    	 		   
    	 	   	}
    	        
    	        theShape.paint(g);
    	        
    	 }
    	
    
    	public Shape getTheShape() {
    		
    		return theShape;
    		
    	}
    
    	public void setTheShape(Shape theShape) {
    		
    		this.theShape = theShape;
    		
    	}
    
    	public int getShapeNum() {
    		
    		return shapeNum;
    		
    	}
    
    	public void setShapeNum(int shapeNum) {
    		
    		this.shapeNum = shapeNum;
    		
    	}
    
    	public ArrayList<Shape> getShapes() {
    		
    		return shapes;
    		
    	}
    
    	public void setShapes(ArrayList<Shape> shapes) {
    		
    		this.shapes = shapes;
    		
    	}
    	
    	public Color getColor() {
    		return color;
    	}
    
    	public void setColor(Color color) {
    		this.color = color;
    	}
    	
    	
    
    }
    Shape - abstract class shape

    Java Code:
    package model;
    
    import java.awt.Color;
    
    import java.awt.Graphics;
    
    import javax.swing.JColorChooser;
    
    public abstract class Shape {
    
    	protected int x = 0;
    	
    	protected int y = 0;
    		
    	protected int width = 0;
    	
    	protected int height = 0;
    	
    	protected static boolean fill = false;
    	
    	protected static Color color;
    	
    	
    	abstract void paint(Graphics g);
    
    
    	public int getX() {
    		
    		return x;
    		
    	}
    
    
    	public void setX(int x) {
    		
    		this.x = x;
    		
    	}
    
    
    	public int getY() {
    		
    		return y;
    		
    	}
    
    
    	public void setY(int y) {
    		
    		this.y = y;
    		
    	}
    
    
    	public int getHeight() {
    		
    		return height;
    		
    	}
    
    
    	public void setHeight(int height) {
    		
    		this.height = height;
    		
    	}
    
    
    	public int getWidth() {
    		
    		return width;
    		
    	}
    
    
    	public void setWidth(int width) {
    		
    		this.width = width;
    		
    	}
    
    
    	public boolean isFill() {
    		
    		return fill;
    		
    	}
    
    
    	public void setFill(boolean fill) {
    		
    		this.fill = fill;
    		
    	}
    
    
    	public static Color getColor() {
    		
    		return color;
    		
    	}
    
    
    	public void setColor(Color color) {
    		
    		Shape.color = color;
    		
    	}
    		
    }
    Line - extends shape

    Java Code:
    package model;
    
    import java.awt.Color;
    import java.awt.Graphics;
    
    public class Line extends Shape {
    	
    	public Line(Color c) {
    		
    		color = c;
    	
    	}
    	
    	public void paint(Graphics g){
    		
    		g.setColor(color);
    
    		g.drawLine(x, y, width, height);
    		
    	}
    	
    }
    Circle = extends Shape

    Java Code:
    package model;
    
    import java.awt.Color;
    import java.awt.Graphics;
    
    public class Circle extends Shape{
    
    	public Circle(Color c, boolean filling) {
    		
    		color = c;
    		
    		fill = filling;
    	}
    	
    	void paint(Graphics g) {		
    		
    		g.setColor(color);
    		
    		if (fill == false)
    			
    			g.drawOval(x, y, width, height);
    		
    		else
    			
    			g.fillOval(x, y, width, height);
    			
    			
    		
    		
    	}
    
    }
    Rectangle - extends Shape

    Java Code:
    package model;
    
    import java.awt.Color;
    import java.awt.Graphics;
    
    public class Rectangle extends Shape{
    
    	public Rectangle(Color c, boolean filling) {
    		
    		color = c;
    		
    		fill = filling;
    	}
    	
    	void paint(Graphics g) {
    
    		g.setColor(color);
    		
    		
    		
    		if(fill == false)
    						
    			g.drawRect(x, y, width, height);
    		
    		else
    			
    			g.fillRect(x, y, width, height);
    		
    	}
    
    }
    Attached Thumbnails Attached Thumbnails My Shapes won't change color correctly-mywork.jpg  

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: My Shapes won't change color correctly

    I see a major problem here:

    Java Code:
    public abstract class Shape {
     
        // ....
         
        protected static Color color; // ***** here *****
    When a variable is static, then that variable is in effect shared by all the objects of the class, and none of the objects get their own unique copy of that variable. So if that variable is set to red, then for all the objects of the class, that variable is red.

    The solution for this problem is simple: make that a non-static variable. If you get another bug from this, then you need to solve that bug, but the solution is not to make the variable static again. I am not sure if this will solve your entire problem or not, but it's at least a start.
    juhess88 likes this.

  3. #3
    juhess88 is offline Member
    Join Date
    Nov 2012
    Posts
    13
    Rep Power
    0

    Default Re: My Shapes won't change color correctly

    Wow! you saved my life. I can't believe you solved this so quickly. Thanks so much for all your help.

    I took out the static, made a few changes, and got:

    My Shapes won't change color correctly-itworks.jpg


    Now, off to finish the rest of this assignment :)
    Fubarable likes this.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. PLEASE HELP! how do i change the color of a pattern?
    By armedrabbit in forum New To Java
    Replies: 9
    Last Post: 04-20-2011, 09:51 PM
  2. Change color of a region
    By sky in forum AWT / Swing
    Replies: 5
    Last Post: 11-24-2009, 04:47 PM
  3. change Panel Color
    By aadi_j in forum AWT / Swing
    Replies: 2
    Last Post: 11-16-2009, 11:12 AM
  4. Replies: 8
    Last Post: 10-29-2009, 11:54 AM
  5. Color Change of data
    By Java.child in forum AWT / Swing
    Replies: 20
    Last Post: 02-12-2009, 07:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •