【问题标题】:Java rotate an image in the centerJava在中心旋转图像
【发布时间】:2012-05-23 20:13:18
【问题描述】:

如果图像本身,我如何在中心旋转图像? 此代码有效,但它会旋转屏幕左上角的图像:

    AffineTransform oldtrans = new AffineTransform();
    AffineTransform trans = new AffineTransform();

    trans.setToIdentity();
    trans.rotate(Math.toRadians(angle));
    trans.translate(_x-(width/2), _y-(height/2));
    g.setTransform(trans);
    g.drawImage(this.getImage(), (int)_x, (int)_y, (int)width, (int)height, null);
    trans.setToIdentity();
    g.setTransform(oldtrans);

请帮忙!!!

【问题讨论】:

    标签: java image rotation center


    【解决方案1】:

    您应该在 rotate() 调用中再提供两个参数:

    affineTransform.rotate(Math.toRadians(angle), m_imageWidth/2, m_imageHeight/2); 
    

    【讨论】:

    • 我忘了...这应该比我建议的单独翻译更快(但本质上是相同的)。代码也更少(+1)。
    【解决方案2】:

    从字面上看,你已经成功了一半。你要做的是两个翻译。大致如下:

    1. 将图像平移到其中心 (tanslate(x-width/2, y-width/2)。
    2. 按角度弧度旋转图像(就像您一样)。
    3. 将图像转换回其原始位置 (tanslate(x+width/2, y+width/2)。

    希望这对你有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-26
      • 1970-01-01
      • 2020-05-19
      • 1970-01-01
      • 2022-01-09
      相关资源
      最近更新 更多