How to move a rectangle in Java?

10,447

Solution 1

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.

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.

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

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.

Solution 2

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?

Solution 3

If you want to be able to 'paint' rectangles this snipet works nicely.

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

Solution 4

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

Solution 5

you just need to increase the x or y variable in a tick method();

Share:
10,447
Mike Lolo
Author by

Mike Lolo

Updated on June 04, 2022

Comments

  • Mike Lolo
    Mike Lolo almost 2 years

    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. 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) {}
     }
    
  • Mike Lolo
    Mike Lolo over 12 years
    i am stuck on getting the rectangle to move
  • dann.dev
    dann.dev over 12 years
    Do you want to be able to drag it? Or just click and have it move?
  • dann.dev
    dann.dev over 12 years
  • dann.dev
    dann.dev over 12 years
    He wants to be able to dragged the mouse around and have the square move with it, where did you get triangle from...
  • ComputerLocus
    ComputerLocus over 12 years
    Ooops for some reason typed triangle I meant rectangle. I will edit my message.
  • ComputerLocus
    ComputerLocus over 12 years
    No problem! Basically you just want to use the mouse listener and whenever it's clicked you want it to keep moving to the position of mouse. When clicked again have it stop doing that. Or I guess when you release click it would work as well.