Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Moving multiple ojects in Java2D

 
Jesse Miller
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
**Correction** the error is on Line 124 of the code I posted.
 
Rob Spoor
Sheriff
Pie
Posts: 20388
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All your array elements are still null.
 
Jesse Miller
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic