Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset Graphics2D object in Java

I was experimenting with Graphics2D in Java. But as usual, I am stuck. :P The problem is: 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 am not able to figure it out. Please help.

like image 695
Nishchay Sharma Avatar asked Jul 13 '11 15:07

Nishchay Sharma


2 Answers

What you'll want to do is restore the transform.

Try

AffineTransform oldXForm = g.getTransform();
g.rotate(...);
g.drawLine(...);

g.setTransform(oldXForm); // Restore transform
g.drawLine(...);
like image 53
mre Avatar answered Oct 18 '22 13:10

mre


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

like image 26
Aaron Digulla Avatar answered Oct 18 '22 14:10

Aaron Digulla