5 Replies Latest reply: Feb 7, 2011 4:13 PM by 810112

# Rotating a Polygon

im currently using this code-
``````public void setRotation(int degrees) {
AffineTransform at = new AffineTransform();

Shape rotatedPoly = at.createTransformedShape(getBounds());

Polygon test=(Polygon) rotatedPoly;

this._rotation = degrees;
}``````
which I think works until I try to cast a shape to a Polygon which doesn't work. I really need to be able to access the vertices in the polygon once it is rotated. Does anyone know how to do that?
• ###### 1. Re: Rotating a Polygon
Im not sure if AffineTransform is the best way to do this or if it is any help?
• ###### 2. Re: Rotating a Polygon
You can use one of the AffineTransform.public void transform() methods passing array of Polygon points and create a new Polygon from transfrormed points.
• ###### 3. Re: Rotating a Polygon
I tried this
``````public void setRotation(int degrees) {
AffineTransform at = new AffineTransform();

Point[] rVal= new Point[getBounds().npoints];

at.transform(polygonToArray(getBounds()),0, rVal,0,0);

this.bounds=new Polygon(pointToIntArray(rVal,true),pointToIntArray(rVal,false),rVal.length);
//Polygon test=(Polygon) rotatedPoly;

this._rotation = degrees;
}

private Point[] polygonToArray(Polygon p){
Point[] array=new Point[p.npoints];

int[] x= p.xpoints;
int[] y= p.ypoints;

for (int i= 0; i < p.npoints; i++) {
array=new Point(x[i],y[i]);
}

return array;
}

private int[] pointToIntArray(Point[] p, boolean isX){
int[] r=new int[p.length];

System.out.println(p.length);

if(isX){
for (int i= 0; i < p.length; i++) {
System.out.println(p[i]);
r[i]=p[i].x;
}
}else{
for (int i= 0; i < p.length; i++) {
r[i]=p[i].y;
}
}

return r;
}
but the array rVal is full of null values.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ``````
• ###### 4. Re: Rotating a Polygon
Why do you pass last 0 here at.transform(polygonToArray(getBounds()),0, rVal,0,0); ?
The param should be int numPts - length of you array.
• ###### 5. Re: Rotating a Polygon
Wow I feel stupid. I completely missed that thank you so much. I also had another mistake for others trying to use this code in the future

it should be changed to Point2D
`````` public void setRotation(int degrees) {
AffineTransform at = new AffineTransform();

Point2D[] rVal= new Point2D[getBounds().npoints];

at.transform(polygonToArray(getBounds()),0, rVal,0,rVal.length);

this.bounds=new Polygon(pointToIntArray(rVal,true),pointToIntArray(rVal,false),rVal.length);
//Polygon test=(Polygon) rotatedPoly;

this._rotation = degrees;
}

private Point[] polygonToArray(Polygon p){
Point[] array=new Point[p.npoints];

int[] x= p.xpoints;
int[] y= p.ypoints;

for (int i= 0; i < p.npoints; i++) {
array=new Point(x[i],y[i]);
}

return array;
}

private int[] pointToIntArray(Point2D[] p, boolean isX){
int[] r=new int[p.length];

System.out.println(p.length);

if(isX){
for (int i= 0; i < p.length; i++) {
System.out.println(p[i]);
r[i]=(int)(p[i].getX()+.5);
}
}else{
for (int i= 0; i < p.length; i++) {
r[i]=(int)(p[i].getY()+.5);
}
}

return r;
}
``````