/* Time_served: Display the time since the supplied date. This is my second java program, so it's probably poorly written. Brian Stormont brian@stormyprods.com */ import java.awt.Graphics; import java.awt.Image; import java.awt.Color; import java.awt.Font; import java.awt.FontMetrics; import java.util.Date; public class time_served extends java.applet.Applet implements Runnable { Image OSC = null; Graphics OSC_g = null; Font f = null; FontMetrics fm = null; String s1 = null; Thread killme = null; int i, y,m,d,h,min,s; int strlen; boolean threadSuspended = false; public void init() { Date then; setBackground(Color.white); f = new Font("TimesRoman",Font.PLAIN,12); fm = getFontMetrics(f); s1 = getParameter("date"); if (s1 == null) { s1 = "5/16/88 8:30"; } resize((58+1)*12, 15); OSC = createImage(size().width, size().height); OSC_g = OSC.getGraphics(); OSC_g.setFont(f); then = new Date(s1); y = then.getYear(); m = then.getMonth(); d = then.getDate(); h = then.getHours(); min = then.getMinutes(); s = then.getSeconds(); } public void get_date() { Date now; int c_y, c_m, c_d, c_h, c_min, c_s; int d_y, d_m, d_d, d_h, d_min, d_s; now = new Date(); c_y = now.getYear(); c_m = now.getMonth(); c_d = now.getDate(); c_h = now.getHours(); c_min = now.getMinutes(); c_s = now.getSeconds(); d_y = c_y - y; d_m = c_m - m; d_d = c_d -d; d_h = c_h - h; d_min = c_min - min; d_s = c_s - s; if (d_s < 0) { d_min--; d_s += 60; } if (d_min < 0) { d_h--; d_min += 60; } if (d_h < 0) { d_d--; d_h += 24; } if (d_d < 0) { d_m--; d_d += 31; // TODO: have real count for each month } if (d_m < 0) { d_y--; d_m += 12; } s1 = d_y + " years, " + d_m + " months, " + d_d + " days, " + d_h + " hours, " + d_min + " minutes, " + d_s + " seconds "; } public void update(Graphics g) { paint(g); } public void start() { if(killme == null) { killme = new Thread(this); killme.start(); } } public void stop() { killme = null; } public void run() { while (killme != null) { try {Thread.sleep(5);} catch (InterruptedException e){} get_date(); repaint(); } killme = null; } public void paint(Graphics g) { OSC_g.setColor(Color.white); // fill OSC with white OSC_g.fillRect(0,0,size().width,size().height); OSC_g.setColor(Color.black); // fill OSC with white OSC_g.drawString(s1, 0, 13); g.drawImage(OSC,0,0,this); } }