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.class (.txt) < prev    next >
Encoding:
Java Class File  |  2004-08-28  |  5.6 KB  |  196 lines

  1. import java.applet.Applet;
  2. import java.awt.Color;
  3. import java.awt.Font;
  4. import java.awt.Graphics;
  5. import java.text.SimpleDateFormat;
  6. import java.util.Date;
  7. import java.util.Locale;
  8.  
  9. public class Clock2 extends Applet implements Runnable {
  10.    Thread timer;
  11.    int lastxs;
  12.    int lastys;
  13.    int lastxm;
  14.    int lastym;
  15.    int lastxh;
  16.    int lastyh;
  17.    SimpleDateFormat formatter;
  18.    String lastdate;
  19.    Font clockFaceFont;
  20.    Date currentDate;
  21.    Color handColor;
  22.    Color numberColor;
  23.  
  24.    public void init() {
  25.       this.lastxs = this.lastys = this.lastxm = this.lastym = this.lastxh = this.lastyh = 0;
  26.       this.formatter = new SimpleDateFormat("EEE MMM dd hh:mm:ss yyyy", Locale.getDefault());
  27.       this.currentDate = new Date();
  28.       this.lastdate = this.formatter.format(this.currentDate);
  29.       this.clockFaceFont = new Font("Serif", 0, 14);
  30.       this.handColor = Color.blue;
  31.       this.numberColor = Color.darkGray;
  32.  
  33.       try {
  34.          this.setBackground(new Color(Integer.parseInt(this.getParameter("bgcolor"), 16)));
  35.       } catch (Exception var6) {
  36.       }
  37.  
  38.       try {
  39.          this.handColor = new Color(Integer.parseInt(this.getParameter("fgcolor1"), 16));
  40.       } catch (Exception var5) {
  41.       }
  42.  
  43.       try {
  44.          this.numberColor = new Color(Integer.parseInt(this.getParameter("fgcolor2"), 16));
  45.       } catch (Exception var4) {
  46.       }
  47.  
  48.       this.resize(300, 300);
  49.    }
  50.  
  51.    public void plotpoints(int x0, int y0, int x, int y, Graphics g) {
  52.       g.drawLine(x0 + x, y0 + y, x0 + x, y0 + y);
  53.       g.drawLine(x0 + y, y0 + x, x0 + y, y0 + x);
  54.       g.drawLine(x0 + y, y0 - x, x0 + y, y0 - x);
  55.       g.drawLine(x0 + x, y0 - y, x0 + x, y0 - y);
  56.       g.drawLine(x0 - x, y0 - y, x0 - x, y0 - y);
  57.       g.drawLine(x0 - y, y0 - x, x0 - y, y0 - x);
  58.       g.drawLine(x0 - y, y0 + x, x0 - y, y0 + x);
  59.       g.drawLine(x0 - x, y0 + y, x0 - x, y0 + y);
  60.    }
  61.  
  62.    public void circle(int x0, int y0, int r, Graphics g) {
  63.       int x = 0;
  64.       int y = r;
  65.       float d = (float)(1 - r);
  66.       this.plotpoints(x0, y0, x, r, g);
  67.  
  68.       for(; y > x; this.plotpoints(x0, y0, x, y, g)) {
  69.          if (d < 0.0F) {
  70.             d = d + (float)(2 * x) + 3.0F;
  71.             ++x;
  72.          } else {
  73.             d = d + (float)(2 * (x - y)) + 5.0F;
  74.             ++x;
  75.             --y;
  76.          }
  77.       }
  78.  
  79.    }
  80.  
  81.    public void paint(Graphics g) {
  82.       int s = 0;
  83.       int m = 10;
  84.       int h = 10;
  85.       this.currentDate = new Date();
  86.       SimpleDateFormat formatter = new SimpleDateFormat("s", Locale.getDefault());
  87.  
  88.       try {
  89.          s = Integer.parseInt(formatter.format(this.currentDate));
  90.       } catch (NumberFormatException var18) {
  91.          s = 0;
  92.       }
  93.  
  94.       formatter.applyPattern("m");
  95.  
  96.       try {
  97.          m = Integer.parseInt(formatter.format(this.currentDate));
  98.       } catch (NumberFormatException var17) {
  99.          m = 10;
  100.       }
  101.  
  102.       formatter.applyPattern("h");
  103.  
  104.       try {
  105.          h = Integer.parseInt(formatter.format(this.currentDate));
  106.       } catch (NumberFormatException var16) {
  107.          h = 10;
  108.       }
  109.  
  110.       formatter.applyPattern("EEE MMM dd HH:mm:ss yyyy");
  111.       String today = formatter.format(this.currentDate);
  112.       int xcenter = 80;
  113.       int ycenter = 55;
  114.       int xs = (int)(Math.cos((double)((float)s * 3.14F / 30.0F - 1.57F)) * (double)45.0F + (double)xcenter);
  115.       int ys = (int)(Math.sin((double)((float)s * 3.14F / 30.0F - 1.57F)) * (double)45.0F + (double)ycenter);
  116.       int xm = (int)(Math.cos((double)((float)m * 3.14F / 30.0F - 1.57F)) * (double)40.0F + (double)xcenter);
  117.       int ym = (int)(Math.sin((double)((float)m * 3.14F / 30.0F - 1.57F)) * (double)40.0F + (double)ycenter);
  118.       int xh = (int)(Math.cos((double)((float)(h * 30 + m / 2) * 3.14F / 180.0F - 1.57F)) * (double)30.0F + (double)xcenter);
  119.       int yh = (int)(Math.sin((double)((float)(h * 30 + m / 2) * 3.14F / 180.0F - 1.57F)) * (double)30.0F + (double)ycenter);
  120.       g.setFont(this.clockFaceFont);
  121.       g.setColor(this.handColor);
  122.       this.circle(xcenter, ycenter, 50, g);
  123.       g.setColor(this.numberColor);
  124.       g.drawString("9", xcenter - 45, ycenter + 3);
  125.       g.drawString("3", xcenter + 40, ycenter + 3);
  126.       g.drawString("12", xcenter - 5, ycenter - 37);
  127.       g.drawString("6", xcenter - 3, ycenter + 45);
  128.       g.setColor(this.getBackground());
  129.       if (xs != this.lastxs || ys != this.lastys) {
  130.          g.drawLine(xcenter, ycenter, this.lastxs, this.lastys);
  131.          g.drawString(this.lastdate, 5, 125);
  132.       }
  133.  
  134.       if (xm != this.lastxm || ym != this.lastym) {
  135.          g.drawLine(xcenter, ycenter - 1, this.lastxm, this.lastym);
  136.          g.drawLine(xcenter - 1, ycenter, this.lastxm, this.lastym);
  137.       }
  138.  
  139.       if (xh != this.lastxh || yh != this.lastyh) {
  140.          g.drawLine(xcenter, ycenter - 1, this.lastxh, this.lastyh);
  141.          g.drawLine(xcenter - 1, ycenter, this.lastxh, this.lastyh);
  142.       }
  143.  
  144.       g.setColor(this.numberColor);
  145.       g.drawString("", 5, 125);
  146.       g.drawString(today, 5, 125);
  147.       g.drawLine(xcenter, ycenter, xs, ys);
  148.       g.setColor(this.handColor);
  149.       g.drawLine(xcenter, ycenter - 1, xm, ym);
  150.       g.drawLine(xcenter - 1, ycenter, xm, ym);
  151.       g.drawLine(xcenter, ycenter - 1, xh, yh);
  152.       g.drawLine(xcenter - 1, ycenter, xh, yh);
  153.       this.lastxs = xs;
  154.       this.lastys = ys;
  155.       this.lastxm = xm;
  156.       this.lastym = ym;
  157.       this.lastxh = xh;
  158.       this.lastyh = yh;
  159.       this.lastdate = today;
  160.       this.currentDate = null;
  161.    }
  162.  
  163.    public void start() {
  164.       this.timer = new Thread(this);
  165.       this.timer.start();
  166.    }
  167.  
  168.    public void stop() {
  169.       this.timer = null;
  170.    }
  171.  
  172.    public void run() {
  173.       for(Thread me = Thread.currentThread(); this.timer == me; this.repaint()) {
  174.          try {
  175.             Thread.currentThread();
  176.             Thread.sleep(100L);
  177.          } catch (InterruptedException var3) {
  178.          }
  179.       }
  180.  
  181.    }
  182.  
  183.    public void update(Graphics g) {
  184.       this.paint(g);
  185.    }
  186.  
  187.    public String getAppletInfo() {
  188.       return "Title: A Clock \nAuthor: Rachel Gollub, 1995 \nAn analog clock.";
  189.    }
  190.  
  191.    public String[][] getParameterInfo() {
  192.       String[][] info = new String[][]{{"bgcolor", "hexadecimal RGB number", "The background color. Default is the color of your browser."}, {"fgcolor1", "hexadecimal RGB number", "The color of the hands and dial. Default is blue."}, {"fgcolor2", "hexadecimal RGB number", "The color of the seconds hand and numbers. Default is dark gray."}};
  193.       return info;
  194.    }
  195. }
  196.