6 Replies - 2992 Views - Last Post: 21 November 2011 - 07:40 PM Rate Topic: -----

#1 mike28  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 03-October 11

how to drag a shape

Posted 20 November 2011 - 01:43 PM

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)` maybe i need to update x and y 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";
           int mouseX = e.getX();
            int mouseY = e.getY();
        	if( mouseX >= x && mouseX <= x+w && mouseY >= y && mouseY <= y+l )
			{
				x++;
				y++;
			}
        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) {}
}


Is This A Good Question/Topic? 0
  • +

Replies To: how to drag a shape

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: how to drag a shape

Posted 20 November 2011 - 01:48 PM

Here's a drag and drop tutorial that Mac wrote that may help.
Was This Post Helpful? 1
  • +
  • -

#3 mike28  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 03-October 11

Re: how to drag a shape

Posted 20 November 2011 - 02:43 PM

works

This post has been edited by mike28: 20 November 2011 - 02:49 PM

Was This Post Helpful? 0
  • +
  • -

#4 mike28  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 03-October 11

Re: how to drag a shape

Posted 20 November 2011 - 03:35 PM

my code works but when i click on the rectangle on the second time it jumps to a different location


whats wrong with the 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 int mouseX, mouseY;
    boolean pressOut = false;


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

		g.setColor(color);
		g.drawRect(x, y, w, l);
		if(pressOut=false)
		g.drawRect(x, y, w, l);

	}
    public void mouseClicked(MouseEvent e)
    {

	}

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

    if (pressOut=true)
      updateLocation(e);


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


    }
  public void updateLocation(MouseEvent e) {
    x=e.getX()-mouseX;
	y=e.getY()-mouseY;
    this.repaint();
  }

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

    public void mousePressed(MouseEvent e)
    {
              mouseX = e.getX();
              mouseY = e.getY();

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

			updateLocation(e);

		else
		{
			  pressOut = true;
		}
	}


   public void mouseReleased(MouseEvent e)
   {
	  if( mouseX >= x && mouseX <= x+w && mouseY >= y && mouseY <= y+l )
      updateLocation(e);
      else {
      pressOut = false;
      }
	   }
}

Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: how to drag a shape

Posted 20 November 2011 - 09:23 PM

This statement does not make sense

		g.drawRect(x, y, w, l);
		if(pressOut=false)      // ?????????
		g.drawRect(x, y, w, l);


first
g.drawRect(x, y, w, l);
has already been done why drawing it a second time ?
second

if(pressOut=false)
sets presseOut to false and then if(false) il always false
you sure you do not want
if(pressOut == false)
but even pressOut is false when you enter paint() useless to draw twice the same Rect
Was This Post Helpful? 0
  • +
  • -

#6 mike28  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 03-October 11

Re: how to drag a shape

Posted 21 November 2011 - 07:31 PM

thanks pbl

This post has been edited by mike28: 21 November 2011 - 07:32 PM

Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10775
  • View blog
  • Posts: 40,122
  • Joined: 27-December 08

Re: how to drag a shape

Posted 21 November 2011 - 07:40 PM

Also, you should really use the more modern Swing JApplet over the antiquated AWT Applet.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1