我正在嘗試將影像旋轉到游標位置(旋轉應該是從影像的中心開始并旋轉到游標位置)
public void rotateImg(Graphics g)
{
Graphics2D g2 = (Graphics2D)g.create();
int cursorX = MouseInfo.getPointerInfo().getLocation().x - panel.getLocationOnScreen().x; // Mouse pos
int cursorY = MouseInfo.getPointerInfo().getLocation().y - panel.getLocationOnScreen().y;
Point center = new Point(x size/2,y size/2); //Image center (x,y are the player's coordinates)
double dx = cursorX - center.getX();
double dy =cursorY - center.getY();
System.out.println(Math.toDegrees(Math.atan2(dx,dy)));
g2.rotate(Math.toRadians(Math.atan2(dy,dx)),center.x,center.y);
g2.drawImage(playerImage,x,y,size,size, null);
}
這個函式在一個執行緒內運行,所以它永遠不會停止,我檢查了游標位置是否正在更新(它們是),角度也是如此,但是當我運行它時,它幾乎沒有旋轉,甚至不能旋轉 45 度
先謝謝了!
uj5u.com熱心網友回復:
試過這個:
public void rotateImg(Graphics g)
{
double degrees;
Graphics2D g2 = (Graphics2D)g.create();
Point center = new Point(x size/2,y size/2); //Image center (x,y are the player's coordinates)
int cursorX = MouseInfo.getPointerInfo().getLocation().x - panel.getLocationOnScreen().x; // Mouse pos
int cursorY = MouseInfo.getPointerInfo().getLocation().y - panel.getLocationOnScreen().y;
double dx = cursorX - center.getX();
double dy = cursorY - center.getY();
degrees = Math.toDegrees(Math.atan2(dy,dx)) 90;
g2.rotate(Math.toRadians(degrees),center.x,center.y);
g2.drawImage(playerImage,x,y,size,size, null);
}
有效!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/359100.html
