2011-10-08 76 views
1

我目前正在嘗試旋轉圖像,然後在不旋轉的頂部繪製圖像。但每當我使用: g2d.rotate(Math.toRadians(rot), (x+15), (y+15)); 我後來繪製的每個圖像也旋轉。有沒有什麼辦法可以旋轉一張圖片,而不是旋轉其餘的圖片(真的很難解釋)。 這裏是我的油漆方法:在另一個圖像下旋轉一個圖像

public void draw(Graphics2D g2d) 
{ 
    move(); 
    if(bo.px==+1)rot--; 
    if(bo.px==-1)rot++; 
    g2d.rotate(Math.toRadians(rot), (x+15), (y+15)); 
    g2d.drawImage(img, x, y, null);//this should rotate 
    g2d.drawImage(shine, x, y, null);//this shouldn't 
} 

在此先感謝。

回答

4

您可以保存原始變換,旋轉並繪製第一個圖像,然後在繪製第二個圖像之前應用原始變換。

嘗試

AffineTransform originalTransform = g2d.getTransform(); 
g2d.rotate(Math.toRadians(rot), (x+15), (y+15)); 
g2d.drawImage(img, x, y, null); 
g2d.setTransform(originalTransform); 
g2d.drawImage(shine, x, y, null); 
+0

非常感謝,解決了我的問題! +1 – chrypthic

1

繪製旋轉後的圖像後,需要執行反轉以使其回到原始的非旋轉狀態。

public void draw(Graphics2D g2d) 
{ 
    move(); 
    if(bo.px==+1)rot--; 
    if(bo.px==-1)rot++; 
    g2d.rotate(Math.toRadians(rot), (x+15), (y+15)); 
    g2d.drawImage(img, x, y, null);//this should rotate 
    g2d.rotate(-Math.toRadians(rot), (x+15), (y+15)); // this resets the rotation! 
    g2d.drawImage(shine, x, y, null);//this shouldn't 
} 
+0

也許更簡單,更清潔,容易出錯做的只是保存原始的AffineTransform巴拉方法更少。 1+到巴拉。 –