Meaningless Drivel is fun!
The moose likes Swing / AWT / SWT and the fly likes Movable Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Movable Objects" Watch "Movable Objects" New topic

Movable Objects

Steven Gibbs

Joined: Apr 24, 2006
Posts: 29
Hi there

I have a JFrame with a JPanel placed on it. The JFrame is 400x400 and the JPanel is 300x300.

I now want to put an object on the panel ie, a rectangle in horizontal position sized at a height of 50 and length of 100.

Basically the JPanel is to be imagined as a 6x6 grid. Its the beginning stages of the Rush Hour game - I want to first get one object moving and then once that is sorted i will then edit it and take it further by adding other cars (rectangles in this case)

I have no idea how to add a rectangle shape to the panel that is moveable by clicking on it and dragging. I know I need to use a MouseListener. I want the rectangle to be able to move in incriments of 50 as 300/6 is obviously 50.

I have made a drawing program before using dragging/rubber banding but this is obviously going to be a bit different.

Here is the code I have at the moment coded in Netbeans - Any help anyone could give me would be much appreciated as at the moment I am at a dead end.

[ November 08, 2007: Message edited by: Steven Gibbs ]
Brandon Broschinsky
Ranch Hand

Joined: Aug 21, 2004
Posts: 41
Well just quickly putting something together you could do something like this. This gives a simple example of moving objects around the screen in a grid like fashion. Just click and drag the squares around. Hope this helps.

I agree. Here's the link:
subject: Movable Objects
It's not a secret anymore!