I'm making a program that allows the user to draw multiple shapes on the screen and choose the desired color. At the current moment I have nine preset colors that the user can choose from. I plan on using a JColorChooser
to allow the user to select different colors. However, I cannot figure out a way to store extra colors that may be chosen so that when the drawn shapes are repainted they will remain the same color that they were drawn with.
I have come up with a way to redraw the shapes in the order that they were drawn and with the color that they were drawn with, as shown below:
private List<Shape> shapeList = new ArrayList<Shape>();
private List<Integer> opNumList = new ArrayList<Integer>();
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int index = 0;
Graphics2D g2 = (Graphics2D) g.create();
if (!opNumList.isEmpty()) {
for (Shape s : shapeList) {
switch (opNumList.get(index))
case 0: g2.setColor(Color.red); g2.draw(s); break;
case 1: g2.setColor(Color.red); g2.fill(s); break;
case 2: g2.setColor(Color.orange); g2.draw(s); break;
case 3: g2.setColor(Color.orange); g2.fill(s); break;
case 4: g2.setColor(Color.yellow); g2.draw(s); break;
case 5: g2.setColor(Color.yellow); g2.fill(s); break;
case 6: g2.setColor(Color.green); g2.draw(s); break;
case 7: g2.setColor(Color.green); g2.fill(s); break;
case 8: g2.setColor(Color.blue); g2.draw(s); break;
case 9: g2.setColor(Color.blue); g2.fill(s); break;
case 10: g2.setColor(purple); g2.draw(s); break;
case 11: g2.setColor(purple); g2.fill(s); break;
case 12: g2.setColor(brown); g2.draw(s); break;
case 13: g2.setColor(brown); g2.fill(s); break;
case 14: g2.setColor(Color.white); g2.draw(s); break;
case 15: g2.setColor(Color.white); g2.fill(s); break;
case 16: g2.setColor(Color.black); g2.draw(s); break;
case 17: g2.setColor(Color.black); g2.fill(s); break;
default: return;
}
index++;
}
}
}
@Override
public void mouseReleased(MouseEvent ev) {
if (color == Color.red) {
if (toolNum == LINE || (toolNum == RECTANGLE && !cbFillItem.isSelected()) || (toolNum == OVAL && !cbFillItem.isSelected()))
opNumList.add(0);
if ((toolNum == RECTANGLE || toolNum == OVAL) && cbFillItem.isSelected())
opNumList.add(1);
}
if (color == Color.orange) {
if (toolNum == LINE || (toolNum == RECTANGLE && !cbFillItem.isSelected()) || (toolNum == OVAL && !cbFillItem.isSelected()))
opNumList.add(2);
if ((toolNum == RECTANGLE || toolNum == OVAL) && cbFillItem.isSelected())
opNumList.add(3);
}
if (color == Color.yellow) {
if (toolNum == LINE || (toolNum == RECTANGLE && !cbFillItem.isSelected()) || (toolNum == OVAL && !cbFillItem.isSelected()))
opNumList.add(4);
if ((toolNum == RECTANGLE || toolNum == OVAL) && cbFillItem.isSelected())
opNumList.add(5);
}
if (color == Color.green) {
if (toolNum == LINE || (toolNum == RECTANGLE && !cbFillItem.isSelected()) || (toolNum == OVAL && !cbFillItem.isSelected()))
opNumList.add(6);
if ((toolNum == RECTANGLE || toolNum == OVAL) && cbFillItem.isSelected())
opNumList.add(7);
}
if (color == Color.blue) {
if (toolNum == LINE || (toolNum == RECTANGLE && !cbFillItem.isSelected()) || (toolNum == OVAL && !cbFillItem.isSelected()))
opNumList.add(8);
if ((toolNum == RECTANGLE || toolNum == OVAL) && cbFillItem.isSelected())
opNumList.add(9);
}
if (color == purple) {
if (toolNum == LINE || (toolNum == RECTANGLE && !cbFillItem.isSelected()) || (toolNum == OVAL && !cbFillItem.isSelected()))
opNumList.add(10);
if ((toolNum == RECTANGLE || toolNum == OVAL) && cbFillItem.isSelected())
opNumList.add(11);
}
if (color == brown) {
if (toolNum == LINE || (toolNum == RECTANGLE && !cbFillItem.isSelected()) || (toolNum == OVAL && !cbFillItem.isSelected()))
opNumList.add(12);
if ((toolNum == RECTANGLE || toolNum == OVAL) && cbFillItem.isSelected())
opNumList.add(13);
}
if (color == Color.white) {
if (toolNum == LINE || (toolNum == RECTANGLE && !cbFillItem.isSelected()) || (toolNum == OVAL && !cbFillItem.isSelected()))
opNumList.add(14);
if ((toolNum == RECTANGLE || toolNum == OVAL) && cbFillItem.isSelected())
opNumList.add(15);
}
if (color == Color.black) {
if (toolNum == LINE || (toolNum == RECTANGLE && !cbFillItem.isSelected()) || (toolNum == OVAL && !cbFillItem.isSelected()))
opNumList.add(16);
if ((toolNum == RECTANGLE || toolNum == OVAL) && cbFillItem.isSelected())
opNumList.add(17);
}
repaint();
}
So what happens is that when the user draws a shape, it is stored in an ArrayList
of Shape
objects. An integer is stored in a separate list based on what kind of shape was drawn and its color. Then when the program redraws all the shapes, it will go through each shape in the list, and it will give the shapes the color that they were drawn with and fill them in if necessary.
Now, what I'm trying to figure out is a way to transfer these colors to their own List
or maybe even a Map
to avoid having to create a ton of Color
objects and adding to the already long list of code so that if the user decides to use other colors, the program will know what color belongs to what shape. So for example, if the user decided to draw a filled-in rectangle with the color 128,128,128; when the program redraws the shape it will know that the rectangle was filled in and was colored that specific shade of gray. But I haven't gone that far quite yet; right now I'm trying to find a solution that will work for just the nine colors that I have at the moment.
Does anyone have any ideas of how this can be done?