Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Movable object  RSS feed

 
Bob Mazz
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just wondered if anybody knows if it is possible to have movable objects (ie using the mouse) inside a JFrame. By movable object I mean just a plain rectangle or something. Any link to some code of something will do. Cheers
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can do this by using a MouseMotionListener and updating the object's location in the mouseMoved() or mouseDragged() methods. You can display your object in one of two ways... override paintComponent() and paint a rectangle based off the x, y coordinates updated in your MouseMotionListener, or just setLayout( null ) on a JPanel and add another JPanel inside it, with setOpaque( true ), location set using setBounds( x, y, width, height ), and color set using setBackground().
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!