If you use plain Graphics
, hit Graphics2D
first:
Graphics2D g2d = (Graphics2D)g;
To rotate an integer Graphics2D
:
g2d.rotate(Math.toRadians(degrees));
//dibujar forma / imagen (se rotará)
To reset the rotation (so that only one thing turns):
AffineTransform old = g2d.getTransform();
g2d.rotate(Math.toRadians(degrees));
//dibujar forma / imagen (se rotará)
g2d.setTransform(old);
//las cosas que dibujas aquí no serán rotadas
Example:
class MyPanel extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
AffineTransform old = g2d.getTransform();
g2d.rotate(Math.toRadians(degrees));
//dibujar forma / imagen (se rotará)
g2d.setTransform(old);
//las cosas que dibujas aquí no serán rotadas
}
}
If you want to convert a new position to an old AffineTransform, you can use rotate
, scale
and translate
. More information here: Class AffineTransform .
Also here is a interesting answer on rotation. ..