我正在嘗試編寫一個程序,該程序將繪製圍繞中心點旋轉的幾種形狀。結果應該像一個Spirograph。我試圖用矩形測試它,但我只能讓它們中的兩個出現在窗口中。其中一個是它應該在的位置,但是在第一次旋轉之後,它將另一個方形投影到窗口的左上角。它應該圍繞中心點旋轉和拉伸。這是我的代碼的一部分。在指定點周圍旋轉Java 2D圖形
import java.awt.*;
import java.awt.geom.Ellipse2D;
import javax.swing.JPanel;
public class Shapes extends JPanel
{
private double angle;
private int type;
private int radius;
private int repeats;
public Shapes(int t, int r, int z)
{
type = t;
radius = r;
repeats = z;
angle = 360/repeats;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g.setColor(Color.red);
int centerX = getWidth()/2;
int centerY = getHeight()/2;
double curAngle = 0;
for(int i=0; i<=repeats; i+=1)
{
g.setColor(Color.blue);
Rectangle tangle = new Rectangle(0, 0, radius, radius);
g2d.rotate(Math.toRadians(curAngle));
g2d.translate(centerX, centerY);
g2d.draw(tangle);
curAngle += angle;
}
}
類似[this](http://stackoverflow.com/questions/12964983/rotate-image-around-character-java/12971987#12971987)? – MadProgrammer
小心旋轉和翻譯混合 – MadProgrammer
就像這樣。 http://i.imgur.com/y6hAMV8.png –