gpt4 book ai didi

java - 如何在Java中旋转图形

转载 作者:搜寻专家 更新时间:2023-11-01 01:00:18 24 4
gpt4 key购买 nike

我在JPanel 中绘制了一些图形,如圆形、矩形等。

但我想绘制一些旋转特定度数的图形,如旋转的椭圆。我该怎么办?

最佳答案

如果您使用的是纯 Graphics,请先转换为 Graphics2D:

Graphics2D g2d = (Graphics2D)g;

旋转整个 Graphics2D:

g2d.rotate(Math.toRadians(degrees));
//draw shape/image (will be rotated)

重置旋转(所以你只旋转一件事):

AffineTransform old = g2d.getTransform();
g2d.rotate(Math.toRadians(degrees));
//draw shape/image (will be rotated)
g2d.setTransform(old);
//things you draw after here will not be rotated

例子:

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));
//draw shape/image (will be rotated)
g2d.setTransform(old);
//things you draw after here will not be rotated
}
}

关于java - 如何在Java中旋转图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14124593/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com