This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Movable object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Movable object" Watch "Movable object" New topic
Author

Movable object

Bob Mazz
Greenhorn

Joined: Mar 17, 2004
Posts: 5
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

Joined: Oct 18, 2000
Posts: 4121

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().


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
 
Don't get me started about those stupid light bulbs.
 
subject: Movable object
 
Similar Threads
[new user!] I've got an undecorated window, how can I make it resizable?
Movable paint objects within JPanel
How to stop dragging the JFrame
portable chicken pen
<jsp:useBean> standard action type and class confusion