我目前是Java班的大学生,我今晚有一个项目(我知道的是短期通知)......
以下是说明: “在这个项目中,你被要求编写一个Java程序,在固定点周围多次绘制一个正多边形。” 用户给出3个整数...一个用于形状类型(3 =三角形,4 =正方形,5 =五边形,0 =圆形),一个用于形状半径(如果它刻在一个圆圈中),一个用于形状他们想要绘制形状的次数。然后程序应该围绕窗口中心旋转指定的时间,每个形状均匀分布。 例如... ![正方形重复24次。] [1]
我已经找到了三角形和正方形的代码,但我真的被五角大楼困住了。我尝试过的每一个代码都不会画一个五边形,更不用说旋转它了24次......如果有人能指导我朝正确的方向发展,我会非常感激!!! {/ p>
到目前为止,这是我所拥有的...它非常粗糙,但它是一个开始!
public class ShapeDrawer extends JPanel{
public double x1, x2, x3, x4, x5, y1, y2, y3, angle= 0, changeAngle;
public int radius;
public int amount, item, center;
public ShapeDrawer(int sh, int ra, int am){
item = sh;
radius = ra;
amount = am;
changeAngle = 360/amount;
center = (radius+500)/2;
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
if(item==3)
{
g2d.setColor(Color.MAGENTA);
for(int i = 0; i < amount; i++){
x1 = center + ((radius * Math.sqrt(3)) * Math.cos(Math.toRadians((angle+(i *changeAngle)) + 300)));
y1 = center + ((radius * Math.sqrt(3)) * Math.sin(Math.toRadians((angle+(i *changeAngle)) + 300)));
x2= center + ((radius * Math.sqrt(3)) * Math.cos(Math.toRadians((angle+(i *changeAngle)) + 240)));
y2= center + ((radius * Math.sqrt(3)) * Math.sin(Math.toRadians((angle+(i *changeAngle)) + 240)));
int[] xValues = {(radius+500)/2, (int)x1, (int)x2};
int[] yValues = {(radius+500)/2, (int)y1, (int)y2};
Polygon tri = new Polygon(xValues, yValues,3);
g2d.drawPolygon(tri);
}
}
if(item==4)
{
g2d.setColor(Color.CYAN);
for(int i = 0; i<amount; i++)
{
x1= center + ((radius*2) * Math.cos(Math.toRadians((angle+(i*changeAngle)) + 315)));
y1= center + ((radius*2) * Math.sin(Math.toRadians((angle+(i*changeAngle)) + 315)));
x2= center + ((radius* Math.sqrt(8)) * Math.cos(Math.toRadians((angle+(i*changeAngle)) + 270)));
y2= center + ((radius* Math.sqrt(8)) * Math.sin(Math.toRadians((angle+(i*changeAngle)) + 270)));
x3= center + ((radius*2) * Math.cos(Math.toRadians((angle+(i*changeAngle)) + 225)));
y3= center + ((radius*2) * Math.sin(Math.toRadians((angle+(i*changeAngle)) + 225)));
int[] xValues = {center, (int)x1, (int)x2, (int)x3};
int[] yValues = {center, (int)y1, (int)y2, (int)y3};
Polygon squa = new Polygon(xValues, yValues,4);
g2d.drawPolygon(squa);
}
if(item==5)
{
g2d.setColor(Color.GREEN);
for(int i=0;i<amount;i++){
Polygon pent = new Polygon();
for (int i = 0; i < 5; i++)
{
pent.addPoint((int) (center+radius * Math.cos(i * 2 * Math.PI / 5)), (int) (center+radius * Math.sin(i * 2 * Math.PI / 5)));
}
g2d.drawPolygon(pent);
}
}
if(item==0)
{
g2d.setColor(Color.BLUE);
for(int i =0; i < amount; i++)
{
}
}
}
}
}