Site Tools


java:codes:analogclock

Analogclock

An analogclock just to see the time analog instead of digital.

import java.awt.*;
import java.util.Date;
 
public class Analogclock extends Frame
{
   //globale Variablen
   int xsec, ysec;
   int xmin, ymin;
   int xstd, ystd;
   int xone, yone;
   int xtwo, ytwo;
   int xfour, yfour;
   int xfive, yfive;
   int xseven, yseven;
   int xeight, yeight;
   int xten, yten;
   int xeleven, yeleven;
 
 
    public void paint (Graphics g)
    {
 
     Graphics2D g2 = (Graphics2D) g;
     ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
 
     g2.translate(150, 170);
 
     g2.setStroke(new BasicStroke(2));                     //Linien Dicke
     g2.drawLine(0,0,xsec,ysec);                           //Sekunden Zeiger
 
     g2.setStroke(new BasicStroke(4));
     g2.drawLine(0,0,xmin,ymin);                           //Minuten Zeiger
 
     g2.setStroke(new BasicStroke(6));
     g2.drawLine(0, 0, xstd, ystd);                        //Stunden Zeiger
 
     g2.setStroke(new BasicStroke(2));
     g.drawOval(-125, -125, 250, 250);                     //Kreis
 
     g2.setStroke(new BasicStroke(2));
     g2.drawLine(xone,yone, xone-2, yone+2);               //Eins
     g2.drawLine(xtwo,ytwo, xtwo-2, ytwo+2);               //Zwei
     g2.drawLine(xfour,yfour, xfour-2, yfour-2);           //Vier
     g2.drawLine(xfive,yfive, xfive-2, yfive-2);           //Fünf
     g2.drawLine(xseven,yseven, xseven+2, yseven-2);       //Sieben
     g2.drawLine(xeight,yeight, xeight+2, yeight-2);       //Acht
     g2.drawLine(xten,yten, xten+2, yten+2);               //Zehn
     g2.drawLine(xeleven,yeleven, xeleven+2, yeleven+2);   //Elf
 
     g2.setStroke(new BasicStroke(4));                     //Linien Dicke
     g.drawLine(0, -115, 0, -124);                         //12
     g.drawLine(0, 115, 0, 124);                           //6
     g.drawLine(-115, 0, -124, 0);                         //9
     g.drawLine(115, 0, 124, 0);                           //3
 
     g.setFont(new Font("Verdana", Font.BOLD, 20));        //Schriftart & Größe
     g.drawString("12", -15, -92 );                        //"12"
     g.drawString("3", 95, 8);                             //"3"
     g.drawString("9", -108, 8);                           //"9"
     g.drawString("6", -6, 110);                           //"6"
 
    }
 
    public static void main (String [] args) throws Exception
    {
      Analogclock b = new Analogclock();
 
      b.setSize(300, 325);
      b.setVisible(true);
      b.setTitle("Uhr");
 
      for(;;)
      {
       Date d = new Date();
       int std = d.getHours() % 12;
       int min = d.getMinutes();
       int sec = d.getSeconds();
 
       //Stundenzeiger
       b.xstd = (int) (90 * Math.cos(Math.toRadians(std * 30 +min/2 - 90)));
       b.ystd = (int) (90 * Math.sin(Math.toRadians(std * 30 +min/2 - 90)));
 
       //Minutenzeiger
       b.xmin = (int) (110 * Math.cos(Math.toRadians(min * 6 - 90)));
       b.ymin = (int) (110 * Math.sin(Math.toRadians(min * 6 - 90)));
 
       //Sekundenzeiger
       b.xsec = (int) (120 * Math.cos(Math.toRadians(sec * 6 - 90)));
       b.ysec = (int) (120 * Math.sin(Math.toRadians(sec * 6 - 90)));
 
 
       b.xone = (int) (125 * Math.cos(Math.toRadians(-60)));          //Eins
       b.yone = (int) (125 * Math.sin(Math.toRadians(-60)));
 
       b.xtwo = (int) (125 * Math.cos(Math.toRadians(-30)));          //Zwei
       b.ytwo = (int) (125 * Math.sin(Math.toRadians(-30)));
 
       b.xfour = (int) (125 * Math.cos(Math.toRadians(30)));          //Vier
       b.yfour = (int) (125 * Math.sin(Math.toRadians(30)));
 
       b.xfive = (int) (125 * Math.cos(Math.toRadians(60)));          //Fünf
       b.yfive = (int) (125 * Math.sin(Math.toRadians(60)));
 
       b.xseven = (int) (125 * Math.cos(Math.toRadians(120)));        //Sieben
       b.yseven = (int) (125 * Math.sin(Math.toRadians(120)));
 
       b.xeight = (int) (125 * Math.cos(Math.toRadians(150)));        //Acht
       b.yeight = (int) (125 * Math.sin(Math.toRadians(150)));
 
       b.xten = (int) (125 * Math.cos(Math.toRadians(-150)));         //Zehn
       b.yten = (int) (125 * Math.sin(Math.toRadians(-150)));
 
       b.xeleven = (int) (125 * Math.cos(Math.toRadians(-120)));      //Elf
       b.yeleven = (int) (125 * Math.sin(Math.toRadians(-120)));
 
       b.repaint();
       Thread.sleep(100);
      }
    }
 }



java/codes/analogclock.txt · Last modified: 2017/10/02 15:13 (external edit)