English 中文(简体)
如何在贾瓦迁一条 rec子?
原标题:How to move a rectangle in Java?

我正试图开一条 rec子,但我不敢肯定会怎样做,我知道这是与变幻觉(MouseEvent e)”有关的事情,但不知道如何使用。 这是我迄今为止制定的法典:

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.

http://www.un.org/Depts/DGACM/index_spanish.htm

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.

If you just want to move your rectangle you could have a variable and add to the x or y. For Example:

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

之后,你可以做以下工作:

      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.

问题回答

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.
Also where are you actually stuck? Getting the listener to work? Or getting the rectangle to move?

如果你想能够画面,那一pet就可 n。

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. You could store the current position using 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();
}

您仅需要用标准方法增加x或 y变量;





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签