java - 改变java形状列表的颜色

标签 java geometry draw shapes

我被这个问题困扰了:

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

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).

我已经尝试了莱昂的建议,但没有成功。我哪里做错了?

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();
        }
    }
}

最佳答案

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

// 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 事件中获取颜色,而不是在形状最初着色时获取颜色。这意味着我们用当前颜色(可能已更改为蓝色)而不是初始颜色填充颜色列表。您应该将添加的循环移动到最初为形状着色的位置(可能是填充图像列表的位置)。

关于java - 改变java形状列表的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51987115/

相关文章:

javascript - 通过 JavaScript 计算旋转元素的边界框的 X、Y、高度和宽度

Java - PaintComponent 中的 MouseListener 操作事件

java - 在同一个窗口java中显示许多文本

java - Perlin 噪声不生成 1 到 -1 之间的数字

java - 使用 Windows 资源管理器上传文件

java - 如何减小 JavaFX 图表的大小使其适合 ListCell?

python - 如何在Pygame中慢慢画一个圆圈?

java - 使用对象哈希码实现可比性

algorithm - 如何定义哪些对角线属于凹多边形

python - 在 pygame 中执行 for 循环后,图像不会保留在屏幕上