有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java在中间旋转图像

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

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

请帮忙


共 (2) 个答案

  1. # 1 楼答案

    你已经走到一半了。你需要做的是两个翻译。大致如下:

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

    希望这对你有用

  2. # 2 楼答案

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

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