为什么下面的代码不能按一定角度渲染图像.
(我希望图像在图像的锚点上呈现).我认为它围绕屏幕旋转.我怎样才能解决这个问题?
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);
}
}
解决方法:
您应该使用这种工作方式:
>推矩阵
>平移到旋转点
>旋转
>在(0,0)渲染
>流行矩阵
在代码中,它将是:
AffineTransform matrix = g.getTransform();
g.translate(originX, originY);
g.rotate(angle);
g.drawImage(0, 0, ...);
g.setTransform(matrix);
您也可以尝试Graphics2D.rotate(theta, originX, originY);