Reset Graphics2D Object in Java

I experimented with Graphics2D in Java. But as usual, I'm stuck .: P The problem is this: Suppose I have this code,

Graphics2D g=(Graphics2D)(this.getGraphics()); //Inside a JFrame g.rotate(Math.PI/8); g.drawLine(10, 20, 65, 80); //I want this one and all following lines to be drawn without any rotation g.drawLine(120, 220, 625, 180); 

Is it possible??? I know there must be some way, but I cannot figure it out. Please, help.

+8
java swing graphics2d
source share
2 answers

What you want to do is restore the conversion.

Try

 AffineTransform oldXForm = g.getTransform(); g.rotate(...); g.drawLine(...); g.setTransform(oldXForm); // Restore transform g.drawLine(...); 
+14
source share

Call getTransform() (gives you a copy), rotate, draw, and then use setTransform() to restore the state. docs for setTransform() even have an example.

+4
source share

All Articles