Results 1 to 4 of 4
  1. #1
    mike28 is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default how to drag a shape

    I want to be able to click and drag the rectangle but I am not sure how to do it, I know it something to do with 'mouseDragged(MouseEvent e)` but don't know how to use it. This is the code I have so far:

    Java Code:
    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";
               int mouseX = e.getX();
                int mouseY = e.getY();
            	if( mouseX >= x && mouseX <= x+w && mouseY >= y && mouseY <= y+l )
    			{
    
    			}
            this.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) {}
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: how to drag a shape

    The mouse motion listener has to change the x,y location where you draw the shape.
    There are some equations you need to use to convert the change in the mouse's location to the change in the shape's location.
    There might be code on the forum that shows how to do that. Do a Search on the forum for mouseDragged or MouseMotionListener.

  3. #3
    mike28 is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default Re: how to drag a shape

    thank you ......

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. A Shape Class
    By GoodApollo in forum AWT / Swing
    Replies: 7
    Last Post: 06-20-2011, 02:13 PM
  2. Selecting Shape
    By lifes46 in forum Java 2D
    Replies: 3
    Last Post: 05-05-2011, 06:30 PM
  3. create own shape
    By kepep in forum Java 2D
    Replies: 2
    Last Post: 12-15-2010, 08:43 PM
  4. Replies: 1
    Last Post: 04-02-2010, 01:48 PM
  5. implementing shape
    By sidkdbl07 in forum Java 2D
    Replies: 1
    Last Post: 01-12-2008, 07:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •