20

Clock

  1. // <applet code="Clock" width="200" height="100"></applet>
  2.  
  3. import java.applet.Applet;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6.  
  7. public class Clock extends Applet implements Runnable, MouseListener {
  8.     private volatile Thread thread = null;
  9.  
  10.     Color fg, bg;
  11.  
  12.     Font font;
  13.  
  14.     java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("HH:mm:ss");
  15.  
  16.     public void init() {
  17.         bg = Color.BLUE;
  18.         fg = Color.YELLOW;
  19.  
  20.         font = new Font("Sans-Serif", Font.BOLD, 36);
  21.  
  22.         setBackground(bg);
  23.  
  24.         addMouseListener(this);
  25.     }
  26.  
  27.     public void start() {
  28.         if (thread == null) {
  29.             thread = new Thread(this, "Clock");
  30.             thread.start();
  31.         }
  32.     }
  33.  
  34.     public void stop() {
  35.         thread = null;
  36.     }
  37.  
  38.     public void run() {
  39.         while (this.thread == Thread.currentThread()) {
  40.             repaint();
  41.             try {
  42.                 Thread.sleep(1000);
  43.             }
  44.             catch (InterruptedException e) {
  45.             }
  46.         }
  47.     }
  48.  
  49.     public void mouseClicked(MouseEvent e) {
  50.         if (thread == null)
  51.             start();
  52.         else
  53.             stop();
  54.     }
  55.  
  56.     public void mouseEntered(MouseEvent e) {}
  57.  
  58.     public void mouseExited(MouseEvent e) {}
  59.  
  60.     public void mousePressed(MouseEvent e) {}
  61.  
  62.     public void mouseReleased(MouseEvent e) {}
  63.  
  64.     public void paint(Graphics g) {
  65.         Dimension size = getSize();
  66.         int width = size.width, height = size.height;
  67.  
  68.         Image offscreen = createImage(width, height);
  69.  
  70.         Graphics2D g2d = (Graphics2D) offscreen.getGraphics();
  71.  
  72.         g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
  73.  
  74.         String s = formatter.format(new java.util.Date());
  75.  
  76.         g2d.setFont(font);
  77.  
  78.         FontMetrics fm = g2d.getFontMetrics();
  79.  
  80.         int x = (width - fm.stringWidth(s)) / 2;
  81.         int y = (height - fm.getHeight()) / 2 + fm.getAscent();
  82.  
  83.         g2d.setColor(fg);
  84.         g2d.drawString(s, x, y);
  85.  
  86.         g.drawImage(offscreen, 0, 0, this);
  87.     }
  88.  
  89.     public void update(Graphics g) {
  90.         paint(g);
  91.     }
  92. }
$ javac Clock.java
$ appletviewer Clock.java

Click in the window to stop the clock. Click again to restart it.

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].