aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes dnd Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "dnd" Watch "dnd" New topic
Author

dnd

Howard Kung
Greenhorn

Joined: Jul 22, 2002
Posts: 24
Hi folks,
I like to move a rectangle (created by drawRect). Is it possible to move this rectangle around with a mouse press? Like drag n drop..

If the above is possible, can someone post a reference to it?
Thanks in advance
[ April 14, 2003: Message edited by: Howard Kung ]

Howard Kung<br />SCJP 1.2<br />SCWCD<br />IBM Certified Specialist: WebSphere 4.0
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

You don't need to use the Drag and Drop API unless you are going to be transferring the rectangle between components.

To get the rectangle to move within the same component that is drawing it, you would just add a MouseListener and a MouseMotionListener to that component. In the mousePressed() method, you would try to detect if the rectangle was clicked, if so, set a boolean. In the mouseReleased, if the boolean is set, unset it. In mouseDragged(), if the boolean is set, move the rectangle based on the location of the mouse, and repaint() the component. In the paint method you will paint the rectangle based off the class attributes that were affected in the mouseDragged method.

If this isn't clear enough, I can post some code if you want.


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Howard Kung
Greenhorn

Joined: Jul 22, 2002
Posts: 24
Thanks!
Your explanation was excellent. But I would still appreciate some sample code if possible.
Thanks in advance
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

You're welcome!

 
Don't get me started about those stupid light bulbs.
 
subject: dnd