我正在尝试编写一个程序,它将绘制围绕中心点旋转的几个形状.结果应该像Spirograph.我正在尝试使用矩形测试它,但我只能让其中两个出现在窗口中.其中一个应该是它应该的位置,但是在第一次旋转之后,它将另一个方向向上抛向窗口的左上角.它应该围绕中心点旋转和绘制.这是我的一部分代码.
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)
{ …Run Code Online (Sandbox Code Playgroud)