Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

dnd

 
Howard Kung
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Howard Kung
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!
Your explanation was excellent. But I would still appreciate some sample code if possible.
Thanks in advance
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome!

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic