-1
我想要采取这个代码我终于得到了绘制一颗星的工作,并且困惑于如何让它工作绘制25个不同的恒星(例如不同的侧面和spikinness,但我是不知道该如何去了解它到底。我想我会做一个新的随机变量int randomStars = (int)(Math.random()*25+1); // Variable for 25 Random Stars
随机生成的明星,但我有点困惑在哪里把它从那里。Java:绘制“随机”星星
我会很感激的帮助。
我的代码(使用DrawingPanel.java):
import java.awt.*;
public class StarSampler {
public static void main(String[] args)
{
DrawingPanel panel = new DrawingPanel(500, 500);
Graphics2D g = panel.getGraphics();
g.setColor(Color.BLUE);
panel.setBackground(new Color(250, 0, 0));
fillStar(g, 250, 250, 150, 5, .3); // How to rotate it to start at center?
}
public static void fillStar(Graphics2D g, int ctrX, int ctrY, int radius, int nPoints, double spikiness)
{
double xDouble[] = new double[2*nPoints];
double yDouble[] = new double[2*nPoints];
int xPoint[] = new int[2*nPoints];
int yPoint[] = new int[2*nPoints];
nPoints = (int) (nPoints * 2);
int randomStars = (int)(Math.random()*25+1); // Variable for 25 Random Stars
// Would Nestest loop go here? for (randomStars++; randomStars < 25; randomStars++)
for (int i = 0; i < nPoints; i++)
{
double iRadius = (i % 2 == 0) ? radius : (radius * spikiness);
double angle = (270) + (i * 360.0)/(nPoints);
xPoint[i] = (int) (ctrX + iRadius * Math.cos(Math.toRadians(angle)));
yPoint[i] = (int) (ctrY + iRadius * Math.sin(Math.toRadians(angle)));
}
g.fillPolygon(xPoint, yPoint, nPoints); // Creates polygon
}
}
我的输出:
你想把它们贴在一起还是一个接一个? – Paul
@Paul目标是彼此相邻,但我想随机的任何地方都可以工作,如果他们不是完全彼此顶部。 – Aramza
好的。首先:您需要为每颗星创建两个数字(假设它们大小相同):半径和尖刺。然后使用'fillStar'方法渲染星星。只需循环插入星星的位置并将它们用作中心坐标即可。 – Paul