比较时出现问题,导致点击时点变色

时间:2019-03-07 22:18:40

标签: 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。

这意味着如果我们有一个带有anchorX 50,anchor Y 100和side 60的正方形,则将在左侧边缘考虑像(49-51,99-161)之类的坐标。

1 个答案:

答案 0 :(得分:0)

我认为问题是与构成正方形的边界有些混乱-可以理解,因为很难可视化。

在里面:

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. 通过检查所有内容是否正确,它仅在正方形的右侧和下方都运行。

一个可行的解决方案是:

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左侧的右侧
  • 确保它在左边缘x <= anchorX-3的左侧a点的左侧
  • 确保其位于底部anchorY-1 <= y的顶部下方
  • 确保其位于底部anchorY+side-3 >= y上方的点上方

与之相关的问题是:

  1. anchorX-1 <= xx <= anchorX-3是矛盾的,因此不存在x都为真的x,因此整个事物必须为假。
  2. 当您可能想使用+1获取边缘的另一面时,您经常使用-3。
  3. 您只检查左边缘和下边缘-其他两个边缘将被忽略。
  4. 当您使用&&而不是||(OR)时,只有当整个情况为真时,它才会为真-它必须同时在两个边缘内-仅意味着角落就算在内了。

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

作为提示,您可以使用已知的事实来简化它。 我建议考虑一下。


如果您正在努力理解所有这些,我建议手动将其绘制出来并将所有值写下来-比尝试动手要容易得多。您做得越多,就越容易。