-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFloatingClock.java
492 lines (404 loc) · 11 KB
/
FloatingClock.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*
Author: Tim Talbot.
Date : 20 March 2017
Feel free to use, modify or otherwise make use of this code in any way you wish,
I just ask that you keep this notice here and pull-request any cool changes back
to me. Thanks!
*/
package FloatingClock;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JMenuItem;
import java.awt.GraphicsEnvironment;
import java.awt.GraphicsDevice;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javax.swing.SwingUtilities;
import javax.swing.plaf.metal.*;
import javax.swing.UIManager;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.GraphicsEnvironment;
import java.awt.GraphicsDevice;
public class FloatingClock
{
boolean debug = true;
int mouseX, mouseY;
String screenName="";
// context menu components
JPopupMenu menu;
JMenuItem settingsMenuItem, exitMenuItem;
boolean isDraggable = false;
FloatingClock clock;
FloatingClockSettings settings;
final String configFileName = "FloatingClock.config";
// declare components--
JFrame frame;
JPanel pane;
JLabel clockText;
Color backgroundColor, foregroundColor;
int distanceValue;
float opacityValue, sizeValue;
public static void main(String... args)
{
try
{
// Set cross-platform Java L&F (also called "Metal")
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}
catch (Exception e)
{
// handle exception
e.printStackTrace();
}
new FloatingClock();
}
// get the system time and apply to the clockText label
public void setTime()
{
DateFormat df = new SimpleDateFormat("HH:mm:ss");
Date dateobj = new Date();
clockText.setText(df.format(dateobj));
}
public FloatingClock()
{
clock = this;
loadSettings();
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
init();
new ClockTick().run();
}
});
}
// initializes swing components and positions frame on users screen
private void init()
{
frame = new JFrame("Floating Clock");
pane = new MainContainer();
clockText = new JLabel("");
// implement drag functionality
frame.addMouseListener(new MouseListener() {
public void mousePressed(MouseEvent e)
{
mouseX = e.getX();
mouseY = e.getY();
}
public void mouseReleased(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mouseClicked(MouseEvent e) { }
});
frame.addMouseMotionListener(new MouseMotionListener() {
public void mouseDragged(MouseEvent e)
{
if(isDraggable && SwingUtilities.isLeftMouseButton(e))
{
frame.setLocation(e.getXOnScreen()-mouseX,e.getYOnScreen()-mouseY);
}
}
public void mouseMoved(MouseEvent e) { }
});
frame.setType(javax.swing.JFrame.Type.UTILITY);
frame.setUndecorated(true);
frame.setAlwaysOnTop(true);
frame.setBackground(new Color(0, 0, 0, 0));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// set clockText details based on settings or defaults
clockText.setForeground(foregroundColor);
clockText.setFont(clockText.getFont().deriveFont(sizeValue));
// prepare context menu
menu = new JPopupMenu();
settingsMenuItem = new JMenuItem("Settings");
exitMenuItem = new JMenuItem("Exit");
menu.add(settingsMenuItem);
menu.add(exitMenuItem);
settingsMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
frame.dispose();
new FloatingClockSettings();
}
});
exitMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
// add components to container and add container to frame
pane.add(clockText);
frame.add(pane);
frame.pack();
// position window to top right of screen
boolean foundScreen = false;
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] screenDevices = ge.getScreenDevices();
GraphicsDevice defaultScreen = ge.getDefaultScreenDevice();
// more than one screen?
if(screenDevices.length > 1)
{
// which screen should i draw to ...
// if no screen name is specified in the settings, draw to default screen
if(screenName.equals(""))
{
screenName = defaultScreen.getIDstring();
}
else
{
// find settings-defined screen and set as default
for (int j = 0; j < screenDevices.length; j++)
{
GraphicsDevice gd = screenDevices[j];
if(gd.getIDstring().equals(screenName))
{
defaultScreen = gd;
foundScreen = true;
break;
}
}
}
}
Rectangle rect = defaultScreen.getDefaultConfiguration().getBounds();
int x = (int) rect.getMaxX() - frame.getWidth();
int y = distanceValue;
frame.setLocation(x, y);
frame.setVisible(true);
frame.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
// if right-click, show the settings frame
if(SwingUtilities.isRightMouseButton(e))
{
menu.show(e.getComponent(), e.getX(), e.getY());
}
} // end mouseClicked
}); // end addMouseListener
} // end initComponents() method
public class MainContainer extends JPanel
{
int x = 200;
int y = 50;
public MainContainer()
{
setOpaque(false);
} //end MainContainer
@Override
public Dimension getPreferredSize()
{
return new Dimension(x, y);
} //end getPreferredSize override
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setComposite(AlphaComposite.SrcOver.derive(opacityValue));
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(backgroundColor);
g2d.fillRoundRect(0, 0, x, y, 0, 0);
g2d.dispose();
} //end paintComponent() override
}
public class ClockTick extends Thread
{
public void run()
{
Timer timer = new Timer();
timer.schedule(new TimerTask()
{
@Override
public void run()
{
clock.setTime();
}
}, 0, 1000);
} // end thread/run
} //end ClockTick
private void loadSettings()
{
Properties prop = new Properties();
InputStream input = null;
try
{
input = new FileInputStream(configFileName);
// load a properties file
prop.load(input);
// get the property value and print it out
try
{
backgroundColor = new Color( Integer.parseInt( prop.getProperty("Background") ), true);
}
catch(NumberFormatException e)
{
if(debug) System.out.println(e.toString());
backgroundColor = Color.BLACK;
System.out.println("Possible Config file corruption. Delete FloatingClock.config to resolve");
}
try
{
foregroundColor = new Color( Integer.parseInt( prop.getProperty("Foreground") ), true);
}
catch(NumberFormatException e)
{
if(debug) System.out.println(e.toString());
foregroundColor = Color.WHITE;
System.out.println("Possible Config file corruption. Delete FloatingClock.config to resolve");
}
try
{
distanceValue = Integer.parseInt( prop.getProperty("Distance") );
}
catch(NumberFormatException e)
{
if(debug) System.out.println(e.toString());
distanceValue = 0;
System.out.println("Possible Config file corruption. Delete FloatingClock.config to resolve");
}
try
{
opacityValue = Float.parseFloat( prop.getProperty("Opacity") ) / 100;
}
catch(NumberFormatException e)
{
if(debug) System.out.println(e.toString());
opacityValue = 0.7f;
System.out.println("Possible Config file corruption. Delete FloatingClock.config to resolve");
}
try
{
sizeValue = Float.parseFloat( prop.getProperty("FontSize") );
}
catch(NumberFormatException e)
{
if(debug) System.out.println(e.toString());
sizeValue = 15.0f;
System.out.println("Possible Config file corruption. Delete FloatingClock.config to resolve");
}
try
{
isDraggable = Boolean.parseBoolean( prop.getProperty("isDraggable") );
}
catch(Exception e)
{
if(debug) System.out.println(e.toString());
isDraggable = false;
System.out.println("Possible Config file corruption. Delete FloatingClock.config to resolve");
}
screenName = prop.getProperty("ScreenName");
//System.out.println("S: "+sizeValue); System.out.println("O: " + opacityValue); System.out.println("D: "+distanceValue);
}
catch (IOException io)
{
if(debug) io.printStackTrace();
// file doesn't exist, save then reload
saveSettings();
loadSettings();
return;
}
finally
{
if (input != null)
{
try
{
input.close();
}
catch (IOException io)
{
if(debug) io.printStackTrace();
}
}
}
} // end loadSettings
// save default settings on first run in case stuff doesn't exist
private void saveSettings()
{
if(debug) System.out.println("saving settings..");
Properties prop = new Properties();
OutputStream output = null;
try
{
output = new FileOutputStream(configFileName);
// set the properties value
Color b = Color.BLACK;
Color w = Color.WHITE;
prop.setProperty("Foreground", Integer.toString( w.getRGB() ) );
prop.setProperty("Background", Integer.toString(b.getRGB() ) );
prop.setProperty("Distance", Integer.toString( 0 ) );
prop.setProperty("Opacity", Integer.toString( 70 ) );
prop.setProperty("FontSize", Integer.toString( 15 ) );
prop.setProperty("isDraggable", Boolean.toString (false) );
prop.setProperty("ScreenName", screenName);
// save properties to project root folder
prop.store(output, null);
if(debug) System.out.println("Settings saved");
}
catch (IOException io)
{
if(debug) io.printStackTrace();
}
finally
{
if (output != null)
{
try
{
output.close();
}
catch (IOException io)
{
if(debug) io.printStackTrace();
}
}
}
} // end saveSettings()
class MenuListener extends MouseAdapter
{
public void MousePressed(MouseEvent e)
{
System.out.println("mouse pressed");
}
public void MouseReleased(MouseEvent e)
{
System.out.println("mouse released");
showMenu(e);
}
private void showMenu(MouseEvent e)
{
if(e.isPopupTrigger())
{
menu.show(e.getComponent(), e.getX(), e.getY());
}
}
}
} // end FloatingClock class