-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScreenV2.java
More file actions
163 lines (132 loc) · 4.25 KB
/
Copy pathScreenV2.java
File metadata and controls
163 lines (132 loc) · 4.25 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package PP14;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.Timer;
public class ScreenV2 extends JPanel implements ActionListener {
Random rand = new Random();
// Size of image
int wid = 1280, hig = 853;
// Starting number of bubbles
int n = 700;
boolean grow = true;
Map<Integer, Point> plist = new HashMap<Integer, Point>(); // Center of bubbles
Map<Integer, Integer> rlis = new HashMap<Integer, Integer>(); // Radius of bubbles
Map<Integer, Boolean> growing = new HashMap<Integer, Boolean>(); // State of bubbles
Map<Integer, Color> color = new HashMap<Integer, Color>(); // Color of bubbles
private Timer timer;
private int delay = 50;
// image
File file = new File("src/PP14/christmas-elf-4679016_1280.jpg");
private BufferedImage image;
public ScreenV2() {
// load image
try {
image = ImageIO.read(file);
} catch (IOException e) {
e.printStackTrace();
}
// initialize timer
timer = new Timer(delay, this);
timer.start();
// initialize first n bubbles
for (int i = 0; i < n; i++) {
newBubble(i);
}
}
public void paint(Graphics g) {
super.paintComponent(g);
// Loop through all bubble pairs to see if they are touching something
for (int i = 0; i < n; i++) {
// check if the bubble is touch the walls
if (growing.get(i)) {
if (plist.get(i).x + rlis.get(i) / 2 >= wid || plist.get(i).x - rlis.get(i) / 2 <= 0
|| plist.get(i).y + rlis.get(i) / 2 >= hig || plist.get(i).y - rlis.get(i) / 2 <= 0) {
growing.put(i, false);
// Create a new bubble if one is touching the wall
newBubble(n);
}
}
// checks if bubbles are touching each other
for (int j = 0; j < n; j++) {
// indexs are not the same and both are growing
if (i != j && (growing.get(i) || growing.get(j))) {
// The x and y distance from the orgins of the bubbles
double dx = Math.abs((plist.get(i).x) - (plist.get(j).x));
double dy = Math.abs((plist.get(i).y) - (plist.get(j).y));
// The hypotenuse of the x and y distances
double maxDistance2 = (Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2)));
// checks if the sum of the radii is greater than the hypotenuse
if (maxDistance2 < ((rlis.get(i) / 2) + (rlis.get(j) / 2))) {
// stops their growth and grows a new bubble
if (growing.get(i))
growing.put(i, false);
if (growing.get(j))
growing.put(j, false);
newBubble(n);
}
}
}
}
// grows the bubbles
for (int l = 0; l < n; l++) {
if (growing.get(l)) {
rlis.put(l, rlis.get(l) + 2);
}
// deletes bubbles that did not grow (their orgin is inside another bubble)
if (rlis.get(l) == 1) {
newBubble(l);
}
}
// draws the bubbles
for (int i = 0; i < n; i++) {
g.setColor(color.get(i));
g.fillOval((int) (plist.get(i).x - (rlis.get(i) / 2)), (int) (plist.get(i).y - (rlis.get(i) / 2)),
rlis.get(i), rlis.get(i));
}
// Stops loop when all bubbles are done growing
if (!growing.containsValue(true)) {
grow = false;
System.out.println("done");
}
}
// Generates a new bubble
public void newBubble(int index) {
// max number of bubbles
if (n < 10000) {
int x, y;
// Ensures that the new bubble is not already existing
do {
x = rand.nextInt(wid - 1) + 1;
y = rand.nextInt(hig - 1) + 1;
} while (plist.containsValue(new Point(x, y)));
// Initializes the bubble
plist.put(index, new Point(x, y));
rlis.put(index, 1);
growing.put(index, true);
color.put(index, getColor(x, y));
if (index == n)
n++;
}
}
// get the color for the bubble (at the orgin) from the image
public Color getColor(int x, int y) {
int clr = image.getRGB(x, y);
return new Color(clr);
}
@Override
public void actionPerformed(ActionEvent e) {
// timed repaint
repaint();
}
}