-
Notifications
You must be signed in to change notification settings - Fork 0
/
RandomTriangles.java
60 lines (50 loc) · 1.76 KB
/
RandomTriangles.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.awt.*;
import javax.swing.*;
import java.util.Random;
public class RandomTriangles extends Canvas{
Random rand = new Random();
int x = rand.nextInt(500)+100;
int y = rand.nextInt(500)+100;
int color = 1 + rand.nextInt(9);
public void paint(Graphics g)
{
int n = 0;
while(n<10)
{
Random rand = new Random();
int x = rand.nextInt(500)+100;
int y = rand.nextInt(500)+100;
int color = 0 + rand.nextInt(3);
Polygon tri = new Polygon();
tri.addPoint(300,100);
tri.addPoint(400,350);
tri.addPoint(200,400);
if(color==1){
g.setColor(Color.red);
g.fillPolygon(tri);
}
else if(color==2){
g.setColor(Color.yellow);
g.fillPolygon(tri);
}
else if(color==3){
g.setColor(Color.blue);
g.fillPolygon(tri);
}
else if(color==0){
g.setColor(Color.cyan);
g.fillPolygon(tri);
}
n++;
}
}
public static void main(String[]args)
{
JFrame f = new JFrame("Graphics: RandomTriangles");
f.setSize(800,600);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
RandomTriangles canvas = new RandomTriangles();
f.add(canvas);
f.setVisible(true);
}
}