简体   繁体   English

比较时出现问题,单击时会使点变色

[英]Trouble with comparisons to make a dot change color when clicked

I'm having trouble with making my comparisons. 我在进行比较时遇到麻烦。 The project is supposed to draw a square from user input, and then wherever the user clicks would draw a dot of varying colors there. 该项目应该从用户输入中绘制一个正方形,然后用户单击的任何位置都将在其中绘制一个不同颜色的点。 For instance, if they click inside the square it should make a red circle, if it's on the edge of the square it makes a green circle, and if outside it makes a blue circle. 例如,如果他们在正方形内单击,则应形成一个红色圆圈,如果在正方形边缘上,则应形成一个绿色圆圈,在外部,则应形成一个蓝色圆圈。 At the moment my program draws red and blue circles, but no greens. 目前,我的程序绘制了红色和蓝色的圆圈,但没有绿色。 In fact it draws red circles when it's above a certain point as well. 实际上,当它高于某个点时,它也会绘制红色圆圈。

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

We were given a hint on how to do the (x,y) locations of the square as 我们得到了关于如何做正方形的(x,y)位置的提示

"For example, the left edge of the square has: “例如,正方形的左边缘有:

x values in the range: anchorX-1 ≤ x ≤ anchorX+1, and y values in the range: anchorY-1 ≤ y ≤ anchorY+side+1. x值范围:anchorX-1≤x≤anchorX + 1,y值范围:anchorY-1≤y≤anchorY + side + 1。

Which would mean that if we had a square with anchorX 50, anchorY 100 and side 60, coordinates like (49-51, 99-161) would be considered on the edge of the left side. 这意味着如果我们有一个带有anchorX 50,anchorY 100和侧面60的正方形,则将在左侧边缘考虑像(49-51,99-161)之类的坐标。

I think the problem is a little confusion with the bounds that make up the square - understandable as it's difficult to visualize. 我认为问题是与构成正方形的边界有些混乱-可以理解,因为很难可视化。

Is Inside: 在里面:

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

Right now this: 现在这个:

  • Makes sure it's to the right of the left side x > anchorX+1 确保它在左侧x > anchorX+1的右侧
  • Makes sure it's to the below the top anchorY+1 < y 确保它在顶部anchorY+1 < y的下方anchorY+1 < y
  • Makes sure it's above the bottom anchorY+side+1 > y 确保它在底部anchorY+side+1 > y上方anchorY+side+1 > y

The problems with that are: 问题在于:

  1. anchorY+side+1 > y will include the edge - the +1 will go over anchorY+side+1 > y将包含边线-+1将结束
  2. There is nothing making sure it's to the left of the right side 没有什么可以确保它在右侧的左侧

A working solution would be: 一个可行的解决方案是:

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

Is Outside: 在外面:

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

Right now this: 现在这个:

  • Makes sure it's to the right of the right side x > anchorX+side+1 确保它在右侧x > anchorX+side+1的右侧
  • Makes sure it's below the bottom anchorY+side+1 < y 确保它在底部anchorY+side+1 < y下方anchorY+side+1 < y
  • Makes sure it's to the right of the right side x > anchorX+side-1 确保它在右侧x > anchorX+side-1的右侧
  • Makes sure it's above the bottom y > anchorY+side-1 确保它在底部y > anchorY+side-1之上

The problems with that are: 问题在于:

  1. x > anchorX+side-1 and y > anchorY+side-1 will include the edge in the area considered outside. x > anchorX+side-1y > anchorY+side-1将在考虑为外部的区域中包含边。
  2. There is nothing making sure it's to the left of the left side or above the top side. 没有什么可以确保它在左侧的左侧或顶部的上方。
  3. x > anchorX+side+1 and x > anchorX+side-1 do practically the same. x > anchorX+side+1x > anchorX+side-1几乎相同。
  4. You used the & symbol I'm not sure was intentional or not. 您使用了&符号,但不确定是否故意。 The difference is && only runs if the previous are all true, while & always runs. 区别是&&仅在前面的条件都为true时运行,而&始终运行。 && is therefore faster and usually preferred. 因此&&更快,通常是首选。
  5. By checking all are true, it only runs when both to the right of and below the square. 通过检查所有内容是否正确,它仅在正方形的右侧和下方均运行。

A working solution would be: 一个可行的解决方案是:

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

Is On Edge: 处于边缘:

This is the more tricky one 这是比较棘手的一个

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

Right now this: 现在这个:

  • Makes sure it's to the right of the left part of the edge anchorX-1 <= x 确保它在边缘anchorX-1 <= x左侧的右侧anchorX-1 <= x
  • Makes sure it's to the left of the a point to the left of the left edge x <= anchorX-3 确保它在左边缘的点a左边缘的左边x <= anchorX-3
  • Makes sure it's below the top part of the bottom anchorY-1 <= y 确保它在底部anchorY-1 <= y的顶部下方anchorY-1 <= y
  • Makes sure it's above a point above the bottom anchorY+side-3 >= y 确保它在底部anchorY+side-3 >= y上方的一个点上方anchorY+side-3 >= y

The problems with that are: 问题在于:

  1. anchorX-1 <= x and x <= anchorX-3 are contridictary, so there is no x such that both are true, so the whole thing must be false. anchorX-1 <= xx <= anchorX-3是矛盾的,因此不存在x都为真的x,因此整个事物必须为假。
  2. You used -3 a lot, when you probably meant to use +1 to get the other side of the edge. 当您可能打算使用+1来获得边缘的另一侧时,您经常使用-3。
  3. You only check the left and bottom edges - the other two are ignored. 您只需检查左边缘和下边缘-其他两个边缘将被忽略。
  4. As you're using && instead of || 当您使用&&而不是|| (OR), you it will only be true when the whole thing is true - it must be within both edges at the same time - meaning only the corner would be counted. (OR),只有当整个事物都是真实的时,它才会为真-它必须同时在两个边缘内-这意味着只计算角点。

This could be fixed to create a working solution for this one, but defining all the conditions manually would take a lot of work. 可以修复此问题以为此创建一个可行的解决方案,但是手动定义所有条件将需要大量工作。 This is what it refers to in the easy and hard comment. 这就是在简单而艰苦的评论中所指的含义。

As a hint, you can use the fact you already know if it's in the square to simplify it. 提示一下,您可以使用已经知道它是否在方格中的事实来简化它。 I suggest thinking on it. 我建议考虑一下。


If you're struggling to comprehend all this, I suggest drawing it out manually and writing down all the values onto it - it's much easier than trying to do it in your head. 如果您难以理解所有这些,我建议手动将其绘制出来并将所有值写下来-比尝试在脑海中做起来容易得多。 It gets easier the more you do it. 您做得越多,就越容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM