-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomWalker.java
More file actions
114 lines (101 loc) · 3.63 KB
/
RandomWalker.java
File metadata and controls
114 lines (101 loc) · 3.63 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Line2D;
import java.util.Random;
public class RandomWalker extends JPanel implements ActionListener {
private final static int WIDTH = 640, HEIGHT = 480;
static class Walker {
Walker prev;
float x, y, r;
Color c;
float angle;
Walker() {}
Walker(float x, float y, float r, float angle, Color c) {
this.x = x;
this.y = y;
this.r = r;
this.angle = angle;
this.c = c;
}
Walker(float x, float y, float r, Color c) {
this.x = x;
this.y = y;
this.r = r;
this.c = c;
this.angle = 0;
prev = new Walker();
prev.x = x;
prev.y = y;
prev.r = r;
prev.c = c;
prev.angle = 0;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return new Walker(x, y, r, angle, c);
}
}
static Walker[] ws = new Walker[100];
private Timer tm = new Timer(1, this);
public static void main(String[] args) {
Random rand = new Random();
for (int i = 0; i < ws.length; i++) {
ws[i] = new Walker(rand.nextFloat() * WIDTH, rand.nextFloat() * HEIGHT,
5 + rand.nextFloat() * 5, new Color(Color.HSBtoRGB(rand.nextFloat(),
rand.nextFloat() / 4 + 0.75f, rand.nextFloat() / 4 + 0.75f)));
}
RandomWalker jp = new RandomWalker();
JFrame jf = new JFrame("Random Walker");
jf.setSize(WIDTH, HEIGHT);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
jf.add(jp);
}
@Override
protected void paintComponent(Graphics g) {
// g.setColor(Color.WHITE);
// g.fillRect(0, 0, WIDTH, HEIGHT);
Graphics2D g2 = (Graphics2D) g;
for (Walker w : ws) {
g2.setStroke(new BasicStroke((int) w.r));
g.setColor(w.c);
g.setColor(new Color(w.c.getRed(), w.c.getBlue(), w.c.getGreen(), 5));
g.fillOval((int) w.x, (int) w.y, (int) w.r, (int) w.r);
// g.drawLine((int) w.prev.x, (int) w.prev.y, (int) w.x, (int) w.y);
if (Math.abs(w.prev.x - w.x) < 75 && Math.abs(w.prev.y - w.y) < 75)
g2.draw(new Line2D.Float(w.prev.x, w.prev.y, w.x, w.y));
try {
w.prev = (Walker) w.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
tm.start();
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
for (Walker w : ws) {
Random rand = new Random();
int prob = rand.nextInt(1000);
w.angle = (float) (rand.nextFloat() * Math.PI * 2);
if (prob == 0) {
w.x += (25 + rand.nextInt(50)) * Math.cos(w.angle);
w.y += (25 + rand.nextInt(50)) * Math.sin(w.angle);
// System.out.println(w.prev.x + " " + w.x + " " + w.prev.y + " " + w.y);
} else {
w.x += 2 * Math.cos(w.angle);
w.y += 2 * Math.sin(w.angle);
}
if (!(w.x > 25 && w.x < WIDTH - 25 &&
w.y > 25 && w.y < HEIGHT - 25)) {
if (w.x < 25) w.x = WIDTH - 26;
if (w.x > WIDTH - 25) w.x = 25;
if (w.y < 25) w.y = HEIGHT - 26;
if (w.y > HEIGHT - 25) w.y = 25;
}
}
repaint();
}
}