|
import java.awt.*;
import java.applet.*;
public class AnalogUhr extends Applet {
private static final long serialVersionUID = 1L;
Image img;
Graphics2D grf;
Point M;
public void init() {
setBackground (new Color(255,255,255));
img = createImage (this.getWidth(), this.getHeight());
grf = (Graphics2D) img.getGraphics();
// Mittelpunkt des Ziffernblattes
M = new Point (this.getWidth()/2, this.getHeight()/2);
}
public void paint(Graphics g) {
actualTime time;
int i;
double x, y;
grf.clearRect(0, 0, 2*M.x, 2*M.y);
// Aktualisieren der Uhrzeit
time = new actualTime();
int hrs = time.getHrs();
int min = time.getMin();
int sec = time.getSec();
// Minutenzeiger
drawHand(grf, 50, 50, 50, 0.75, 6*min - 90);
// Stundenzeiger
drawHand(grf, 50, 50, 50, 0.5, 30*hrs - 90 + min/2);
// Stundenmarkierungen
grf.setColor (new Color(0, 0, 128));
for (i=0; i<12; i++) {
x = M.x*xFactor(30*i); y = M.y*yFactor(30*i);
grf.drawLine (
M.x + (int)(0.70*x), M.y + (int)(0.70*y),
M.x + (int)(0.85*x), M.y + (int)(0.85*y));
}
// Minutenmarkierungen
grf.setColor (new Color(0, 0, 128));
for (i=0; i<60; i++) {
x = M.x*xFactor(6*i); y = M.y*yFactor(6*i);
if (i%5 != 0) {
grf.drawLine(
M.x + (int)(0.80*x), M.y + (int)(0.80*y),
M.x + (int)(0.85*x), M.y + (int)(0.85*y));
}
}
// Sekundenzeiger
drawHand(grf, 200, 0, 0, 0.78, 6*sec - 90);
g.drawImage (img, 0, 0, this);
try {Thread.sleep(10);} catch (InterruptedException e) {}
this.repaint();
}
public void drawHand (Graphics2D g2D,
int R, int G, int B,
double k, double angle) {
g2D.setColor(new Color(R,G,B));
g2D.drawLine(M.x, M.y, xE(M.x, k, angle), yE(M.y, k, angle));
}
public int xE (int xm, double k, double angle) {
return xm + (int)(k*xm*xFactor(angle));
}
public int yE (int ym, double k, double angle) {
return ym + (int)(k*ym*yFactor(angle));
}
public double xFactor (double angle) {
return Math.cos(Math.toRadians(angle));
}
public double yFactor (double angle) {
return Math.sin(Math.toRadians(angle));
}
public void update (Graphics g) {
paint(g);
}
}
|