Results 1 to 4 of 4
- 12-24-2012, 04:44 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 13
- Rep Power
- 0
My Shapes won't change color correctly
Hey guys my assignment is to make something like this:

So far my project looks like this:

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
Shape - abstract class shapeJava 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; } }
Line - extends shapeJava 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; } }
Circle = extends ShapeJava 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); } }
Rectangle - extends ShapeJava 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); } }
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); } }
-
Re: My Shapes won't change color correctly
I see a major problem 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.Java Code:public abstract class Shape { // .... protected static Color color; // ***** here *****
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.
- 12-24-2012, 06:42 PM #3
Member
- Join Date
- Nov 2012
- Posts
- 13
- Rep Power
- 0
-
Re: My Shapes won't change color correctly
Congratulations, and it's looking good!
Similar Threads
-
PLEASE HELP! how do i change the color of a pattern?
By armedrabbit in forum New To JavaReplies: 9Last Post: 04-20-2011, 08:51 PM -
Change color of a region
By sky in forum AWT / SwingReplies: 5Last Post: 11-24-2009, 03:47 PM -
change Panel Color
By aadi_j in forum AWT / SwingReplies: 2Last Post: 11-16-2009, 10:12 AM -
why isnt my background color displaying correctly ?
By nicnicnic in forum Java 2DReplies: 8Last Post: 10-29-2009, 10:54 AM -
Color Change of data
By Java.child in forum AWT / SwingReplies: 20Last Post: 02-12-2009, 06:51 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks