更改形状列表的颜色 java

Changing colors of a list of shapes java

我被这个问题困住了:

当我在一个形状(有一个矩形和圆形列表)内单击时,它会改变颜色。但是当我点击外面的时候,它不会变回来。

public void mouseClicked(MouseEvent me) {
    Color colorAux;
    for (int i = 0; i < images.size(); i++) {
        colorAux = images.get(i).getColor();
        if (images.get(i).getShape() == "Rectangle") {
            if ((images.get(i).getLocation().getX() < me.getX() && images.get(i).getLocation().getY() < me.getY() && images.get(i).getX() + images.get(i).getWidth() > me.getX() && images.get(i).getLocation().getY() + images.get(i).getHeight() > me.getY())) {
                images.get(i).setColor(Color.BLUE);
                repaint();
                JOptionPane.showMessageDialog(null, colorAux); //Debug
            } else if (!(images.get(i).getLocation().getX() < me.getX() && images.get(i).getLocation().getY() < me.getY() && images.get(i).getX() + images.get(i).getWidth() > me.getX() && images.get(i).getLocation().getY() + images.get(i).getHeight() > me.getY()) && (images.get(i).getColor() == Color.BLUE)) {
                images.get(i).setColor(colorAux);           
                repaint();
            }
        }
    }

我应该使用一组颜色吗?不知道我该如何解决这个问题。为了阐明我要存档的内容,这里有一个例子:

If the list contains a purple rectangle, I want it to change to blue when clicking inside it (which works). Then, when I click outside the rectangle, I want it to change back to purple (which does not work).

我已经尝试过 Leon 的建议,但没有奏效。我哪里做错了?

Being more specific, when i draw only 1 shape it works! But for example, i draw a blue rectangle, a purple circle and a red rectangle, and click inside some of the shapes, like the red rectangle, every shape changes its color to BLUE. And when i click outside again, it changes every shape's color to the default color (black).

public void mouseClicked(MouseEvent me) {
    List<Color> colors = new ArrayList<Color>();
    for (int j = 0; j < images.size(); j++) {
        colors.add(images.get(j).getColor());
    }
    for (int i = 0; i < images.size(); i++) {
        if ((images.get(i).getLocation().getX() < me.getX() && images.get(i).getLocation().getY() < me.getY() && images.get(i).getX() + images.get(i).getWidth() > me.getX() && images.get(i).getLocation().getY() + images.get(i).getHeight() > me.getY())) {
            images.get(i).setColor(Color.BLUE);
            repaint();
        } else {
            images.get(i).setColor(colors.get(i));
            repaint();
        }
    }
}

这是我认为正在发生的事情:

  • 用户在形状内单击鼠标 - 形状的颜色设置为蓝色。
  • 用户在形状外点击鼠标,我们设置颜色为colorAux,由图片颜色设置,蓝色!

您需要将原始颜色保存在某处。

你在正确的想法中使用颜色列表的想法(除非所有对象在开始时都具有相同的颜色)。在此列表中,您存储所有对象的初始颜色以及您可以执行的操作

// initialColors is the list holding the initial colors
for (int i=0; i<images.size(); i++) {
    if (images.get(i).getShape() == "Rectangle") {
        if (/*code to check if we are inside the rectangle which you already have*/) {
            images.get(i).setColor(Color.BLUE);
            repaint();
        } else {
            images.get(i).setColor(initialColors.get(i));
            repaint();
        }
    } /* maybe add a case for getShape() == "Circle" */
}

您可以在 images 列表的同时创建和填充 initialColors 列表(因为在那一刻,您知道每个形状的颜色)。

关于为什么您的方法不起作用:假设我们在一个矩形内部单击,它的颜色变为蓝色。当我们现在使用 colorAux = images.get(i).getColor() 来获取颜色时,我们得到蓝色,因为我们改变了矩形的颜色。当我们到达 images.get(i).setColor(colorAux) 时,我们将蓝色矩形的颜色设置为蓝色,这意味着没有任何反应。

此外,您不需要 else if,可以使用 else,因为第一个 if 检查点击是否发生在矩形内部。这意味着当单击 不在 矩形内时我们执行 else 分支,我们可以简单地重置那里的颜色。

编辑:现在您添加到问题中的更改不起作用,因为我们仍然有同样的问题:我们在 mouseClicked 事件中获得颜色,而不是在形状最初着色时获得颜色。这意味着我们用当前颜色(可能已更改为蓝色)而不是初始颜色填充颜色列表。您应该将添加的循环移动到最初为形状着色的位置(可能是填充 images 列表的位置)。