File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Rotating a Graphic Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Rotating a Graphic" Watch "Rotating a Graphic" New topic

Rotating a Graphic

Steven Joost

Joined: Sep 29, 2012
Posts: 28
Hello guys, I want to make a Graphic rotating, but don't know how.
I'm using GeneralPaths, and already wrote this:

The result is a blue square, and I only need a formula for rotating the corners of p.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46405
Do you mean that you rotate it the way a sheet of paper would rotate while lying on the table?
I suggest you try affine transforms on the Graphics object. Somebody else asked a similar question last week, so you could adapt what I said here, by using the rotate() method. It is worth finding out whether you rotate in degrees or radians, and whether it is clockwise or anticlockwise.
Tony Docherty

Joined: Aug 07, 2007
Posts: 2839
Also you shouldn't put a sleep in the paintComponent method or the GUI will just hang. If you want to repaint at a regular interval use a Timer to call repaint every n milliseconds.
Kemal Sokolovic

Joined: Jun 19, 2010
Posts: 825

You should cast your Graphics to Graphics2D at the beginning of your method and work with it.

To apply rotation (or even some other transformation) on your coordinates, you should check the AffineTransform API for more information. For your requirement:

In case you want to apply transformation just temporary, you can get old transformation that will be set back after the one you're doing:

The quieter you are, the more you are able to hear.
I agree. Here's the link:
subject: Rotating a Graphic
It's not a secret anymore!