2013-04-01 19 views
0

我目前正在嘗試使用仿射變換類旋轉多邊形。使用旋轉方法,多邊形的圖形表示更新,但多邊形的邊界框不會更新。除了更新座標外,我怎樣才能旋轉多邊形?使用仿射變換旋轉多邊形

+0

你是什麼意思的多邊形的「邊界框」?你是否將它存儲爲多邊形的一部分? –

回答

4

創建一個新的Shape,而不是像繪製它一樣旋轉多邊形。例如:

Polygon shape = new Polygon(); 
shape.addPoint(...); 
.... 
Rectangle bounds = shape.getBounds(); 
AffineTransform transform = new AffineTransform(); 
transform.rotate(Math.toRadians(angle), bounds.width/2, bounds.height/2); 

Path2D path = (shape instanceof Path2D) ? (Path2D)shape : new GeneralPath(shape); 
Shape rotated = path.createTransformedShape(transform); 
System.out.println(rotated.getBounds()); 
+0

創建新形狀似乎可行,給我更新的座標。但是我不太瞭解路徑部分......多邊形怎麼可能成爲Path2d的一個實例? –

+0

這不是,這就是爲什麼代碼進行檢查。在這種情況下,多邊形首先轉換爲GeneralPath,因此可以旋轉。 (我剛剛更新了代碼,以便使用「形狀」變量而不是原來的「多邊形」變量。希望這更有意義) – camickr

+0

好吧,謝謝,這真的有幫助! –