【问题标题】:Rotation image in loop循环旋转图像
【发布时间】:2018-05-21 11:59:54
【问题描述】:

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

任何帮助将不胜感激。

@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);

}

【问题讨论】:

  • 那么..你的问题是什么?请注意,如果它是像 “有人可以帮助我吗?” 之类的模糊和广泛的内容,它可能很快就会关闭。一个提示:不要做潜在的“长时间运行”的任务,比如在绘画方法中加载资源。在构建组件时加载图像并将图像存储为类的属性。
  • 更改您的问题,使其更易于理解,例如“如何显示旋转图像?”
  • 不要在画法中读取图像。您无法控制何时重新绘制组件,因此您不想继续阅读图像。在类的构造函数中读取图像。

标签: java swing rotation image-rotation


【解决方案1】:

您可以在java中使用带有AffineTransform类的角度的旋转方法。

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 度之间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多