-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWindow.java
328 lines (268 loc) · 7.48 KB
/
Window.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import java.awt.CardLayout;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.net.URL;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Window extends JFrame implements Runnable, WindowListener
{
/**
* Gets rid of the warning.
*/
private static final long serialVersionUID = 1L;
/**
* Location of the right edge of the screen.
*/
double rightbound = 0;
/**
* Location of the left edge of the screen.
*/
double leftbound = 0;
/**
* Location of the top edge of the screen.
*/
double upbound = 0;
/**
* Location of the bottom edge of the screen.
*/
double downbound = 0;
/**
* Velocity of the window on the X axis.
*/
int XVel = 0;
/**
* Velocity of the window on the Y axis.
*/
int YVel = 0;
/**
* Calculated velocity of the mouse on the X axis.
*/
int MouseVelX = 0;
/**
* Calculated velocity of the mouse on the Y axis.
*/
int MouseVelY = 0;
/**
* True when the program is closing.
*/
boolean shuttingDown = false;
/**
* Position of the mouse one cycle ago.
*/
Point OldMousePos = MouseInfo.getPointerInfo().getLocation();
/**
* Runs the function to update the position each millisecond.
*/
ScheduledExecutorService Controller = Executors.newScheduledThreadPool(1);
/**
* The gif/image displayed on the window.
*/
ImageIcon BugGif;
/**
* The label to display the gif/image.
*/
JLabel Bug = new JLabel(BugGif);
/**
* The panel used to display the labe.
*/
JPanel MainPanel = new JPanel();
/**
* Default and only constructor. Calculates the monitor bounds.
*/
Window()
{
this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
this.addWindowListener(this);
URL url = getClass().getResource("roach.gif");
BugGif = new ImageIcon(url);
BugGif.setImage(BugGif.getImage().getScaledInstance(200, 200, Image.SCALE_DEFAULT));
Bug = new JLabel();
Bug.setIcon(BugGif);
MainPanel.setLayout(new CardLayout());
MainPanel.add(Bug);
this.add(MainPanel);
this.setUndecorated(true);
this.pack();
this.setResizable(false);
this.setVisible(true);
this.setAlwaysOnTop(true);
GraphicsDevice[] Screens = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
for(int i = 0; i < Screens.length; i++)
{
double y = Screens[i].getDefaultConfiguration().getBounds().y;
double x = Screens[i].getDefaultConfiguration().getBounds().x;
if(y >= 0)
{
upbound += y;
}
else
{
downbound -= y;
}
if(x >= 0)
{
rightbound += x;
}
else
{
leftbound -= x;
}
}
this.rightbound += Screens[0].getDisplayMode().getWidth();
this.upbound += Screens[0].getDisplayMode().getHeight();
this.Controller.scheduleAtFixedRate(this, 1, 1, TimeUnit.MILLISECONDS);
}
@Override
/**
* Moves the window based on velocity and RNG.
* Detects collisions with the mouse and monitor borders.
*/
public void run()
{
if(shuttingDown)
{
System.exit(0);
}
int x = this.getLocation().x + ThreadLocalRandom.current().nextInt(-1, 2) + XVel/25;
int y = this.getLocation().y + ThreadLocalRandom.current().nextInt(-1, 2) + YVel/25;
this.setLocation(x, y);
XVel += ThreadLocalRandom.current().nextInt(-1, 2);
YVel += ThreadLocalRandom.current().nextInt(-1, 2);
if(this.getLocation().x >= this.rightbound-this.getSize().width && this.XVel >= 0)
{
this.XVel = (int) (-XVel*0.97);
}
else if(this.getLocation().x <= -leftbound && this.XVel <= 0)
{
this.XVel = (int) (-XVel*0.97);
}
if(this.getLocation().y >= this.upbound-this.getSize().height && this.YVel >= 0)
{
this.YVel = (int) (-YVel*0.97);
}
else if(this.getLocation().y <= -downbound && this.YVel <= 0)
{
this.YVel = (int) (-YVel*0.97);
}
Point MouseLocation = MouseInfo.getPointerInfo().getLocation();
MouseVelX = (int) (MouseVelX/1.05 + (MouseLocation.x - OldMousePos.x));
MouseVelY = (int) (MouseVelY/1.05 + (MouseLocation.y - OldMousePos.y));
x += this.getBounds().width/2;
y += this.getBounds().height/2;
int relX = MouseLocation.x - x;
int relY = MouseLocation.y - y;
if(this.XVel < 0 && relX < 0 && relX > -this.getBounds().width/2 && Math.abs(relY) < this.getBounds().height)
{
this.XVel = (int) (-XVel*0.97)+MouseVelX;
this.YVel += MouseVelY;
}
else if(this.XVel > 0 && relX > 0 && relX < this.getBounds().width/2 && Math.abs(relY) < this.getBounds().height)
{
this.XVel = (int) (-XVel*0.97)+MouseVelX;
this.YVel += MouseVelY;
}
if(this.YVel < 0 && relY < 0 && relY > -this.getBounds().height/2 && Math.abs(relX) < this.getBounds().width)
{
this.YVel = (int) (-YVel*0.97)+MouseVelY;
this.XVel += MouseVelX;
}
else if(this.YVel > 0 && relY > 0 && relY < this.getBounds().height/2 && Math.abs(relX) < this.getBounds().width)
{
this.YVel = (int) (-YVel*0.97)+MouseVelY;
this.XVel += MouseVelX;
}
OldMousePos = MouseLocation;
}
@Override
/**
* Unused implemented method.
*/
public void windowActivated(WindowEvent arg0)
{
// TODO Auto-generated method stub
}
@Override
/**
* Unused implemented method.
*/
public void windowClosed(WindowEvent arg0)
{
// TODO Auto-generated method stub
}
@Override
/**
* Called when the user attempts to close the window.
* Makes it go splat and exits the program.
*/
public void windowClosing(WindowEvent arg0)
{
this.Controller.shutdown();
try
{
AudioInputStream Stream = AudioSystem.getAudioInputStream(getClass().getResource("splat.wav"));
DataLine.Info AudioInfo = new DataLine.Info(Clip.class, Stream.getFormat());
Clip AudioClip = (Clip) AudioSystem.getLine(AudioInfo);
AudioClip.open(Stream);
AudioClip.start();
} catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}
BugGif.setImage(BugGif.getImage().getScaledInstance(500, 75, Image.SCALE_DEFAULT));
Bug.setIcon(BugGif);
this.pack();
this.repaint();
shuttingDown = true;
Controller = Executors.newScheduledThreadPool(1);
Controller.schedule(this, 3000, TimeUnit.MILLISECONDS);
//^^^ This code sucks
}
@Override
/**
* Unused implemented method.
*/
public void windowDeactivated(WindowEvent arg0)
{
// TODO Auto-generated method stub
}
@Override
/**
* Unused implemented method.
*/
public void windowDeiconified(WindowEvent arg0)
{
// TODO Auto-generated method stub
}
@Override
/**
* Unused implemented method.
*/
public void windowIconified(WindowEvent arg0)
{
// TODO Auto-generated method stub
}
@Override
/**
* Unused implemented method.
*/
public void windowOpened(WindowEvent arg0)
{
// TODO Auto-generated method stub
}
}