Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package projectresurrection; import java.util.*; import javax.swing.*; import java.net.*; import org.joda.time.*; import org.apache.commons.net.ntp.NTPUDPClient; import org.apache.commons.net.ntp.TimeInfo; import java.io.*; import sun.audio.*; /** * * @author Onryo */ public class Clock implements Runnable { private Thread t; private final String THREAD_NAME = "clockThread"; private JPanel tab; private int mode; private Boolean alive = true; public static final int BRAIN = 0; public static final int HOME = 1; public static final int TAB = 2; public static final int UPDATE = 0; public static final int NO_UPDATE = 1; private static Queue<String> commands = new PriorityQueue<>(); private int commandCount = 0; public long epoch; private DateTime date; private static DateTimeZone timeZone; private long count; private int update; public Clock(int m, int u) { this(new JPanel(), m, u, "Etc/UTC"); } public Clock(int m, int u, String zone) { this(new JPanel(), m, u, zone); } public Clock(JPanel pan, int m, int u) { this(pan, m, u, "Etc/UTC"); } public Clock(JPanel pan, int m, int u, String zone) { mode = m; update = u; tab = pan; if (zone.equals("")) { timeZone = DateTimeZone.forID("Etc/UTC"); } else { timeZone = DateTimeZone.forID(zone); } date = new DateTime(timeZone); t = new Thread(this, THREAD_NAME); t.start(); } public void changeTimeZone(String zone) { timeZone = DateTimeZone.forID(zone); } public static void addCommand(String command) { commands.add(command); } public DateTime getCurrent() { return date; } public long getEpoch() { return ((Long) (epoch / 1000)) * 1000; } private void updateLbl() { String stamp = String.format("%02d.%02d.%04d %02d:%02d:%02d:%02d", date.getDayOfMonth(), date.getMonthOfYear(), date.getYear(), date.getHourOfDay(), date.getMinuteOfHour(), date.getSecondOfMinute(), date.getMillisOfSecond() / 10); if (mode == BRAIN) { if (count % 100 == 0) { try { InputStream in = new FileInputStream("F:/Java/ProjectResurrection/sounds/heartbeat.wav"); AudioStream audioStream = new AudioStream(in); AudioPlayer.player.start(audioStream); } catch (Exception e) { e.printStackTrace(System.out); } } } else if (mode == HOME) { ((JLabel) ((JPanel) ((JPanel) ((JViewport) ((JScrollPane) tab.getComponent(0)).getComponent(0)) .getComponent(0)).getComponent(0)).getComponent(0)).setText(stamp); } else if (mode == TAB) { ((JLabel) ((JPanel) ((JViewport) ((JScrollPane) tab.getComponent(0)).getComponent(0)).getComponent(0)) .getComponent(0)).setText(stamp); } } public void checkUpdate() { if (!Eve.getPref("time zone").equals(timeZone)) { changeTimeZone((String) Eve.getPref("time zone")); } } private void runCommand() { switch (commands.peek()) { case "kill": if (mode != BRAIN) { alive = false; } break; } } public void run() { count = 0; try { while (alive) { if (update == UPDATE) { checkUpdate(); } if (commands.size() != commandCount) { runCommand(); } epoch = System.currentTimeMillis(); date = new DateTime(epoch, timeZone); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(System.out); } updateLbl(); count++; } } catch (Exception e) { e.printStackTrace(System.out); Thread.currentThread().interrupt(); } } }