How can I rotate different figures independently?

1

I need to rotate several figures, but the whole plane moves

Methods to turn that I use

this.graphics.transform(AffineTransform.getRotateInstance(Math.toRadians(this.direction),this.x, this.y));

And the other one

this.graphics.rotate(Math.toRadians(this.direction),this.x, this.y);
    
asked by Felipe Chaparro 07.04.2018 в 04:58
source

1 answer

0

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. ..

    
answered by 07.04.2018 / 16:18
source