我正在尝试用 Java 制作一些形状,我创建了两个矩形,它们正常显示,但最近我集成了一个多边形形状代码,但在运行程序时它没有显示。请有人帮忙!
下面是运行后的截图:
这是我使用的代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class shapes extends JPanel{
int midX = 120;
int midY = 60;
int radius[] = {118,40,90,40};
int nPoints = 16;
int[] X = new int[nPoints];
int[] Y = new int[nPoints];
int i;
double max;
public void paintComponent(Graphics gphcs){
super.paintComponent(gphcs);
this.setBackground(Color.WHITE);
gphcs.setColor(Color.BLUE);
gphcs.fillRect(25,25,100,30);
gphcs.setColor(Color.GRAY);
gphcs.fillRect(25,65,100,30);
gphcs.setColor(new Color(190,81,215));
gphcs.drawString("This is my text", 25, 120);
for (double current=0.0; i<nPoints; i++)
{
double x = Math.cos(current*((2*Math.PI)/max))*radius[i % 4];
double y = Math.sin(current*((2*Math.PI)/max))*radius[i % 4];
X[i] = (int) x+midX;
Y[i] = (int) y+midY;
}
gphcs.setColor(Color.RED);
gphcs.fillPolygon(X, Y, nPoints);
}
}
实际上,我希望多边形显示为星形,但它甚至根本没有出现!
谢谢..