0

I am trying to create a simple rectangle using an AffineTransform. Here is kinda what I am wanting to do...

AffineTransform at = new AffineTransform();
at.transform(width/2, height/2);

switch(direction){
case 1:
     return new Rectangle(at, width, height);
case 2:
     return new Rectangle(at, width*2, height*2);
}

I'm not sure how, or if you can, create a rectangle using an AffineTransform. If anyone knows how to do this please share your information.

user2892875
  • 99
  • 1
  • 1
  • 9
  • [A quick glance at the `Rectangle` Javadoc suggests that you can't pass in an `AffineTransform` as an argument to the constructor.](http://docs.oracle.com/javase/7/docs/api/java/awt/Rectangle.html) You may be best served by extracting the bits you need from the `AffineTransform` and plug them into the `Rectangle` directly. – Makoto Jun 22 '14 at 02:18
  • Try `createTransformedShape()`, mentioned [here](http://stackoverflow.com/q/23644698/230513). – trashgod Jun 22 '14 at 02:28

1 Answers1

1

After applying an AffineTransform, a Rectangle is no longer necessarily an ordinary Rectangle. A Rectangle however derives from Rectangle2D which implements Shape.

And an AffineTransform can transform a Shape into a new Shape with its createTransformedShape method.

That's a best way to go about it, if you want the full power of AffineTransform. You can draw a Shape, test whether points fall inside or outside it, turn it into an Area, etc.

Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79