1

Ok when a user draws a Rectangle on top of an image, I want to find all the rotated Rectangles ahead of time for all image rotation angles (90,180,270,360).

According to Java API I can just keep calling the Graphics2D rotate() method. I can then use this Graphics2D transformer to get the rotated Rectangle.

This works for the very first rotation(1.5708) call. I get the correct Rectangle Point. All other calls after that return the wrong Rectangle Point after using Transformer.

I think my problem is the Graphics2D translate(x,y). I don't understand how to use it.

Anyone knows how to fix my code so that it will return the correct Rectangle after every rotation?

Thank you.

public void rotateRectangles(BufferedImage bim,int width,int height,Rectangle rect){
   BufferedImage bim = new BufferedImage(height, width,BufferedImage.TYPE_INT_RGB);
   Graphics2D g2d = (Graphics2D) (bufferedImage.createGraphics());
   g2d.translate(bufferedImage.getHeight(),0); 

   //Get Rectangle for 90 degree image rotation. This always good.
   g2d.rotate(1.5708);
   Shape shape = g2d.getTransform().createTransformedShape(rect); 
   Rectangle rotatedRect = shape.getBounds(); 
   System.out.println("rotated rectangle at 90 degrees.  Point x="+rotatedRect.x+"  y="+rotatedRect.y);


   //Get Rectangle for 180 degree image rotation. Getting wrong rotatedRect.
   g2d.rotate(1.5708);
   shape = g2d.getTransform().createTransformedShape(rect); 
   rotatedRect = shape.getBounds(); 
   System.out.println("rotated rectangle at 180 degrees. Point x="+rotatedRect.x+"  y="+rotatedRect.y); 


  //Get Rectangle for 270 degree image rotation. Getting wrong rotatedRect.
   g2d.rotate(1.5708);
   shape = g2d.getTransform().createTransformedShape(rect); 
   rotatedRect = shape.getBounds(); 
   System.out.println("rotated rectangle at 270 degrees. Point x="+rotatedRect.x+"  y="+rotatedRect.y);


  //Get Rectangle for 360 degree image rotation.Getting wrong rotatedRect.
   g2d.rotate(1.5708);
   shape = g2d.getTransform().createTransformedShape(rect); 
   rotatedRect = shape.getBounds(); 
   System.out.println("rotated rectangle at 360 degrees. Point x="+rotatedRect.x+"  y="+rotatedRect.y);         

}

Thank you.

Marquinio
  • 4,601
  • 13
  • 45
  • 68
  • What is it returning and what are you expecting? – jzd Feb 24 '11 at 17:40
  • I need an actual Rectangle object for all 90,180,270,360 degrees. Just trying to create the Rectangles ahead of time for all rotations. – Marquinio Feb 24 '11 at 17:59
  • yes, but what is the code outputting the second time and what are you expecting specifically. – jzd Feb 24 '11 at 18:09
  • I modified my question. Previous was too confusing. After the first rotation the Rectangles have the wrong Point(x,y). The Height and Width are ok thou. – Marquinio Feb 24 '11 at 18:43

1 Answers1

2

Instead of rotating the graphics context's affine transform via g2d.rotate(), consider using createTransformedShape() as suggested in this example.

Addendum: Note in particular that the baseline of the example Polygon is initially centered on the origin. As a result, the initial transformation is rotation around the origin. In your case, you can use the rotate() method that includes an anchor point, which would be the center of your rectangle.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045