简体   繁体   English

如何用鼠标画一个正方形

[英]How to draw a square with the mouse

What I'm trying to do is basically the thing you can do in the desktop when you click and drag te mouse making a square. 我想做的基本上是您单击并拖动一个正方形鼠标时可以在桌面上执行的操作。 The problem is I don't know how to make it draw "backwards" or how to clean the previous parameters when you start a new square. 问题是我不知道如何在开始新的正方形时使其向后绘制或向后清理先前的参数。 here is the entire code: 这是完整的代码:

public void paint (Graphics j){
 super.paint(j);
   j.drawRect(x,y,z,w);

}

private void formMousePressed(java.awt.event.MouseEvent evt) {                                  
    x=evt.getX();
    y=evt.getY();

    repaint();       
} 

 private void formMouseDragged(java.awt.event.MouseEvent evt) {                                  
    z=evt.getX();
    w=evt.getY();

     repaint();
}

The signature for drawRect is: drawRect(int x, int y, int width, int height) . drawRect的签名是: drawRect(int x, int y, int width, int height) You need to calculate the top left corner of the square, and the width and height. 您需要计算正方形的左上角以及宽度和高度。

The top-left corner is (min(x, z), min(y, w)) . 左上角是(min(x, z), min(y, w))
The width is abs(xz) and the height is abs(yw) 宽度为abs(xz) ,高度为abs(yw)

Putting this together we get 放在一起我们得到

Try 尝试

j.drawRect(Math.min(x, z), Math.min(y, w), Math.abs(x - z), Math.abs(y - w));

Why does this work? 为什么这样做? Well you're given 2 points. 好吧,您会得到2分。 It's a well known fact that 2 points can determine a square(opposite corners). 一个众所周知的事实是2个点可以确定一个正方形(对角)。 The first problem is that you have to translate the points you're given, into an input that java likes. 第一个问题是您必须将给定的点转换为java喜欢的输入。 In this case, you first need the upper left hand corner. 在这种情况下,您首先需要左上角。 You don't know which point you have is that corner, or actually it could be that neither of them are. 您不知道哪个点是那个角,或者实际上可能都不是。

So what do we know about the upper left corner? 那么我们知道左上角? We know that it's x value is the smallest x value that exists in the square. 我们知道,x值是正方形中存在的最小x值。 We also know that at least one of the 2 points given rest on that same edge. 我们也知道给定的两个点中至少有一个位于同一条边上。 Using this information we can determine that the x coordinate of the top left corner is the smallest x value of our 2 points. 使用此信息,我们可以确定左上角的x坐标是2个点中的最小x值。 Or min(x, z) . min(x, z) We use the same procedure to find the y coordinate. 我们使用相同的过程来找到y坐标。

Now width and height are easy. 现在,宽度和高度都很容易。 The width is the right edge - the left edge. 宽度是右边缘-左边缘。 We don't know which point is the right side, and which is the left side, but it doesn't matter. 我们不知道哪个点在右边,哪个点在左边,但这无关紧要。 If we take the absolute value of the difference will always give you the positive difference between the points. 如果我们采用差的绝对值,将始终为您提供两点之间的正差。 In this case abs(xz) . 在这种情况下abs(xz) The process is the same for the height. 高度的过程相同。

As for resetting the square try adding a formMouseReleased method and setting x, y, z, w to 0. 至于重置正方形,请尝试添加formMouseReleased方法并将x,y,z,w设置为0。

I think you might create a method that resets the parameters something like: void modifyMouse() in your Mouse class //your parameters=0 我认为您可能会创建一个重置参数的方法,例如:Mouse类中的void ModifyMouse()//您的参数= 0

I might try to give you a better help if you clarify your question, for now try that. 如果您澄清问题,我可能会尝试为您提供更好的帮助,现在尝试一下。

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

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