繁体   English   中英

Java中未显示多边形

[英]Polygon shape does not show up in Java

我试图用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);
}
}

从根本上说,我希望多边形显示为星形,但根本不出现!

谢谢..

您所有的多边形坐标都相同。 尝试

for (int i=0; i < nPoints; i++) {
   double x = Math.cos(i * ((2 * Math.PI) / nPoints)) * radius[i % 4];
   double y = Math.sin(i * ((2 * Math.PI) / nPoints)) * radius[i % 4];

   X[i] = (int) x + midX;
   Y[i] = (int) y + midY;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM