为什么以下代码无法以某个角度渲染图像。 (我希望图像在图像上的锚定点上渲染)。我认为它在屏幕上旋转。我该如何解决这个问题?
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 :(得分:4)
你应该使用这种工作方式:
在代码中,它将是:
AffineTransform matrix = g.getTransform();
g.translate(originX, originY);
g.rotate(angle);
g.drawImage(0, 0, ...);
g.setTransform(matrix);