java - 比较时出现问题,使点在单击时改变颜色

标签 java colors square

我在进行比较时遇到了麻烦。该项目应该根据用户输入绘制一个正方形,然后用户单击的任何地方都会绘制一个不同颜色的点。例如,如果他们在正方形内部单击,则应生成一个红色圆圈,如果在正方形边缘单击,则应生成一个绿色圆圈,如果在正方形外部单击,则应生成一个蓝色圆圈。目前我的程序绘制了红色和蓝色圆圈,但没有绿色。事实上,当它高于某个点时,它也会绘制红色圆圈。

public class Square extends GraphicsProgram {
    // Instance variables
    private int side; // the length of a side
    private int anchorX; // the X value at the upper left corner
    private int anchorY; // the Y value at the upper left corner

    public Square(int x, int y, int side) {
        anchorX = x;
        anchorY = y;
        this.side = side;
    }

    // mouseClicked method
    public void mouseClicked(MouseEvent e) {
        // Find the location where the mouse was clicked
        int x = e.getX();
        int y = e.getY();

        // boolean variables to indicate location
        boolean isInside = false;
        boolean isOutside = false;
        boolean isOnEdge = false;

        if (x > anchorX + 1 && anchorY + 1 < y && anchorY + side + 1 > y) {
            isInside = true;
        }

        if (x > anchorX + side + 1 && anchorY + side + 1 < y && x > anchorX + side - 1 & y > anchorY + side - 1) {
            isOutside = true;
        }

        /*** NOTE: There a hard, and an easy way to do this! ***/

        if (anchorX - 1 <= x && x <= anchorX - 3 && anchorY - 1 <= y && anchorY + side - 3 >= y) {
            isOnEdge = true;
        }

        if (isOnEdge == true) {
            System.out.println("(" + x + ", " + y + ") is on the square");
            GOval circle = new GOval(x - 2, y - 2, 4, 4);
            circle.setFillColor(Color.GREEN);
            circle.setFilled(true);
            add(circle);
        }

        else if (isInside == true) {
            System.out.println("(" + x + ", " + y + ") is inside the square");
            GOval circle = new GOval(x - 2, y - 2, 4, 4);
            circle.setFillColor(Color.RED);
            circle.setFilled(true);
            add(circle);
        }

        else if (isOutside == true) {
            System.out.println("(" + x + ", " + y + ") is outside the square");
            GOval circle = new GOval(x - 2, y - 2, 4, 4);
            circle.setFillColor(Color.BLUE);
            circle.setFilled(true);
            add(circle);
        }
    }
}

我们得到了关于如何将正方形的 (x,y) 位置设置为

的提示

“例如,正方形的左边缘有:

x 值范围为:anchorX-1 ≤ x ≤anchorX+1,并且 y 值范围:anchorY-1 ≤ y ≤anchorY+side+1。

这意味着,如果我们有一个 anchor X 50、 anchor Y 100 和边 60 的正方形,则像 (49-51, 99-161) 这样的坐标将被视为位于左侧边缘。

最佳答案

我认为问题在于构成正方形的边界有点困惑 - 可以理解,因为它很难可视化。

在里面:

if (x > anchorX+1 && anchorY+1 < y && anchorY+side+1 > y) {
    isInside = true;
}

现在这个:

  • 确保它位于左侧的右侧 x > anchorX+1
  • 确保它位于顶部下方 anchorY+1 < y
  • 确保它位于底部上方 anchorY+side+1 > y

问题是:

  1. anchorY+side+1 > y将包括边缘 - +1 将超过
  2. 没有任何东西可以确保它位于右侧的左侧

一个可行的解决方案是:

if (x > anchorX+1 && y > anchorY+1 && x < anchorX+side-1 && y < anchorY+side-1) {
    isInside = true;
}

在外面:

if (x > anchorX+side+1 && anchorY+side+1 < y && x > anchorX+side-1 & y > anchorY+side-1) {
    isOutside = true;
}

现在这个:

  • 确保它位于右侧的右侧 x > anchorX+side+1
  • 确保其位于底部下方 anchorY+side+1 < y
  • 确保它位于右侧的右侧 x > anchorX+side-1
  • 确保它位于底部上方 y > anchorY+side-1

问题是:

  1. x > anchorX+side-1y > anchorY+side-1将包括外部区域的边缘。
  2. 没有任何东西可以确保它位于左侧的左侧或顶部的上方。
  3. x > anchorX+side+1x > anchorX+side-1几乎做同样的事情。
  4. 您使用了&我不确定是有意还是无意的符号。区别是&&仅当前面的全部为 true 时才运行,而 &总是运行。 &&因此速度更快,通常是首选。
  5. 通过检查所有内容是否正确,它仅在方 block 右侧和下方时运行。

一个可行的解决方案是:

if (x < anchorX-1 || y < anchorY-1 || x > anchorX+side+1 || y > anchorY+side+1) {
    isOutside = true;
}

处于边缘:

这是比较棘手的一个

if (anchorX-1 <= x && x <= anchorX-3 && anchorY-1 <= y && anchorY+side-3 >= y) {
    isOnEdge = true;
} 

现在这个:

  • 确保它位于边缘左侧部分的右侧 anchorX-1 <= x
  • 确保它位于左边缘左侧 a 点的左侧 x <= anchorX-3
  • 确保它位于底部顶部下方 anchorY-1 <= y
  • 确保其位于底部上方某个点 anchorY+side-3 >= y

问题是:

  1. anchorX-1 <= xx <= anchorX-3是矛盾的,所以不存在 x 使得两者都为真,所以整个事情一定是假的。
  2. 当您可能想使用 +1 来获得边缘的另一边时,您经常使用 -3。
  3. 您只检查左边缘和下边缘 - 其他两个边缘将被忽略。
  4. 当您使用 && 时而不是|| (或者),只有当整个事情都是真的时,它才会是真的 - 它必须同时在两个边缘内 - 这意味着只有角才会被计算在内。

可以修复此问题以为此创建一个可行的解决方案,但手动定义所有条件将需要大量工作。这就是简单和困难注释中所指的内容。

作为提示,您可以使用您已经知道它是否在正方形中的事实来简化它。 我建议考虑一下。

<小时/>

如果您很难理解这一切,我建议您手动将其绘制出来并在其上写下所有值 - 这比尝试在脑海中完成要容易得多。你做得越多,事情就会变得越容易。

关于java - 比较时出现问题,使点在单击时改变颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55053705/

相关文章:

java - 使用 jar 文件中资源中的音频

java - Android 服务未启动

html - 使用 FOR XML PATH 在 SQL Server 中格式化颜色

android - 如何使用 okhttp 禁用 SSLv3 回退

java - Okhttp3 在 Android Studio 中看不到 WebSocket 接口(interface)

java - 如何创建像 Uber 这样的手机身份验证凭证?

java - 如何在另一个创建的类中运行两个线程?

eclipse - 以编程方式更改 Eclipse 中的背景颜色

pandas - 设置 pandas 索引单元格的背景颜色

python - 通过python以特定方式计算嵌套数组的平均值?