我已经在得出了一些图形JPanel
,如圆形,矩形等
但是,我想画一些图形旋转特定程度的量,像一个旋转的椭圆形。 我该怎么办?
我已经在得出了一些图形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
}
}
在你paintComponent()
重写方法中,将图形参数的Graphics2D,调用rotate()
上,使用此Graphics2D,并绘制你的椭圆。