如何在Java中随机化特定点的x / y值以及它们创建的多边形的颜色? 我有代码我试图用连续500次创建这个效果,有500个不同的三角形,以相同的方式随机化每个三角形(这是我要求帮助的代码):< / p>
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Canvas;
import javax.swing.JFrame;
import java.awt.Polygon;
import java.util.Random;
public final class CRassignment34 extends Canvas
{
private static final long serialVersionUID=99L;
public static final void main (String[] args)
{
final JFrame win=new JFrame("500 randomly-selected triangles (a trainwreck indeed)");
win.setSize(1024,768);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.add(new CRlab34());
win.setVisible(true);
}
public final void paint (Graphics g)
{
for (int i=0;i<500;i++)
{
final Polygon tri=new Polygon();
final Random r=new Random();
tri.addPoint(x,y);
tri.addPoint(x,y);
tri.addPoint(x,y);
g.setColor(colorRand());
g.fillPolygon(tri);
}
}
public final void colorRand(Color c)
{
}
}