1 36 37 40 41 import java.util.*; 42 import java.awt.*; 43 import java.applet.*; 44 import java.text.*; 45 46 52 public class Clock extends Applet implements Runnable { 53 private volatile Thread timer; private int lastxs, lastys, lastxm, 55 lastym, lastxh, lastyh; private SimpleDateFormat formatter; private String lastdate; private Font clockFaceFont; private Date currentDate; private Color handColor; private Color numberColor; private int xcenter = 80, ycenter = 55; 64 public void init() { 65 int x,y; 66 lastxs = lastys = lastxm = lastym = lastxh = lastyh = 0; 67 formatter = new SimpleDateFormat ("EEE MMM dd hh:mm:ss yyyy", 68 Locale.getDefault()); 69 currentDate = new Date(); 70 lastdate = formatter.format(currentDate); 71 clockFaceFont = new Font("Serif", Font.PLAIN, 14); 72 handColor = Color.blue; 73 numberColor = Color.darkGray; 74 75 try { 76 setBackground(new Color(Integer.parseInt(getParameter("bgcolor"), 77 16))); 78 } catch (NullPointerException e) { 79 } catch (NumberFormatException e) { 80 } 81 try { 82 handColor = new Color(Integer.parseInt(getParameter("fgcolor1"), 83 16)); 84 } catch (NullPointerException e) { 85 } catch (NumberFormatException e) { 86 } 87 try { 88 numberColor = new Color(Integer.parseInt(getParameter("fgcolor2"), 89 16)); 90 } catch (NullPointerException e) { 91 } catch (NumberFormatException e) { 92 } 93 resize(300,300); } 95 96 public void update(Graphics g) { 98 int xh, yh, xm, ym, xs, ys; 99 int s = 0, m = 10, h = 10; 100 String today; 101 102 currentDate = new Date(); 103 104 formatter.applyPattern("s"); 105 try { 106 s = Integer.parseInt(formatter.format(currentDate)); 107 } catch (NumberFormatException n) { 108 s = 0; 109 } 110 formatter.applyPattern("m"); 111 try { 112 m = Integer.parseInt(formatter.format(currentDate)); 113 } catch (NumberFormatException n) { 114 m = 10; 115 } 116 formatter.applyPattern("h"); 117 try { 118 h = Integer.parseInt(formatter.format(currentDate)); 119 } catch (NumberFormatException n) { 120 h = 10; 121 } 122 123 xs = (int) (Math.cos(s * Math.PI / 30 - Math.PI / 2) * 45 + xcenter); 125 ys = (int) (Math.sin(s * Math.PI / 30 - Math.PI / 2) * 45 + ycenter); 126 xm = (int) (Math.cos(m * Math.PI / 30 - Math.PI / 2) * 40 + xcenter); 127 ym = (int) (Math.sin(m * Math.PI / 30 - Math.PI / 2) * 40 + ycenter); 128 xh = (int) (Math.cos((h*30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 30 129 + xcenter); 130 yh = (int) (Math.sin((h*30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 30 131 + ycenter); 132 133 formatter.applyPattern("EEE MMM dd HH:mm:ss yyyy"); 135 today = formatter.format(currentDate); 136 137 g.setFont(clockFaceFont); 138 g.setColor(getBackground()); 140 if (xs != lastxs || ys != lastys) { 141 g.drawLine(xcenter, ycenter, lastxs, lastys); 142 g.drawString(lastdate, 5, 125); 143 } 144 if (xm != lastxm || ym != lastym) { 145 g.drawLine(xcenter, ycenter-1, lastxm, lastym); 146 g.drawLine(xcenter-1, ycenter, lastxm, lastym); 147 } 148 if (xh != lastxh || yh != lastyh) { 149 g.drawLine(xcenter, ycenter-1, lastxh, lastyh); 150 g.drawLine(xcenter-1, ycenter, lastxh, lastyh); 151 } 152 153 g.setColor(numberColor); 155 g.drawString(today, 5, 125); 156 g.drawLine(xcenter, ycenter, xs, ys); 157 g.setColor(handColor); 158 g.drawLine(xcenter, ycenter-1, xm, ym); 159 g.drawLine(xcenter-1, ycenter, xm, ym); 160 g.drawLine(xcenter, ycenter-1, xh, yh); 161 g.drawLine(xcenter-1, ycenter, xh, yh); 162 lastxs = xs; lastys = ys; 163 lastxm = xm; lastym = ym; 164 lastxh = xh; lastyh = yh; 165 lastdate = today; 166 currentDate = null; 167 } 168 169 public void paint(Graphics g) { 170 g.setFont(clockFaceFont); 171 g.setColor(handColor); 173 g.drawArc(xcenter-50, ycenter-50, 100, 100, 0, 360); 174 g.setColor(numberColor); 175 g.drawString("9", xcenter-45, ycenter+3); 176 g.drawString("3", xcenter+40, ycenter+3); 177 g.drawString("12", xcenter-5, ycenter-37); 178 g.drawString("6", xcenter-3, ycenter+45); 179 180 g.setColor(numberColor); 182 g.drawString(lastdate, 5, 125); 183 g.drawLine(xcenter, ycenter, lastxs, lastys); 184 g.setColor(handColor); 185 g.drawLine(xcenter, ycenter-1, lastxm, lastym); 186 g.drawLine(xcenter-1, ycenter, lastxm, lastym); 187 g.drawLine(xcenter, ycenter-1, lastxh, lastyh); 188 g.drawLine(xcenter-1, ycenter, lastxh, lastyh); 189 } 190 191 public void start() { 192 timer = new Thread (this); 193 timer.start(); 194 } 195 196 public void stop() { 197 timer = null; 198 } 199 200 public void run() { 201 Thread me = Thread.currentThread(); 202 while (timer == me) { 203 try { 204 Thread.currentThread().sleep(100); 205 } catch (InterruptedException e) { 206 } 207 repaint(); 208 } 209 } 210 211 public String getAppletInfo() { 212 return "Title: A Clock \n" 213 + "Author: Rachel Gollub, 1995 \n" 214 + "An analog clock."; 215 } 216 217 public String [][] getParameterInfo() { 218 String [][] info = { 219 {"bgcolor", "hexadecimal RGB number", 220 "The background color. Default is the color of your browser."}, 221 {"fgcolor1", "hexadecimal RGB number", 222 "The color of the hands and dial. Default is blue."}, 223 {"fgcolor2", "hexadecimal RGB number", 224 "The color of the second hand and numbers. Default is dark gray."} 225 }; 226 return info; 227 } 228 } 229 | Popular Tags |