简体   繁体   English

如何在Java中移动矩形?

[英]How to move a rectangle in Java?

I am trying to move a rectangle but I am not sure how to do it, I know it something to do with 'mouseClicked(MouseEvent e)` but don't know how to use it. 我试图移动一个矩形,但我不知道该怎么做,我知道它与'mouseClicked(MouseEvent e)`有关,但不知道如何使用它。 This is the code I have so far: 这是我到目前为止的代码:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class MovRect extends Applet implements MouseMotionListener, MouseListener {
Color color = Color.green;
int x=30,y=30,w=150,l=150;
String MouseMotion ="";

public void init()
{
    addMouseListener(this);
    addMouseMotionListener(this);
}
public void paint(Graphics g)
{
    super.paint(g);

    g.setColor(color);
    g.drawRect(x, y, w, l);

}
public void mouseClicked(MouseEvent e)
{
    String clickDesc;
    if (e.getClickCount() == 2)
        clickDesc = "double";
    else
        clickDesc = "single";

    System.out.println("Mouse was " + clickDesc + "-clicked at location (" +
        e.getX() + ", " + e.getY() + ")");

        int mouseX = e.getX();
        int mouseY = e.getY();

    if( mouseX >= x && mouseX <= x+w && mouseY >= y && mouseY <= y+l )
    {

    }
    else
    {

    }
        this.repaint();
}

public void mouseDragged(MouseEvent e)
{
    System.out.println("mouse is being dragged at location (" + e.getX() + ", " +      e.getY() + ")");
    MouseMotion ="mouseDragged";
    repaint();
}
public void mouseMoved(MouseEvent e)
{
    System.out.println("mouse is being moved at location (" + e.getX() + ", " + e.getY() + ")");
    MouseMotion ="mouseMoved";
    repaint();
}


public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
 }

New answer If you want to be able to click and drag the rectangle you just basically update the x and y of the rectangle and have a mouse listener change those values to the mouses current position on click. 新答案如果您希望能够单击并拖动矩形,您只需更新矩形的x和y,并让鼠标监听器将这些值更改为鼠标在单击时的当前位置。

Old Answer 老答案

Your question is a little confusing. 你的问题有点令人困惑。 You mention using mouseClicked(MouseEvent e) yet that hasing nothing to do with actually moving the rectangle that deals with a event where the mouse is clicked. 你提到使用mouseClicked(MouseEvent e)但这与实际移动处理单击鼠标的事件的矩形无关。

If you just want to move your rectangle you could have a variable and add to the x or y. 如果您只想移动矩形,可以使用变量并添加到x或y。 For Example: 例如:

 int x = 100; int y = 100; g.fillRect(x,y,100,100); 

Then in your public void run you could do: 然后在你的公共无效运行中你可以做到:

  try { Thread.sleep(100); }catch(Exception e) { } x = x + 2; y = y +2; repaint(); 

Or for if the mouse was clicked basically you'd be using the mouse event and when it's clicked you would just set that x and y to the mouse's position. 或者,如果单击鼠标基本上你正在使用鼠标事件,当它被点击时你只需将x和y设置为鼠标的位置。

If you want to be able to 'paint' rectangles this snipet works nicely. 如果你想能够'绘制'矩形,这个snipet可以很好地工作。

public int XVal = 0 , YVal = 0;

public void paint(Graphics g) {

    g.fillRect(XVal, YVal, 20, 20);

    addMouseMotionListener(
            new MouseMotionAdapter() {

                public void mouseDragged(MouseEvent e) {

                    XVal = e.getX();
                    YVal = e.getY();
                    repaint();
                }
            });
} 

In order to have it move sequentially, you need to get the relative position which is always currentPosition - LastPosition. 为了让它按顺序移动,您需要获得始终为currentPosition - LastPosition的相对位置。 You could store the current position using mouseMove. 您可以使用mouseMove存储当前位置。

public void mouseMoved(MouseEvent e)
{
  _relativePosition.x = e.getX() - _currentPosition.x;
  _relativePosition.y = e.getX() - _currentPosition.y;
  _currentPosition.x = e.getX();
  _currentPosition.y = e.getY();
}

你只需要在tick方法()中增加x或y变量;

You need to add the mouse listener to the object you want to listen. 您需要将鼠标侦听器添加到要侦听的对象。 Check out http://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html it will get you started on how to set up the mouse listener. 查看http://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html ,它将帮助您开始设置鼠标监听器。
Also where are you actually stuck? 你还在哪里卡住? Getting the listener to work? 让听众工作? Or getting the rectangle to move? 或者让矩形移动?

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

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