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 wrote:Why should having the references to these polygons in an array make any difference?
How would you move a single polygon?
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). I'm not sure how to call each polygon (array, array, array, ....) to move at the same time and differently in a loop
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.
Joined: Oct 13, 2005
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
Also, Are you trying to create an "Asteroids" type game?
Joined: Nov 13, 2012
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.
Joined: Oct 13, 2005
You can pass the direction as a constructor parameter.