如何在Java中旋转图形

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

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

回答:

如果您使用plain Graphics,则强制转换为Graphics2Dfirst:

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中旋转图形 的全部内容, 来源链接: utcz.com/qa/424839.html

回到顶部