在 Java 中重置 Graphics2D 对象

发布于 2024-11-20 00:27:40 字数 346 浏览 7 评论 0原文

我正在用 Java 尝试 Graphics2D。但像往常一样,我被困住了。 :P 问题是: 假设我有这段代码,

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

可能吗???我知道一定有办法,但我无法弄清楚。请帮忙。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抚你发端 2024-11-27 00:27:40

您要做的就是恢复转换。

尝试

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

g.setTransform(oldXForm); // Restore transform
g.drawLine(...);

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(...);
迷迭香的记忆 2024-11-27 00:27:40

调用getTransform()(给您一个副本)、旋转、绘制,然后使用setTransform()恢复状态。 setTransform() 的文档甚至有一个示例。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文