• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Rotating a Graphic

 
Steven Joost
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 49793
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2970
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic