204 Applets, Demo 4: "Clock"

Wegen geänderter Sicherheitseinstellungen kann dieses Applet aktuell nicht ausgeführt werden. Man studiere den Quellcode.


Dieses Applet wird im Body des Quelltextes dieses HTML-Dokuments wie folgt aufgerufen:

                
                 .....
                <applet code="Clock.class">
                        <param name="height" value="40">
                        <param name="width"  value="40">
                </applet>
                .....
                
                
Der Quelltext von "Clock.java" (von Ketan ):
                
/* ************************************************************************************************* */
/* Ketan's Clock Applet: Inspired by a digital clock I saw somewhere.
 * Permission to copy freely, only send me an email if you do.
 * Just so I feel good! :)
 */

import java.awt.Graphics;
import java.awt.Color;
import java.util.Date;

/* Class definition */
public class Clock extends java.applet.Applet implements Runnable {

        Thread clockThread;
        int radius;
        int ht, wd;
        String height, width;
        int cx, cy;

        public void start() {
                height = getParameter("height");
                ht = (height == null) ? 40 : Integer.valueOf(height).intValue();
                width = getParameter("width");
                wd = (width == null) ? 40 : Integer.valueOf(width).intValue();
                radius = (ht < wd) ? ht / 2 : wd / 2;
                resize(ht, wd);
                cx = cy = radius;
                if (clockThread == null) {
                        clockThread = new Thread(this, "Clock");
                        clockThread.start();
                }
        }

        public void run() {
                while (clockThread != null) {
                        repaint();
                        try {
                                clockThread.sleep(1000);
                        } catch (InterruptedException e) {
                                System.out.println("Interrupt Exception");
                        }
                }
        }

        public void paint(Graphics g) {
                Date now = new Date();

                // Set background color
                setBackground(Color.gray);

                // Draw the second hand
                g.setColor(Color.blue);
                g.drawLine(cx,cy,
                        cx+(int)(Math.round(0.9*radius*Math.sin(now.getSeconds()*3.14/30.0))),
                        cy-(int)(Math.round(0.9*radius*Math.cos(now.getSeconds()*3.14/30.0))));

                // Draw the minute hand
                g.setColor(Color.red);
                g.drawLine(cx,cy,
                        cx+(int)(Math.round(0.8*radius*Math.sin(now.getMinutes()*3.14/30.0))),
                        cy-(int)(Math.round(0.8*radius*Math.cos(now.getMinutes()*3.14/30.0))));

                // Draw the hour hand
                g.setColor(Color.yellow);
                g.drawLine(cx,cy,
                        cx+(int)(Math.round(radius*0.5*Math.sin(
                                        (now.getHours()%12)*3.14/6.0
                                        + (3.14*now.getMinutes()/360.0)))),
                        cy-(int)(Math.round(radius*0.5*Math.cos(
                                        (now.getHours()%12)*3.14/6.0
                                        + (3.14*now.getMinutes()/360.0)))));
        }

        public void stop() {
                clockThread.stop();
                clockThread = null;
        }
}
/* ************************************************************************************************* */

   
   

Zurück zur Startseite