循环中的旋转图像

时间:2018-05-21 11:59:55

标签: java swing rotation image-rotation

我的图像旋转有问题。这是我尝试过的,但它不起作用。

任何帮助都将不胜感激。

@Override
public void paintComponent(Graphics p) {

    BufferedImage arrow = LoadImage("C:\\Users\\Pawel Celuch\\Desktop\\arrow.png");

    super.paintComponent(p);
    Graphics2D g2d = (Graphics2D) p;
    g2d.drawImage(arrow, (int)x, (int)y+550, this);

}

1 个答案:

答案 0 :(得分:0)

您可以在java中使用具有AffineTransform类角度的rotate方法。

private double currentAngle = 60.7D;

@Override
protected void paintComponent(final Graphics g) {
     BufferedImage arrow = LoadImage("C:\\Users\\Pawel Celuch\\Desktop\\arrow.png");
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    AffineTransform orgX = g2d.getTransform();
    AffineTransform newX = (AffineTransform) (orgX.clone());
    // adjust center of image view of the panel
    int centX = this.getWidth() / 2;
    int centY = this.getHeight() / 2;
    newX.rotate(Math.toRadians(currentAngle), centX, centY);
    g2d.setTransform(newX);
    g2d.drawImage(arrow, x, y+550, this);
    g2d.setTransform(orgX);

}

currentAngle的范围必须介于0到360度之间。

enter image description here