home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-tomcat-addon-1.4.9-installer.exe / Clock2.java < prev    next >
Encoding:
Java Source  |  2004-08-28  |  7.3 KB  |  213 lines

  1. /*
  2. * Copyright 2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. *     http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16.  
  17. import java.util.*;
  18. import java.awt.*;
  19. import java.applet.*;
  20. import java.text.*;
  21.  
  22. /**
  23.  * Time!
  24.  *
  25.  * @author Rachel Gollub
  26.  */
  27.  
  28. public class Clock2 extends Applet implements Runnable {
  29.     Thread timer;                // The thread that displays clock
  30.     int lastxs, lastys, lastxm,
  31.         lastym, lastxh, lastyh;  // Dimensions used to draw hands 
  32.     SimpleDateFormat formatter;  // Formats the date displayed
  33.     String lastdate;             // String to hold date displayed
  34.     Font clockFaceFont;          // Font for number display on clock
  35.     Date currentDate;            // Used to get date to display
  36.     Color handColor;             // Color of main hands and dial
  37.     Color numberColor;           // Color of second hand and numbers
  38.  
  39.     public void init() {
  40.         int x,y;
  41.         lastxs = lastys = lastxm = lastym = lastxh = lastyh = 0;
  42.         formatter = new SimpleDateFormat ("EEE MMM dd hh:mm:ss yyyy", Locale.getDefault());
  43.         currentDate = new Date();
  44.         lastdate = formatter.format(currentDate);
  45.         clockFaceFont = new Font("Serif", Font.PLAIN, 14);
  46.         handColor = Color.blue;
  47.         numberColor = Color.darkGray;
  48.  
  49.         try {
  50.             setBackground(new Color(Integer.parseInt(getParameter("bgcolor"),16)));
  51.         } catch (Exception E) { }
  52.         try {
  53.             handColor = new Color(Integer.parseInt(getParameter("fgcolor1"),16));
  54.         } catch (Exception E) { }
  55.         try {
  56.             numberColor = new Color(Integer.parseInt(getParameter("fgcolor2"),16));
  57.         } catch (Exception E) { }
  58.         resize(300,300);              // Set clock window size
  59.     }
  60.  
  61.     // Plotpoints allows calculation to only cover 45 degrees of the circle,
  62.     // and then mirror
  63.     public void plotpoints(int x0, int y0, int x, int y, Graphics g) {
  64.         g.drawLine(x0+x,y0+y,x0+x,y0+y);
  65.         g.drawLine(x0+y,y0+x,x0+y,y0+x);
  66.         g.drawLine(x0+y,y0-x,x0+y,y0-x);
  67.         g.drawLine(x0+x,y0-y,x0+x,y0-y);
  68.         g.drawLine(x0-x,y0-y,x0-x,y0-y);
  69.         g.drawLine(x0-y,y0-x,x0-y,y0-x);
  70.         g.drawLine(x0-y,y0+x,x0-y,y0+x);
  71.         g.drawLine(x0-x,y0+y,x0-x,y0+y);
  72.     }
  73.  
  74.     // Circle is just Bresenham's algorithm for a scan converted circle
  75.     public void circle(int x0, int y0, int r, Graphics g) {
  76.         int x,y;
  77.         float d;
  78.         x=0;
  79.         y=r;
  80.         d=5/4-r;
  81.         plotpoints(x0,y0,x,y,g);
  82.  
  83.         while (y>x){
  84.             if (d<0) {
  85.                 d=d+2*x+3;
  86.                 x++;
  87.             }
  88.             else {
  89.                 d=d+2*(x-y)+5;
  90.                 x++;
  91.                 y--;
  92.             }
  93.             plotpoints(x0,y0,x,y,g);
  94.         }
  95.     }
  96.  
  97.     // Paint is the main part of the program
  98.     public void paint(Graphics g) {
  99.         int xh, yh, xm, ym, xs, ys, s = 0, m = 10, h = 10, xcenter, ycenter;
  100.         String today;
  101.  
  102.         currentDate = new Date();
  103.         SimpleDateFormat formatter = new SimpleDateFormat("s",Locale.getDefault());
  104.         try {
  105.             s = Integer.parseInt(formatter.format(currentDate));
  106.         } catch (NumberFormatException n) {
  107.             s = 0;
  108.         }
  109.         formatter.applyPattern("m");
  110.         try {
  111.             m = Integer.parseInt(formatter.format(currentDate));
  112.         } catch (NumberFormatException n) {
  113.             m = 10;
  114.         }    
  115.         formatter.applyPattern("h");
  116.         try {
  117.             h = Integer.parseInt(formatter.format(currentDate));
  118.         } catch (NumberFormatException n) {
  119.             h = 10;
  120.         }
  121.         formatter.applyPattern("EEE MMM dd HH:mm:ss yyyy");
  122.         today = formatter.format(currentDate);
  123.         xcenter=80;
  124.         ycenter=55;
  125.     
  126.     // a= s* pi/2 - pi/2 (to switch 0,0 from 3:00 to 12:00)
  127.     // x = r(cos a) + xcenter, y = r(sin a) + ycenter
  128.     
  129.         xs = (int)(Math.cos(s * 3.14f/30 - 3.14f/2) * 45 + xcenter);
  130.         ys = (int)(Math.sin(s * 3.14f/30 - 3.14f/2) * 45 + ycenter);
  131.         xm = (int)(Math.cos(m * 3.14f/30 - 3.14f/2) * 40 + xcenter);
  132.         ym = (int)(Math.sin(m * 3.14f/30 - 3.14f/2) * 40 + ycenter);
  133.         xh = (int)(Math.cos((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 30 + xcenter);
  134.         yh = (int)(Math.sin((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 30 + ycenter);
  135.     
  136.     // Draw the circle and numbers
  137.     
  138.         g.setFont(clockFaceFont);
  139.         g.setColor(handColor);
  140.         circle(xcenter,ycenter,50,g);
  141.         g.setColor(numberColor);
  142.         g.drawString("9",xcenter-45,ycenter+3); 
  143.         g.drawString("3",xcenter+40,ycenter+3);
  144.         g.drawString("12",xcenter-5,ycenter-37);
  145.         g.drawString("6",xcenter-3,ycenter+45);
  146.  
  147.     // Erase if necessary, and redraw
  148.     
  149.         g.setColor(getBackground());
  150.         if (xs != lastxs || ys != lastys) {
  151.             g.drawLine(xcenter, ycenter, lastxs, lastys);
  152.             g.drawString(lastdate, 5, 125);
  153.         }
  154.         if (xm != lastxm || ym != lastym) {
  155.             g.drawLine(xcenter, ycenter-1, lastxm, lastym);
  156.             g.drawLine(xcenter-1, ycenter, lastxm, lastym); }
  157.         if (xh != lastxh || yh != lastyh) {
  158.             g.drawLine(xcenter, ycenter-1, lastxh, lastyh);
  159.             g.drawLine(xcenter-1, ycenter, lastxh, lastyh); }
  160.         g.setColor(numberColor);
  161.         g.drawString("", 5, 125);
  162.         g.drawString(today, 5, 125);    
  163.         g.drawLine(xcenter, ycenter, xs, ys);
  164.         g.setColor(handColor);
  165.         g.drawLine(xcenter, ycenter-1, xm, ym);
  166.         g.drawLine(xcenter-1, ycenter, xm, ym);
  167.         g.drawLine(xcenter, ycenter-1, xh, yh);
  168.         g.drawLine(xcenter-1, ycenter, xh, yh);
  169.         lastxs=xs; lastys=ys;
  170.         lastxm=xm; lastym=ym;
  171.         lastxh=xh; lastyh=yh;
  172.         lastdate = today;
  173.         currentDate=null;
  174.     }
  175.  
  176.     public void start() {
  177.         timer = new Thread(this);
  178.         timer.start();
  179.     }
  180.  
  181.     public void stop() {
  182.         timer = null;
  183.     }
  184.  
  185.     public void run() {
  186.         Thread me = Thread.currentThread();
  187.         while (timer == me) {
  188.             try {
  189.                 Thread.currentThread().sleep(100);
  190.             } catch (InterruptedException e) {
  191.             }
  192.             repaint();
  193.         }
  194.     }
  195.  
  196.     public void update(Graphics g) {
  197.         paint(g);
  198.     }
  199.  
  200.     public String getAppletInfo() {
  201.         return "Title: A Clock \nAuthor: Rachel Gollub, 1995 \nAn analog clock.";
  202.     }
  203.   
  204.     public String[][] getParameterInfo() {
  205.         String[][] info = {
  206.             {"bgcolor", "hexadecimal RGB number", "The background color. Default is the color of your browser."},
  207.             {"fgcolor1", "hexadecimal RGB number", "The color of the hands and dial. Default is blue."},
  208.             {"fgcolor2", "hexadecimal RGB number", "The color of the seconds hand and numbers. Default is dark gray."}
  209.         };
  210.         return info;
  211.     }
  212. }
  213.