View Javadoc

1   package com.timjohnstondev.timer;
2   
3   import java.awt.Dimension;
4   import java.awt.Image;
5   import java.awt.Window;
6   import java.awt.event.ActionEvent;
7   import java.awt.event.ActionListener;
8   import java.net.URL;
9   import javax.swing.BoxLayout;
10  import javax.swing.ImageIcon;
11  import javax.swing.JFrame;
12  import javax.swing.SwingUtilities;
13  import javax.swing.Timer;
14  import com.sun.awt.AWTUtilities;
15  
16  public class TimerApplication extends JFrame
17  {
18    private static final String TIMER_TITLE = "   Timer   ";
19    private static Window window;
20    private final CountDownPanel countDownPanel = new CountDownPanel();
21    private final TopPanel topPanel = new TopPanel(countDownPanel);
22    private final Timer displayUpdater = new Timer(1000, new TimerAction());
23  
24    public TimerApplication()
25    {
26      super(TIMER_TITLE);
27      setSize(new Dimension(280, 140));
28      setLocationRelativeTo(null);
29      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
30      setLayout(new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS));
31      setResizable(false);
32  
33      add(topPanel);
34      add(countDownPanel);
35      setAlwaysOnTop(true);
36      setIconImage(getImage());
37    }
38  
39    public static void main(final String[] args)
40    {
41      SwingUtilities.invokeLater(new Runnable()
42      {
43        @Override
44        public void run()
45        {
46          window = new TimerApplication();
47          window.setVisible(true);
48          AWTUtilities.setWindowOpacity(window, 0.7f);
49        }
50      });
51    }
52  
53    public final void startDispayTimer()
54    {
55      displayUpdater.start();
56    }
57  
58    public final void stopDispayTimer()
59    {
60      new TimerAction();
61      displayUpdater.stop();
62    }
63  
64    public class TimerAction implements ActionListener
65    {
66      public final void actionPerformed(final ActionEvent ae)
67      {
68        if (countDownPanel.getRemainingSeconds() == 0)
69        {
70          final int newValue = topPanel.getSliderValue() > 50 ? 0 : 100;
71          topPanel.setSliderValue(newValue);
72        }
73        countDownPanel.updateRemainingTimeDisplay();
74      }
75    }
76  
77    private Image getImage()
78    {
79  
80      final URL imgURL = getClass().getResource("images/T-96x96.png");
81      return new ImageIcon(imgURL).getImage();
82    }
83  }