aspose file tools*
The moose likes Java in General and the fly likes How can i move polygons that are in an array independently? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How can i move polygons that are in an array independently?" Watch "How can i move polygons that are in an array independently?" New topic
Author

How can i move polygons that are in an array independently?

Thomas Crew
Greenhorn

Joined: Nov 13, 2012
Posts: 10
Hey,
I'm writing a program that displays an array of various GPolygons on a canvas. I want to have them to slowly move around the canvas independently from one another. The problem is that I'm unsure of how to have each polygons move differently and simultaneously since these 15 polygons are in a single array. I used polymorphism to create the different shape classes that extended to a common class. Then used a "driver" class to create an array of them and draw them on a canvas. Thanks for the help in advance!
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
Why should having the references to these polygons in an array make any difference?

How would you move a single polygon?

Bill
Thomas Crew
Greenhorn

Joined: Nov 13, 2012
Posts: 10
William Brogden wrote:Why should having the references to these polygons in an array make any difference?

How would you move a single polygon?

Bill


I'm not sure if i understand your questions correctly. If I wanted to move a single polygon i would call the method move() like array[i].move(x, y); or array[i].movePolar(r, theta) with "i" being an individual polygon in the array (so 1 of the 15 in aray[15]). I'm not sure how to call each polygon (array[1], array[2], array[3], ....) to move at the same time and differently in a loop
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38412
    
  23
There are all sorts of ways you can make a polygon or another shape move. I would suggest you make your polygon class implement an interface like thisYou can overload that method to take float, float and double, double arguments, if you wish, because some graphics classes work on doubles and floats.
What WB means is: work out, using a piece of paper, how you would move any shape. It would have been a lot easier if you had designed the polygons with an origin point, so you might have to go back to the polygon class and add an origin.
Beware: If you use the methods of the Graphics2D class which execute affine transforms, you would do well to use a copy of the graphics object, which I think you get with its create() method. The translate() and scale() methods don’t usually cause problems, but shear() can cause odd effects because of arithmetical inaccuracies. If you use a copy object, the inaccuracies all vanish when you let the object go out of scope.
To move each shape in an array, you can iterate the array and call the move method. You iterate your array with a for-each loop just as you would for any other array.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38412
    
  23
If you have the shapes all in the same array, you are implicitly grouping them together. You could have fields in each object which tell it where to go which direction to move in (provided to the constructor), and call a move() method
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 859

Is this related to your other recent post?

Also, Are you trying to create an "Asteroids" type game?

WP
Thomas Crew
Greenhorn

Joined: Nov 13, 2012
Posts: 10
Campbell Ritchie wrote:If you have the shapes all in the same array, you are implicitly grouping them together. You could have fields in each object which tell it where to go which direction to move in (provided to the constructor), and call a move() method


I thought of that but the array of 20 shapes are not all different from one another. So if I assigned fields to the object (if this is what you mean), wouldn't (for example) all the diamonds all move the same?

William O'Sullivan, yes this question is related to my previous post but i tried being more specific (Sorry if it seems like i'm posting the same topic twice). But no i'm not trying to code a asteroid game. Just a Graphicsprogram animation that displays a canvas with the said shapes moving and animating according to the stated rules/parameters.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38412
    
  23
You can pass the direction as a constructor parameter.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can i move polygons that are in an array independently?