渲染旋转图像

时间:2011-12-04 00:42:59

标签: java rotation graphics2d

为什么以下代码无法以某个角度渲染图像。 (我希望图像在图像上的锚定点上渲染)。我认为它在屏幕上旋转。我该如何解决这个问题?

public void drawWeapon(Graphics2D g) {
    int iconSize = main.SOutput.iconSize;
    int ePosX =(int) (getPosX() * iconSize 
                - (int) main.player.getPosX() *iconSize)/iconSize;
    int ePosY =(int) (getPosY() * iconSize 
                - (int) main.player.getPosY() *iconSize)/iconSize;

    int PosX = ePosX + (main.SOutput.resX/2)*iconSize 
                + main.SOutput.xPaddingSide;
    int PosY = ePosY + (main.SOutput.resY/2)*iconSize 
                - getImageSizeY()*iconSize;

    if (inventory[0][main.gui.itemSelected] != null){
    g.rotate(facingLeft?scincePressed:-scincePressed);
    g.drawImage(inventory[0][main.gui.itemSelected].item.getImage(), 
                 PosX + (facingLeft?0:getImageSizeX() * iconSize/2), PosY +
                 (getImageSizeY() * iconSize/2), getImageSizeX() * iconSize/2,
                 getImageSizeY() * iconSize/3, null);
    g.rotate(facingLeft?-scincePressed:scincePressed);
    }
}

1 个答案:

答案 0 :(得分:4)

你应该使用这种工作方式:

  1. 推矩阵
  2. 转换为轮换点
  3. 旋转
  4. 渲染(0,0)
  5. Pop matrix
  6. 在代码中,它将是:

    AffineTransform matrix = g.getTransform();
    g.translate(originX, originY);
    g.rotate(angle);
    g.drawImage(0, 0, ...);
    g.setTransform(matrix);
    

    您也可以尝试Graphics2D.rotate(theta, originX, originY);