This week's giveaway is in the EJB and other Java EE Technologies forum. We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line! See this thread for details.
You can rotate the Graphics object to draw the image in the 8 different directions. Call the rotate() method and specify the number of radians to rotate by.
I've no idea what the performance implications of using rotate are so if this paintComponent method is likely to be called lots of times and you notice redraws are slow it may be more efficient to pre-compute the 8 rotated images either by providing 8 separate images to load or by creating and rotating the 8 images on constructing the object.
Joined: Jun 15, 2011
That's a vry good point. I would have to call that method very very often. It's really better to create the images beforehand.
Isn’t rotate a Graphics2D method?
I have had problems with the Affine Transforms of Graphics2D; they are easy to execute and difficult to undo. It is shear that is the worst; you won’t notice much with rotate. But I think you should do several things.
1: Get a copy of the Graphics object.
2: Cast it to Graphics2D.
3: Translate the “origin” to the centre of the panel drawn on.
6: Discard the graphics object and go back to the original Graphics object.
I think rotation is ↺ anti‑clockwise in my example, but you would have to check that. I notice there is a rotate method which does the translation, too. There are 4 affine transforms: shear, rotate, translate and scale.
Joined: Aug 07, 2007
Campbell Ritchie wrote:Isn’t rotate a Graphics2D method?