[66] | 1 | /* |
---|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
---|
| 3 | * contributor license agreements. See the NOTICE file distributed with |
---|
| 4 | * this work for additional information regarding copyright ownership. |
---|
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
---|
| 6 | * (the "License"); you may not use this file except in compliance with |
---|
| 7 | * the License. You may obtain a copy of the License at |
---|
| 8 | * |
---|
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 10 | * |
---|
| 11 | * Unless required by applicable law or agreed to in writing, software |
---|
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 14 | * See the License for the specific language governing permissions and |
---|
| 15 | * limitations under the License. |
---|
| 16 | */ |
---|
| 17 | |
---|
| 18 | import java.util.*; |
---|
| 19 | import java.awt.*; |
---|
| 20 | import java.applet.*; |
---|
| 21 | import java.text.*; |
---|
| 22 | |
---|
| 23 | /** |
---|
| 24 | * Time! |
---|
| 25 | * |
---|
| 26 | * @author Rachel Gollub |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | public class Clock2 extends Applet implements Runnable { |
---|
| 30 | Thread timer; // The thread that displays clock |
---|
| 31 | int lastxs, lastys, lastxm, |
---|
| 32 | lastym, lastxh, lastyh; // Dimensions used to draw hands |
---|
| 33 | SimpleDateFormat formatter; // Formats the date displayed |
---|
| 34 | String lastdate; // String to hold date displayed |
---|
| 35 | Font clockFaceFont; // Font for number display on clock |
---|
| 36 | Date currentDate; // Used to get date to display |
---|
| 37 | Color handColor; // Color of main hands and dial |
---|
| 38 | Color numberColor; // Color of second hand and numbers |
---|
| 39 | |
---|
| 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); // Set clock window size |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | // Plotpoints allows calculation to only cover 45 degrees of the circle, |
---|
| 63 | // and then mirror |
---|
| 64 | 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 | // Circle is just Bresenham's algorithm for a scan converted circle |
---|
| 76 | 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 | // Paint is the main part of the program |
---|
| 99 | 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 | // a= s* pi/2 - pi/2 (to switch 0,0 from 3:00 to 12:00) |
---|
| 128 | // x = r(cos a) + xcenter, y = r(sin a) + ycenter |
---|
| 129 | |
---|
| 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 | // Draw the circle and numbers |
---|
| 138 | |
---|
| 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 | // Erase if necessary, and redraw |
---|
| 149 | |
---|
| 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 | } |
---|