home *** CD-ROM | disk | FTP | other *** search
/ com!online 2002 April / comcd0402.iso / homepage / javaspecial / 07_01 / Uhr.java < prev    next >
Encoding:
Java Source  |  1999-08-19  |  10.2 KB  |  373 lines

  1. /***************************************************************************
  2. *                                                                          *
  3. *        Uhr                                                               *
  4. *                                                                          *
  5. *        Author: Michael Kraus                                             *
  6. *        Date: 1997/1998/1999                                              *
  7. *        Version: 1.3                                                      *
  8. *        JDK Version: 1.0.2                                                *
  9. *                                                                          *
  10. *        Please contact me via email or visit the homepage of Uhr:         *
  11. *        michael.kraus@informatik.uni-muenchen.de                          *
  12. *        www.informatik.uni-muenchen.de/~michael.kraus/uhr.html            *
  13. *                                                                          *
  14. *        <!--                                                              *
  15. *        Use this applet like this:                                        *
  16. *        <APPLET code="Uhr.class" width=200 height=200>                    *
  17. *        <PARAM name="timezone" value="120">                               *
  18. *        </APPLET>                                                         *
  19. *        -->                                                               *
  20. *                                                                          *
  21. ***************************************************************************/
  22.  
  23. import java.applet.*;
  24. import java.awt.*;
  25. import java.lang.*;
  26. import java.util.*;
  27.  
  28. public class Uhr extends Applet implements Runnable
  29. {
  30.     Thread thread;
  31.     ParameterUtilities parameterUtilities;
  32.  
  33.     Polygon lastsecondpolygon;
  34.     Polygon lastminutepolygon;
  35.     Polygon lasthourpolygon;
  36.  
  37.     int center_x;
  38.     int center_y;
  39.  
  40.     int timezoneoffset;
  41.  
  42.     private static final int LOCALTIMEZONEOFFSET=new Date().getTimezoneOffset();
  43.  
  44.     Color hourcolor;
  45.     Color minutecolor;
  46.     Color secondcolor;
  47.  
  48.     int hourhand;
  49.     int minutehand;
  50.     int secondhand;
  51.  
  52.     int none=0;
  53.     int slide=1;
  54.     int bound=2;
  55.     int off=0;
  56.     int on=1;
  57.  
  58.     String handvalues[]=
  59.     {
  60.         "none","slide","bound"
  61.     };
  62.  
  63.     String secondhandvalues[]=
  64.     {
  65.         "off","on"
  66.     };
  67.  
  68.     Color fifteenminutecolor;
  69.     Color fiveminutecolor;
  70.     Color oneminutecolor;
  71.  
  72.     int fifteenminute;
  73.     int fiveminute;
  74.     int oneminute;
  75.  
  76.     int diamonds=1;
  77.     int lines=1;
  78.     int numbers=2;
  79.  
  80.     String minutevalues[]=
  81.     {
  82.         "none","diamonds","numbers"
  83.     };
  84.  
  85.     String oneminutevalues[]=
  86.     {
  87.         "none","lines","numbers"
  88.     };
  89.  
  90.     Color dialcolor;
  91.     Color dialedgecolor;
  92.  
  93.     int dial;
  94.     int withedge=1;
  95.     int withoutedge=2;
  96.  
  97.     String dialvalues[]=
  98.     {
  99.         "none","with edge","without edge"
  100.     };
  101.  
  102.     private static final int UNDEFINED=0xffff;
  103.  
  104.     public void init()
  105.     {
  106.         Color backgroundcolor;
  107.  
  108.         thread=null;
  109.         parameterUtilities=new ParameterUtilities(this);
  110.  
  111.         lastsecondpolygon=new Polygon();
  112.         lastminutepolygon=new Polygon();
  113.         lasthourpolygon=new Polygon();
  114.  
  115.         timezoneoffset=parameterUtilities.getIntegerParameter("timezone",UNDEFINED);
  116.  
  117.         secondhand=parameterUtilities.getStringArrayParameter("second hand",secondhandvalues,on);
  118.         minutehand=parameterUtilities.getStringArrayParameter("minute hand",handvalues,bound);
  119.         hourhand=parameterUtilities.getStringArrayParameter("hour hand",handvalues,slide);
  120.  
  121.         oneminute=parameterUtilities.getStringArrayParameter("1 minute",oneminutevalues,lines);
  122.         fiveminute=parameterUtilities.getStringArrayParameter("5 minute",minutevalues,diamonds);
  123.         fifteenminute=parameterUtilities.getStringArrayParameter("15 minute",minutevalues,diamonds);
  124.  
  125.         dial=parameterUtilities.getStringArrayParameter("dial",dialvalues,withedge);
  126.  
  127.         dialcolor=parameterUtilities.getColorParameter("dial color",Color.lightGray);
  128.         dialedgecolor=parameterUtilities.getColorParameter("dial edge color",Color.darkGray);
  129.  
  130.         if((backgroundcolor=parameterUtilities.getColorParameter("background color",null))!=null)
  131.             setBackground(backgroundcolor);
  132.  
  133.         secondcolor=parameterUtilities.getColorParameter("second hand color",Color.red);
  134.         minutecolor=parameterUtilities.getColorParameter("minute hand color",hourcolor);
  135.         hourcolor=parameterUtilities.getColorParameter("hour hand color",Color.black);
  136.  
  137.         oneminutecolor=parameterUtilities.getColorParameter("1 minute color",Color.darkGray);
  138.         fiveminutecolor=parameterUtilities.getColorParameter("5 minute color",Color.white);
  139.         fifteenminutecolor=parameterUtilities.getColorParameter("15 minute color",Color.black);
  140.     }
  141.  
  142.     Polygon getDiamond(int seconds,double angle,double forwards,double backwards,double sideways)
  143.     {
  144.         Polygon polygon=new Polygon();
  145.  
  146.         double leftsin=Math.sin((seconds-angle)*Math.PI/30);
  147.         double leftcos=Math.cos((seconds-angle)*Math.PI/30);
  148.         double middlesin=Math.sin(seconds*Math.PI/30);
  149.         double middlecos=Math.cos(seconds*Math.PI/30);
  150.         double rightsin=Math.sin((seconds+angle)*Math.PI/30);
  151.         double rightcos=Math.cos((seconds+angle)*Math.PI/30);
  152.  
  153.         polygon.addPoint(center_x+(int)(center_x*forwards*middlesin),center_y-(int)(center_y*forwards*middlecos));
  154.         polygon.addPoint(center_x+(int)(center_x*sideways*leftsin),center_y-(int)(center_y*sideways*leftcos));
  155.         polygon.addPoint(center_x+(int)(center_x*backwards*middlesin),center_y-(int)(center_y*backwards*middlecos));
  156.         polygon.addPoint(center_x+(int)(center_x*sideways*rightsin),center_y-(int)(center_y*sideways*rightcos));
  157.  
  158.         return polygon;
  159.     }
  160.  
  161.     void drawNumber(Graphics graphics,int seconds,int number,double size,double radius)
  162.     {
  163.         Font font=new Font("TimesRoman",Font.PLAIN,(int)(Math.min(center_x,center_y)*size));
  164.         FontMetrics fontmetrics=getFontMetrics(font);
  165.  
  166.         String string=(new Integer(number)).toString();
  167.  
  168.         int x=center_x+(int)(center_x*radius*Math.sin(seconds*Math.PI/30));
  169.         int y=center_y-(int)(center_y*radius*Math.cos(seconds*Math.PI/30));
  170.  
  171.         x-=fontmetrics.stringWidth(string)/2;
  172.         y+=fontmetrics.getAscent()/2;
  173.  
  174.         graphics.setFont(font);
  175.         graphics.drawString(string,x,y);
  176.     }
  177.  
  178.     public void paint(Graphics graphics)
  179.     {
  180.         int n;
  181.         Dimension dimension=size();
  182.  
  183.         center_x=dimension.width/2;
  184.         center_y=dimension.height/2;
  185.  
  186.         lastsecondpolygon=new Polygon();
  187.         lastminutepolygon=new Polygon();
  188.         lasthourpolygon=new Polygon();
  189.  
  190.         if(dial==withedge)
  191.         {
  192.             graphics.setColor(dialedgecolor);
  193.             graphics.fillOval(0,0,dimension.width,dimension.height);
  194.  
  195.             graphics.setColor(dialcolor);
  196.             graphics.fillOval((int)(dimension.width*.02),(int)(dimension.height*.02),(int)(dimension.width*.96),(int)(dimension.height*.96));
  197.         }        
  198.         else if(dial==withoutedge)
  199.         {
  200.             graphics.setColor(dialcolor);
  201.             graphics.fillOval(0,0,dimension.width,dimension.height);
  202.         }
  203.  
  204.         for(n=0;n<60;n++)
  205.         {
  206.             if(n%15==0&&fifteenminute==diamonds)
  207.             {
  208.                 graphics.setColor(fifteenminutecolor);
  209.                 graphics.fillPolygon(getDiamond(n,.4,1,.8,.9));
  210.             }
  211.             else if(n%15==0&&fifteenminute==numbers)
  212.             {
  213.                 graphics.setColor(fifteenminutecolor);
  214.                 drawNumber(graphics,n,n==0?12:n/5,.17,.9);
  215.             }
  216.             else if(n%5==0&&fiveminute==diamonds)
  217.             {
  218.                 graphics.setColor(fiveminutecolor);
  219.                 graphics.fillPolygon(getDiamond(n,.2,.95,.85,.9));
  220.             }
  221.             else if(n%5==0&&fiveminute==numbers)
  222.             {
  223.                 graphics.setColor(fiveminutecolor);
  224.                 drawNumber(graphics,n,n==0?12:n/5,.1,.9);
  225.             }
  226.             else if(oneminute==lines)
  227.             {
  228.                 double sin=Math.sin(n*Math.PI/30);
  229.                 double cos=Math.cos(n*Math.PI/30);
  230.  
  231.                 graphics.setColor(oneminutecolor);
  232.                 graphics.drawLine(center_x+(int)(center_x*.88*sin),center_y-(int)(center_y*.88*cos),center_x+(int)(center_x*.92*sin),center_y-(int)(center_y*.92*cos));
  233.             }
  234.             else if(oneminute==numbers)
  235.             {
  236.                 graphics.setColor(oneminutecolor);
  237.                 drawNumber(graphics,n,n,.05,.9);
  238.             }
  239.         }
  240.     }
  241.  
  242.     Polygon getHand(double seconds,double forwards,double backwards,double sideways)
  243.     {
  244.         Polygon polygon=new Polygon();
  245.  
  246.         double sin=Math.sin(seconds*Math.PI/30);
  247.         double cos=Math.cos(seconds*Math.PI/30);
  248.  
  249.         polygon.addPoint(center_x+(int)(center_x*forwards*sin),center_y-(int)(center_y*forwards*cos));
  250.         polygon.addPoint(center_x+(int)(center_x*sideways*cos),center_y+(int)(center_y*sideways*sin));
  251.         polygon.addPoint(center_x-(int)(center_x*backwards*sin),center_y+(int)(center_y*backwards*cos));
  252.         polygon.addPoint(center_x-(int)(center_x*sideways*cos),center_y-(int)(center_y*sideways*sin));
  253.  
  254.         return polygon;
  255.     }
  256.  
  257.     void drawHand(Graphics graphics,Polygon polygon)
  258.     {
  259.         graphics.fillPolygon(polygon);
  260.         graphics.drawLine(polygon.xpoints[0],polygon.ypoints[0],polygon.xpoints[2],polygon.ypoints[2]);
  261.     }
  262.  
  263.     public void update(Graphics graphics)
  264.     {
  265.         Polygon hourpolygon=new Polygon();
  266.         Polygon minutepolygon=new Polygon();
  267.         Polygon secondpolygon=new Polygon();
  268.  
  269.         double hours=0;
  270.         double minutes;
  271.         double seconds;
  272.         long time;
  273.  
  274.         Date date=new Date();
  275.  
  276.         if(timezoneoffset!=UNDEFINED)
  277.         {
  278.             time=date.getTime();
  279.             time+=LOCALTIMEZONEOFFSET*60*1000;
  280.             time+=timezoneoffset*60*1000;
  281.             date.setTime(time);
  282.         }
  283.  
  284.         seconds=date.getSeconds();
  285.  
  286.         if(minutehand==slide)
  287.             minutes=date.getMinutes()+seconds/60;
  288.         else
  289.             minutes=date.getMinutes();
  290.  
  291.         if(hourhand==slide)
  292.             hours=date.getHours()+minutes/60;
  293.         if(hourhand==bound)
  294.             hours=date.getHours();
  295.  
  296.         if(hourhand!=none)
  297.             hourpolygon=getHand(hours*5,.4,.1,.05);
  298.         if(minutehand!=none)
  299.             minutepolygon=getHand(minutes,.7,.2,.04);
  300.         if(secondhand==on)
  301.             secondpolygon=getHand(seconds,.8,.3,.03);
  302.  
  303.         if(dial!=none)
  304.             graphics.setColor(dialcolor);
  305.         else
  306.             graphics.setColor(getBackground());
  307.  
  308.         if(hourhand!=none)
  309.             drawHand(graphics,lasthourpolygon);
  310.         if(minutehand!=none)
  311.             drawHand(graphics,lastminutepolygon);
  312.         if(secondhand==on)
  313.             drawHand(graphics,lastsecondpolygon);
  314.  
  315.         if(hourhand!=none)
  316.         {
  317.             graphics.setColor(hourcolor);
  318.             drawHand(graphics,hourpolygon);
  319.         }
  320.  
  321.         if(minutehand!=none)
  322.         {
  323.             graphics.setColor(minutecolor);
  324.             drawHand(graphics,minutepolygon);
  325.         }
  326.  
  327.         if(secondhand==on)
  328.         {
  329.             graphics.setColor(secondcolor);
  330.             drawHand(graphics,secondpolygon);
  331.         }
  332.  
  333.         lasthourpolygon=hourpolygon;
  334.         lastminutepolygon=minutepolygon;
  335.         lastsecondpolygon=secondpolygon;
  336.     }
  337.  
  338.     public void run()
  339.     {
  340.         while(true)
  341.         {
  342.             repaint();
  343.  
  344.             try
  345.             {
  346.                 thread.sleep(1000-System.currentTimeMillis()%1000);
  347.             }
  348.             catch(InterruptedException exception)
  349.             {
  350.             }
  351.         }
  352.     }
  353.  
  354.     public void start()
  355.     {
  356.         if(thread==null)
  357.         {
  358.             thread=new Thread(this);
  359.             thread.start();
  360.         }
  361.     }
  362.  
  363.     public void stop()
  364.     {
  365.         if(thread!=null)
  366.         {
  367.             thread.stop();
  368.             thread=null;
  369.         }
  370.     }
  371. }
  372.  
  373.