I am implementing a paint app in java. I am having trouble with choosing the colors of the shapes that are drawn. I am able to choose the color and draw with it but if i draw a change the color to blue, draw a blue rectangle, change the color to red, draw rectangle in red, it changes the rectangle in blue to red as well, happens with every shape. I think its because Im saving the shapes in an arrayList which changes the color of all the shapes, but, I have different arrayLists for different shapes.
I am trying to save the color in static color1 and using setPaint(color1) to change the color to the chosen one. Any help would be appreciated.
Below I have part of my code:
public class PaintAppFrame extends JFrame implements MouseListener, MouseMotionListener, ActionListener {
static final long serialVersionUID = 2L;
static int flag = 0;
// -------------------------------------------------------------------------
private JButton changeColor;
private JButton rectButton;
private JButton rectfillButton;
private JButton lineButton;
static Color color1;
static Graphics2D gr;
public static ArrayList<Shape> rectStruct = new ArrayList<Shape>();
public static ArrayList<Shape> rectFillStruct = new ArrayList<Shape>();
public static ArrayList<Shape> lineStruct = new ArrayList<Shape>();
private static Point mouseStart;
private static Point mouseEnd;
changeColor = new JButton("CHANGE COLOR");
changeColor.setActionCommand("color");
changeColor.addActionListener(this);
// added the menu goes here
image = Toolkit.getDefaultToolkit().getImage(".");
paintPanel = new PaintPanel();
paintPanel.addMouseMotionListener(this);
paintPanel.addMouseListener(this);
// make this a toolbar and images
Icon lineIcon = new ImageIcon("icons/line.png");
Icon rgbIcon = new ImageIcon("icons/color.png");
Icon rectIcon = new ImageIcon("icons/rect.png");
Icon rectfillIcon = new ImageIcon("icons/fullRect.png");
changeColor = new JButton(rgbIcon);
changeColor.setActionCommand("color");
changeColor.addActionListener(this);
rectButton = new JButton(rectIcon);
rectButton.setActionCommand("rectangle");
rectButton.addActionListener(this);
rectfillButton = new JButton(rectfillIcon);
rectfillButton.setActionCommand("rectanglefill");
rectfillButton.addActionListener(this);
lineButton = new JButton(lineIcon);
lineButton.setActionCommand("line");
lineButton.addActionListener(this);
JPanel buttons = new JPanel(new GridLayout());
buttons.setBorder(BorderFactory.createRaisedSoftBevelBorder());
buttons.setLayout(new GridLayout(16, 2));
buttons.add(changeColor);
buttons.add(rectButton);
buttons.add(rectfillButton);
buttons.add(lineButton);
paintCanvas = new JPanel(new BorderLayout());
paintCanvas.add(paintPanel, "Center");
paintCanvas.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
paintCanvas.setBackground(new Color(100, 100, 142));
paintCanvas.setPreferredSize(new Dimension(650, 520));
paintCanvas.setMaximumSize(new Dimension(2000, 1600));
}
public void mouseDragged(MouseEvent me) {
if (flag == 1) {
mouseEnd = new Point(me.getX(), me.getY());
repaint();
System.out.println("rect dragged");
}
else if (flag == 3) {
mouseEnd = new Point(me.getX(), me.getY());
repaint();
}
} else if (flag == 7) {
mouseEnd = new Point(me.getX(), me.getY());
repaint();
}
}
public void mouseMoved(MouseEvent me) {
}
public void mouseClicked(MouseEvent me) {
}
public void mouseEntered(MouseEvent me) {
}
public void mouseExited(MouseEvent me) {
}
public void mousePressed(MouseEvent me) {
if (flag == 1) {
mouseStart = new Point(me.getX(), me.getY());
mouseEnd = mouseStart;
repaint();
}
else if (flag == 3) {
mouseStart = new Point(me.getX(), me.getY());
mouseEnd = mouseStart;
repaint();
}
else if (flag == 7) {
mouseStart = new Point(me.getX(), me.getY());
mouseEnd = mouseStart;
repaint();
}
}
public void mouseReleased(MouseEvent me) {
} else if (flag == 1) {
Shape r = createRect(mouseStart.x, mouseStart.y, me.getX(), me.getY());
rectStruct.add(r);
mouseStart = null;
mouseEnd = null;
repaint();
}
else if (flag == 3) {
Shape r = createFillRect(mouseStart.x, mouseStart.y, me.getX(), me.getY());
rectFillStruct.add(r);
mouseStart = null;
mouseEnd = null;
repaint();
}
else if (flag == 7) {
Shape r = createLine(mouseStart.x, mouseStart.y, me.getX(), me.getY());
lineStruct.add(r);
mouseStart = null;
mouseEnd = null;
repaint();
}
}
@SuppressWarnings("static-access")
public void actionPerformed(ActionEvent ae) {
String command = ae.getActionCommand();
Object source = ae.getSource();
// instantiate the filechooser
switch (command) {
case "color":
this.setPaintColor();
break;
case "rectangle":
flag = 1;
FLAG = 0;
this.paintRect(gr);
break;
case "rectanglefill":
flag = 3;
this.paintRectFill(gr);
break;
case "line":
flag = 7;
this.paintLine(gr);
break;
private void setPaintColor() {
Color color = JColorChooser.showDialog(null, "Choose Paint Color", Color.black);
color1 = color;
}
public static void paintRect(Graphics g) {
gr = (Graphics2D) g;
for (Shape s : rectStruct) {
gr.setPaint(color1);
gr.setStroke(new BasicStroke(2));
gr.draw(s);
// gr.setPaint(this.LINE_COLOR);
// gr.fill(s);
}
if (flag == 1) {
if (mouseStart != null && mouseEnd != null) {
//makes outline while dragging rectangle
gr.setPaint(Color.RED);
Shape r = createRect(mouseStart.x, mouseStart.y, mouseEnd.x, mouseEnd.y);
gr.draw(r);
}
}
}
public static void paintRectFill(Graphics g) {
gr = (Graphics2D) g;
for (Shape s : rectFillStruct) {
gr.setPaint(color1);
gr.setStroke(new BasicStroke(2));
gr.draw(s);
gr.fill(s);
}
if (flag == 3) {
if (mouseStart != null && mouseEnd != null) {
//makes outline while dragging rectangle
gr.setPaint(Color.RED);
Shape r = createFillRect(mouseStart.x, mouseStart.y, mouseEnd.x, mouseEnd.y);
gr.draw(r);
}
}
}
public static void paintLine(Graphics g) {
gr = (Graphics2D) g;
for (Shape s : lineStruct) {
gr.setPaint(color1);
gr.setStroke(new BasicStroke(2));
gr.draw(s);
gr.fill(s);
}
if (flag == 7) {
if (mouseStart != null && mouseEnd != null) {
//makes outline while dragging rectangle
gr.setPaint(Color.GREEN);
Shape r = createLine(mouseStart.x, mouseStart.y, mouseEnd.x, mouseEnd.y);
gr.draw(r);
}
}
}
public static Rectangle2D.Float createRect(int x1, int y1, int x2, int y2) {
return new Rectangle2D.Float(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2));
}
public static Rectangle2D.Float createFillRect(int x1, int y1, int x2, int y2) {
return new Rectangle2D.Float(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2));
}
public static Line2D.Float createLine(int x1, int y1, int x2, int y2) {
return new Line2D.Float(x1, y1, x2, y2);
}}