如果您使用的是普通图形,请首先转换为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
}
}