BreakEndpublic class BreakEnd extends JFrame implements RunnableBreakEnd -
Tool to display current time and time that class starts/resumes.
This started as a quick hack when I saw the Windows
guys with a tool like this, but it has grown somewhat since then.
TODO
- add another Thread to flash the screen if the resume time is passed.
|
Fields Summary |
---|
protected JLabel | nowLabelLabel for the current time. | protected JLabel | endsLabelLabel for when break ends. | protected JSlider | fontSizeJSlider for font sizes | protected Thread | tickerA thread to run the clock ticker | protected Font | fThe font for the large text | protected static final int | FONT_SIZE_MINThe minimum allowable font size | protected static final int | FONT_SIZE_MAXThe maximum sensible font size given current monitors. | protected static final int | FONT_SIZE_DEFAULTDefault point size | protected NumberFormat | formNumberFormat to format a non-localized two-digit number |
Constructors Summary |
---|
public BreakEnd(String s)Constructor: set a font, initialize UI components.
super("Java Breaker: " + s);
setBackground(Color.white);
setForeground(Color.red);
Container cp;
cp = getContentPane();
// cp = this;
cp.setLayout(new BorderLayout());
fontSize = new JSlider(JSlider.HORIZONTAL,
FONT_SIZE_MIN, FONT_SIZE_MAX, FONT_SIZE_DEFAULT);
fontSize.setMajorTickSpacing(10);
fontSize.setMinorTickSpacing(5);
fontSize.setSnapToTicks(true);
fontSize.setPaintTicks(true);
fontSize.setPaintLabels(true);
fontSize.setBorder(BorderFactory.createEmptyBorder(0,0,10,0));
fontSize.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
JSlider source = (JSlider)e.getSource();
if (!source.getValueIsAdjusting()) {
int points = (int)source.getValue();
setFontSize(points);
}
}
});
//add the slider to the content pane
cp.add(BorderLayout.NORTH, fontSize);
// Make a panel for CENTER to hold two labels above each other
JPanel timesPanel = new JPanel();
timesPanel.setLayout(new GridLayout(2,1));
cp.add(BorderLayout.CENTER, timesPanel);
timesPanel.add(
nowLabel = new JLabel("Time now is: 00:00:00", JLabel.CENTER));
String mesg = null;
if (s.startsWith("+")) { // "This will be HARDer..."
Calendar d = new GregorianCalendar();
int newMinutes = d.get(Calendar.MINUTE)+
Integer.parseInt(s.substring(1))+1;
d.set(Calendar.MINUTE, newMinutes);
mesg = "We start at " + toHHMM_String(d);
} else {
mesg = "We start at " + s + " ";
}
timesPanel.add(BorderLayout.CENTER,
endsLabel = new JLabel(mesg, JLabel.CENTER));
setFontSize(FONT_SIZE_DEFAULT);
JButton b;
cp.add(BorderLayout.SOUTH, b = new JButton("Done"));
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
// Pack should be done here so the ComponentAdapter's
// componentResized doesn't fire and shrink the size
pack();
// Start the timer thread now. Tick, tick, tick.
(ticker = new Thread(this)).start();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e) {
Dimension d = getSize();
if (d.height < 100)
setFontSize(15);
else
setFontSize(d.height/6);
}
});
|
Methods Summary |
---|
public static void | main(java.lang.String[] av)Main method to start me up.
for (int i=0; i<av.length; i++) {
// parse options...
}
if (av.length != 1) {
System.err.println("Usage: javaBreak time");
return;
}
BreakEnd b = new BreakEnd(av[0]);
UtilGUI.centre(b);
b.setVisible(true);
| public void | run()
Calendar d /* = new GregorianCalendar() */; // For 1.4
while (ticker.isAlive()) {
//d.setTimeInMillis(System.currentTimeMillis()); // For 1.4
d = new GregorianCalendar();
nowLabel.setText("Time is now " + toHHMM_String(d));
try {
Thread.sleep(1000);
} catch (Exception e) {
return;
}
}
| protected void | setFontSize(int sz)Set the font to the given size
if (sz > FONT_SIZE_MAX)
sz = FONT_SIZE_MAX;
//System.out.println("Setting font size to " + sz);
Font f = new Font("Helvetica", Font.PLAIN, sz);
nowLabel.setFont(f);
endsLabel.setFont(f);
fontSize.setValue(sz);
| private java.lang.String | toHHMM_String(java.util.Calendar d)Get the Calendar into HHMM format
String result = null;
int h = d.get(Calendar.HOUR_OF_DAY);
int m = d.get(Calendar.MINUTE);
int s = d.get(Calendar.SECOND);
try {
result = form.format(h) + ":" +
form.format(m) + ":" +
form.format(s);
// Why not just use a DateFormat for that?
} catch (IllegalArgumentException iae) {
JOptionPane.showMessageDialog(this,
"Formatting Error!" + iae, // message
"Formatting Error!", // titlebar
JOptionPane.ERROR_MESSAGE); // icon
}
return result;
|
|