Reset Graphics2D object in Java

半城伤御伤魂 提交于 2020-05-11 05:13:47

问题


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.


回答1:


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(...);



回答2:


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



来源:https://stackoverflow.com/questions/6681601/reset-graphics2d-object-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!