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

Moving multiple ojects in Java2D

Jesse Miller
Ranch Hand

Joined: Apr 05, 2010
Posts: 37
I have an applet that I am trying to write that should probably be tackled in multiple parts. Currently I have an applet that creates one rectangle and lets the user click on it (and only on it) and move it around the viewing window. Well, I would like to add more rectangles and make them movable as well. I guess it doesn't really matter if they are allowed to overlap, I just need to make sure they can always be moved individually. The end program is going to be used in a flexible environment so I need to be able to create a dynamic number of rectangles at run-time from a reading a variable off of an XML file. My code is attached. Thanks

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
so what is the problem?

is there a specific requirement to use rectangles, perhaps it might be easier to use
JApplet and JLabels with borders
Jesse Miller
Ranch Hand

Joined: Apr 05, 2010
Posts: 37
Well, the problem is as I described. I need to add a dynamic number of rectangles to the panel and make them all movable. I started coding this program using JApplet however it was suggested that I use Java2D instead because of the increased flexibility of Java2D. What is your reason for suggesting JApplet instead? Do you know of an implementation of this program using JApplet? Thanks

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Well, the problem is as I described.

you still haven't described any problem, only what you want to do,
so what is the exact nature of your problem you are stuck on.

if it's that you can't do any of it, that's a job to pay someone.

JApplets are Swing, which handles flickering better.
you can then use a JLabel with a border, which should look like
your current rectangle, and is extremely easy to drag around
Jesse Miller
Ranch Hand

Joined: Apr 05, 2010
Posts: 37
Ok, Ive changed my implementation a little bit. I got rid of Java2D and am just using JApplet and Swing right now. Currently I have two problems with my code. I want to create a dynamic number of squares so I have an objects with several functions in it that I need to store x and y positions and such. I would like to create an array of these obects so I can have multiple squares. The code builds without any errors however when I run it I get error when I try to paint the square (Line 162).

My second problem is moving the square. I put a simple if statement in to check if the mouse event is within the boundaries of the square that I am trying to move however when I try to drag the square the motion is wrong. Any ideas?

Jesse Miller
Ranch Hand

Joined: Apr 05, 2010
Posts: 37
**Correction** the error is on Line 124 of the code I posted.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

All your array elements are still null.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jesse Miller
Ranch Hand

Joined: Apr 05, 2010
Posts: 37
I have tried adding:

redSquare[0] = new RedSquare();
redSquare[1] = new RedSquare();
etc...

but I get a build error for incorrect syntax. Is this the correct way to initialize the array elements?


Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> but I get a build error for incorrect syntax. Is this the correct way to initialize the array elements?

I added the indicated line to the MyPanel constructor, compiled OK and ran OK (as in error free)



here's a simple demo of using a JLabel instead of drawing a rectangle/square
run the code, then drag the label around the screen
Jesse Miller
Ranch Hand

Joined: Apr 05, 2010
Posts: 37
ok great! this seems alot easier using JLabel instead of my method. However I am still faced with the initial problem of adding multiple squares and making them all movable. I have been playing around with the code but I guess I need to read up on JLabel. Is there a quick fix possible? Thanks.

Jesse Miller
Ranch Hand

Joined: Apr 05, 2010
Posts: 37
ok, I got the multiple squares part working. I created an array of panels and assigned the Mouse Listener to all of them so they all are movable individually. Do you see any problems with this implementation?

The next part I am working on is displaying a single number on top of each square that I created. No quite sure how to do this but im guess that I can just create a text label under each square panel I created? Will this work? Thanks
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> I created an array of panels and assigned the Mouse Listener to all of them so they all are movable individually.

I don't understand why you'd create an array of panels - I would have thought an array of JLabels would suit multiple squares

> The next part I am working on is displaying a single number on top of each square that I created.

use a titledBorder
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Moving multiple ojects in Java2D