Reputation: 101
just trying to paint multiple unfilled triangles rotating a central point in Java. Paint one triangle, rotate the points by a certain radius, and paint another one.
int rad = 10 //Radius between the triangles
int num = 20 //Number of triangles
for (int i = 0; i < num; i++){
// (250,250) would be the center
int[] xPoints = (250,175,325) //X points of the first triangle
int[] yPoints = (250,100,100) //Y points of the first triangle
g.drawPolygon(xPoints,yPoints,3); //Paint the shape
}
Of course my code only prints the first triangle, as I'm unsure how to rotate the points. I've searched around and found some trig, but I don't really understand it. Is there a simple way to rotate each point? Thanks.
Upvotes: 1
Views: 906
Reputation: 46960
The Graphics2d
object contains an AffineTransform
and has a call to set it directly to a rotation about given point.
When using this, you often (not always) want to save a copy of the transform first and then restore it so the next use of g
has the original transform rather than a pre- or post-multiplied version:
AffineTransform savedTransform = g.getTransform();
g.rotate(theta, x_center_of_rotation, y_center_of_rotation);
g.setTransform(savedTransform);
Upvotes: 1
Reputation: 168815
Is there a simple way to rotate each point?
Use an AffineTranform
that does the geometry for you.
Some examples can be seen in posts tagged affinetransform. Particularly those of mine, Trashgod, MadProgrammer & HovercraftFullOfEels (my apologies if I forgot someone who has done some nice examples).
Upvotes: 5