-2
随机三角形我有一个窗口打印500个三角形的一个问题。打印输出500个在Java
的代码,我创建的节目之一黄金三角,它只是改变,而我调整窗口,我必须让所有500个traingles出现一次。任何想法如何做到这一点?
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class BoringTriangle extends Canvas {
public void paint(Graphics g){
Random nmb = new Random();
//Colours
int x1 = nmb.nextInt(200) + 1;
int x2 = nmb.nextInt(200) + 1;
int x3 = nmb.nextInt(200) + 1;
int x4 = nmb.nextInt(500) + 1;
int x5 = nmb.nextInt(500) + 1;
int x6 = nmb.nextInt(500) + 1;
int x7 = nmb.nextInt(500) + 1;
int x8 = nmb.nextInt(500) + 1;
int x9 = nmb.nextInt(500) + 1;
for(int z = 1; z<=500; z++) {
g.setColor(new Color(x1, x2, x3));
g.fillPolygon(new int[]{x4, x5, x6}, new int[]{x7, x8, x9}, 3);
}
}
public static void main(String[] args)
{
// You can change the title or size here if you want.
JFrame win = new JFrame("Boring Traingle lul");
win.setSize(800,600);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BoringTriangle canvas = new BoringTriangle();
win.add(canvas);
win.setVisible(true);
}
}
当画布被绘制,你生成9个随机数字。这就是为什么它只在画布重新涂漆时才会改变。 9号,重复使用500次,不给你500个随机三角形。它可能会给你一个随机三角形500次。 – khelwood
你画他们在彼此的顶部。 – eldo