//Author: ^-^Veerle^-^ import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.Container; import java.applet.Applet; import javax.swing.JApplet; import javax.swing.JPanel; import javax.swing.JLabel; import javax.swing.JButton; import javax.swing.JTextField; import javax.swing.ImageIcon; import javax.swing.JOptionPane; import javax.swing.BorderFactory; import java.util.Calendar; import java.text.DecimalFormat; public class binaryClock extends JApplet implements Runnable { private final String[] strBinaryColumn = {"32", "16", "8", "4", "2", "1"}; private final String[] strRowHeading = {"", "H:", "M:", "S:"}; private final int WIDTH = 200, HEIGHT = 150; private final DecimalFormat decFormat = new DecimalFormat("00"); private Thread clockThread = null; private ImageIcon imgOn = null; private JLabel[][] lblClock; private JTextField txtTime; private JPanel panRoot, panClock, panSouthArea, panTime; private Container frame; public void init () { try { imgOn = dataManager.createImageIcon(this, getParameter("LED"), "LED light"); } catch (Exception ex) { JOptionPane.showMessageDialog(null, "The LED image could not be loaded\n" + ex.getMessage(), "Missing Files", JOptionPane.ERROR_MESSAGE); } panClock = new JPanel(new GridLayout(4, (strBinaryColumn.length + 1), 2, 2)); lblClock = new JLabel[4][(strBinaryColumn.length + 1)]; for (int i = 0; i < 4; i++) { for (int k = 0; k < lblClock[i].length; k++) { if (k == 0) //First column, display column headings (H, M, S) { lblClock[i][k] = new JLabel(strRowHeading[i], JLabel.CENTER); } else { if (i == 0) //First row, display the binary column values (32,16,8,4,2,1) { lblClock[i][k] = new JLabel(strBinaryColumn[(k - 1)], JLabel.CENTER); } else //Normal cells which the LED could appear in { lblClock[i][k] = new JLabel("", JLabel.CENTER); } } panClock.add(lblClock[i][k]); } } txtTime = new JTextField(""); txtTime.setToolTipText("For those who do not understand binary"); txtTime.setEditable(false); panSouthArea = new JPanel(new BorderLayout()); panTime = new JPanel(); panRoot = new JPanel(new BorderLayout()); frame = getContentPane(); frame.add(panRoot, BorderLayout.CENTER); panRoot.add(panClock, BorderLayout.NORTH); panRoot.add(panSouthArea, BorderLayout.SOUTH); panSouthArea.add(panTime, BorderLayout.NORTH); panTime.add(txtTime); //Add some padding to make the clock easier to read panRoot.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2)); //Start the clock thread start(); } //Called every second private void updateClock () { Calendar cal = Calendar.getInstance(); //Gets the current time int[] time = {cal.get(Calendar.HOUR), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND)}; txtTime.setText(decFormat.format(time[0]) + ":" + decFormat.format(time[1]) + ":" + decFormat.format(time[2])); setTime(time); } private void setTime (int[] time) { for (int i = 0; i < time.length; i++) { for (int k = 0; k < strBinaryColumn.length; k++) { //Find all the columns this number go into, e.g. 33 is greater then 32, so the 32 column has a LED if (time[i] >= Integer.parseInt(strBinaryColumn[k])) { //Subtract the column value, e.g. 33 - 32 = 1, so it will loop until //it gets to the 1 column, and add an LED for that column time[i] -= Integer.parseInt(strBinaryColumn[k]); lblClock[(i + 1)][(k + 1)].setIcon(imgOn); } else //All the cells without an LED get reset { lblClock[(i + 1)][(k + 1)].setIcon(null); } } } } public void start () { //If the clock thread is not currently running if (clockThread == null) { //Create the clock thread and start it clockThread = new Thread(this, "clock"); clockThread.start(); } } public void stop () { //If the clock thread is currently running if (clockThread != null) { //Send an interrupt clockThread.interrupt(); clockThread = null; } } public void run () { Thread runningThread = clockThread; //Keeps running until it becomes null (from the stop() method) while (runningThread == clockThread) { updateClock(); try { clockThread.sleep(1000L); //Sleep one second) } catch (Exception ex) { } } } }