-
Notifications
You must be signed in to change notification settings - Fork 9
/
Rectangular.java
99 lines (95 loc) · 2.69 KB
/
Rectangular.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import javax.swing.*;
import java.awt.*;
import static java.lang.Math.random;
import java.util.*;
/**
* @author Jiayi Sun
*/
public class Rectangular extends JFrame {
public Rectangular() {
setSize(800, 800);
Container c = getContentPane();
RecBoard r = new RecBoard();
c.add(r);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Rectangular();
}
}
class RecInfo extends Thread {
int x, y, length, change;
int flag = 1;
int count1 = 0;
int count2 = 0;
public RecInfo(int x, int y, int length, int change) {
this.x = x;
this.y = y;
this.length = length;
this.change = change;
}
public void run() {
while (true) {
if (flag == 1) {
length += change;
x = x - change / 2;
y = y - change / 2;
try {
Thread.sleep(300);
} catch (Exception e) {
e.printStackTrace();
}
count1++;
if (count1 == 5) {
flag = -flag;
count1 = 0;
}
}
if (flag == -1) {
length -= change;
x = x + change / 2;
y = y + change / 2;
try {
Thread.sleep(300);
} catch (Exception e) {
e.printStackTrace();
}
count2++;
if (count2 == 5) {
flag = -flag;
count2 = 0;
}
}
}
}
}
class RecBoard extends JPanel {
public ArrayList<RecInfo> r;
int count = 0; // add more rects
int j = 0;
public RecBoard() {
r = new ArrayList<>();
r.add(new RecInfo((int) (random() * 700 + 100), (int) (random() * 700 + 100), 15, 5));
r.get(0).start();
}
public void paintComponent(Graphics g) {
count++;
int a = (int)(Math.random()*255)+1;
int b = (int)(Math.random()*255)+1;
int c = (int)(Math.random()*255)+1;
g.setColor(new Color(a,b,c));
for (int i = 0; i < r.size(); i++) {
g.fillRect(r.get(i).x, r.get(i).y, r.get(i).length, r.get(i).length);
}
if (count % 500 == 0) {
r.add(new RecInfo((int) (random() * 700 + 100), (int) (random() * 700 + 100), 15, 5));
j++;
r.get(j).start();
}
if (count == 5000) {
count = 0; //start over in case count is too big and over flow
}
repaint(); //loop again
}
}