Designeruhr



Und das ist der ganze Quelltext (auch zum download):

/*
 *
 * Copyright (c) 1996 - 1998
 * Frank Buss (fb@frank-buss.de), Stephan Schloepke (stephan@nordrhein.net)
 *
 * This applet was published in the book:
 *
 *     "Programmier Training Java, In 15 Stunden topfit"
 *     ISBN 3-8158-1303-4
 *     Authors: Frank Buss, Stephan Schloepke
 *     Data Becker Verlage (http://www.databecker.de)
 *
 * You can get this applet and more at http://www.frank-buss.de
 *
 */

//
// eine Uhr in Java
//

import java.applet.*;
import java.awt.*;
import java.util.*;

class fPoint {
    public double x;
    public double y;
    public fPoint(double x,double y) {
        this.x=x;
        this.y=y;
    }
}

public class Uhr extends Applet implements Runnable {
    Thread Animation;
    private Image Buffer;
    private Graphics gBuffer;
    private static int mitte=72;
    private int altSek;
    private Image uhrBild;

    public void start() {
        if (Animation==null) {
            Animation = new Thread (this);
            Animation.start();
        }
    }
    public void stop() {
        if (Animation != null) {
            Animation.stop();
            Animation = null;
        }
    }
    public void run() {
        while (true) {
            try {
                Animation.sleep (50);
            } catch (Exception e) {}
            repaint();
        }
    }
    public void update(Graphics g) {
        paint(g);
    }

    public void init() {
        uhrBild=getImage(getDocumentBase(),"Uhr.gif");
        MediaTracker tracker=new MediaTracker(this);
        tracker.addImage(uhrBild,0);
        try {
            tracker.waitForID(0);
        } catch (InterruptedException e) {}
        Buffer=createImage(uhrBild.getWidth(this),uhrBild.getHeight(this));
        gBuffer=Buffer.getGraphics();
        altSek=-1;
    }

    public void paint(Graphics g) {
        g.drawImage(Buffer,0,0,this);
        Date d=new Date();
        int sek=d.getSeconds();
        if (sek!=altSek) {
            altSek=sek;
            int min=d.getMinutes();
            int stu=d.getHours();
            gBuffer.drawImage(uhrBild,0,0,this);
            gBuffer.setColor(Color.black);
            drawZeiger(stu*5+min/12,37,2,4);
            drawZeiger(min,60,2,4);
            drawZeiger(sek,60,1.1,10);
        }
    }

    private fPoint holeVektor(int winkel, double laenge) {
        winkel=winkel+30;
        return new fPoint(Math.sin(Math.PI/30*winkel)*laenge,Math.cos(Math.PI/30*winkel)*laenge);
    }
    private int runden(double x) {
        return (int)Math.round(x);
    }
    private void drawZeiger(int winkel,double laenge,double breite,double ueber) {
        fPoint e=holeVektor(winkel,laenge);
        fPoint a=holeVektor(winkel+30,ueber);
        fPoint r=holeVektor(winkel+15,breite);
        Polygon z=new Polygon();
        z.addPoint(runden(a.x+r.x+mitte),runden(a.y+r.y+mitte));
        z.addPoint(runden(a.x-r.x+mitte),runden(a.y-r.y+mitte));
        z.addPoint(runden(e.x-r.x+mitte),runden(e.y-r.y+mitte));
        z.addPoint(runden(e.x+r.x+mitte),runden(e.y+r.y+mitte));
        gBuffer.fillPolygon(z);
    }
}


11. November 1999, Frank Buß