1 17 18 import java.util.*; 19 import java.awt.*; 20 import java.applet.*; 21 import java.text.*; 22 23 28 29 public class Clock2 extends Applet implements Runnable { 30 Thread timer; int lastxs, lastys, lastxm, 32 lastym, lastxh, lastyh; SimpleDateFormat formatter; String lastdate; Font clockFaceFont; Date currentDate; Color handColor; Color numberColor; 40 public void init() { 41 int x,y; 42 lastxs = lastys = lastxm = lastym = lastxh = lastyh = 0; 43 formatter = new SimpleDateFormat ("EEE MMM dd hh:mm:ss yyyy", Locale.getDefault()); 44 currentDate = new Date(); 45 lastdate = formatter.format(currentDate); 46 clockFaceFont = new Font("Serif", Font.PLAIN, 14); 47 handColor = Color.blue; 48 numberColor = Color.darkGray; 49 50 try { 51 setBackground(new Color(Integer.parseInt(getParameter("bgcolor"),16))); 52 } catch (Exception E) { } 53 try { 54 handColor = new Color(Integer.parseInt(getParameter("fgcolor1"),16)); 55 } catch (Exception E) { } 56 try { 57 numberColor = new Color(Integer.parseInt(getParameter("fgcolor2"),16)); 58 } catch (Exception E) { } 59 resize(300,300); } 61 62 public void plotpoints(int x0, int y0, int x, int y, Graphics g) { 65 g.drawLine(x0+x,y0+y,x0+x,y0+y); 66 g.drawLine(x0+y,y0+x,x0+y,y0+x); 67 g.drawLine(x0+y,y0-x,x0+y,y0-x); 68 g.drawLine(x0+x,y0-y,x0+x,y0-y); 69 g.drawLine(x0-x,y0-y,x0-x,y0-y); 70 g.drawLine(x0-y,y0-x,x0-y,y0-x); 71 g.drawLine(x0-y,y0+x,x0-y,y0+x); 72 g.drawLine(x0-x,y0+y,x0-x,y0+y); 73 } 74 75 public void circle(int x0, int y0, int r, Graphics g) { 77 int x,y; 78 float d; 79 x=0; 80 y=r; 81 d=5/4-r; 82 plotpoints(x0,y0,x,y,g); 83 84 while (y>x){ 85 if (d<0) { 86 d=d+2*x+3; 87 x++; 88 } 89 else { 90 d=d+2*(x-y)+5; 91 x++; 92 y--; 93 } 94 plotpoints(x0,y0,x,y,g); 95 } 96 } 97 98 public void paint(Graphics g) { 100 int xh, yh, xm, ym, xs, ys, s = 0, m = 10, h = 10, xcenter, ycenter; 101 String today; 102 103 currentDate = new Date(); 104 SimpleDateFormat formatter = new SimpleDateFormat("s",Locale.getDefault()); 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 formatter.applyPattern("EEE MMM dd HH:mm:ss yyyy"); 123 today = formatter.format(currentDate); 124 xcenter=80; 125 ycenter=55; 126 127 130 xs = (int)(Math.cos(s * 3.14f/30 - 3.14f/2) * 45 + xcenter); 131 ys = (int)(Math.sin(s * 3.14f/30 - 3.14f/2) * 45 + ycenter); 132 xm = (int)(Math.cos(m * 3.14f/30 - 3.14f/2) * 40 + xcenter); 133 ym = (int)(Math.sin(m * 3.14f/30 - 3.14f/2) * 40 + ycenter); 134 xh = (int)(Math.cos((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 30 + xcenter); 135 yh = (int)(Math.sin((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 30 + ycenter); 136 137 139 g.setFont(clockFaceFont); 140 g.setColor(handColor); 141 circle(xcenter,ycenter,50,g); 142 g.setColor(numberColor); 143 g.drawString("9",xcenter-45,ycenter+3); 144 g.drawString("3",xcenter+40,ycenter+3); 145 g.drawString("12",xcenter-5,ycenter-37); 146 g.drawString("6",xcenter-3,ycenter+45); 147 148 150 g.setColor(getBackground()); 151 if (xs != lastxs || ys != lastys) { 152 g.drawLine(xcenter, ycenter, lastxs, lastys); 153 g.drawString(lastdate, 5, 125); 154 } 155 if (xm != lastxm || ym != lastym) { 156 g.drawLine(xcenter, ycenter-1, lastxm, lastym); 157 g.drawLine(xcenter-1, ycenter, lastxm, lastym); } 158 if (xh != lastxh || yh != lastyh) { 159 g.drawLine(xcenter, ycenter-1, lastxh, lastyh); 160 g.drawLine(xcenter-1, ycenter, lastxh, lastyh); } 161 g.setColor(numberColor); 162 g.drawString("", 5, 125); 163 g.drawString(today, 5, 125); 164 g.drawLine(xcenter, ycenter, xs, ys); 165 g.setColor(handColor); 166 g.drawLine(xcenter, ycenter-1, xm, ym); 167 g.drawLine(xcenter-1, ycenter, xm, ym); 168 g.drawLine(xcenter, ycenter-1, xh, yh); 169 g.drawLine(xcenter-1, ycenter, xh, yh); 170 lastxs=xs; lastys=ys; 171 lastxm=xm; lastym=ym; 172 lastxh=xh; lastyh=yh; 173 lastdate = today; 174 currentDate=null; 175 } 176 177 public void start() { 178 timer = new Thread (this); 179 timer.start(); 180 } 181 182 public void stop() { 183 timer = null; 184 } 185 186 public void run() { 187 Thread me = Thread.currentThread(); 188 while (timer == me) { 189 try { 190 Thread.currentThread().sleep(100); 191 } catch (InterruptedException e) { 192 } 193 repaint(); 194 } 195 } 196 197 public void update(Graphics g) { 198 paint(g); 199 } 200 201 public String getAppletInfo() { 202 return "Title: A Clock \nAuthor: Rachel Gollub, 1995 \nAn analog clock."; 203 } 204 205 public String [][] getParameterInfo() { 206 String [][] info = { 207 {"bgcolor", "hexadecimal RGB number", "The background color. Default is the color of your browser."}, 208 {"fgcolor1", "hexadecimal RGB number", "The color of the hands and dial. Default is blue."}, 209 {"fgcolor2", "hexadecimal RGB number", "The color of the seconds hand and numbers. Default is dark gray."} 210 }; 211 return info; 212 } 213 } 214 | Popular Tags |