Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Java Animated Clock
#1
Hello all this is a Little program i did. Note that it uses no images and is all code.

This should provide you with a good understanding of how to animate things in Java. Also it should give you some intro to basic Action Listiners

I know its really messy and i probably didn't follow any naming standards but hey i did this in 1 hour so give me a break.


Main.java this runs as JFrame
Code:
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
*
* @author Scott Adams
*/
public class Main
{

    /**
     * @param args the command line arguments
     */

    public static void main(String[] args)
    {
        String input;
        JFrame frame = new JFrame();
        frame.setSize(500, 500);
        frame.setTitle("Scott's Clock");
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        boolean AMPM=true;
        input="";
        while (!input.equalsIgnoreCase("AM")&&!input.equalsIgnoreCase("PM"))
            {
                input = JOptionPane.showInputDialog("AM or PM");
                AMPM=input.equalsIgnoreCase("AM");//if clock is AM AMPM=true if PM AMPM=false
            }
        int hour = 0;
        int minute = 60;
        int second = 60;
        String Time="";
        String[] timeArray;
        boolean rerun1=false,rerun2=false,rerun3=false;
        do
            {
                input = JOptionPane.showInputDialog("Enter time in hh:mm:ss");
                Time=input;

                timeArray=Time.split(":");
                if (timeArray.length==3)
                {
                    for (int i=1;i<=12;i++)
                    {
                        if (timeArray[0].equals(""+i))
                        {
                            rerun1=true;
                            hour=Integer.parseInt(timeArray[0]);
                        }
                    }
                    for (int i=0;i<60;i++)
                    {
                        if (timeArray[1].equals(""+i))
                        {
                            rerun2=true;
                            minute=Integer.parseInt(timeArray[1]);
                        }
                        else if(i<10)
                        {
                            if (timeArray[1].equals("0"+i))
                            {
                                rerun2=true;
                                minute=Integer.parseInt(timeArray[1]);
                            }
                        }


                    }
                    for (int i=0;i<60;i++)
                    {
                        if (timeArray[2].equals(""+i))
                        {
                            rerun3=true;
                            second=Integer.parseInt(timeArray[2]);
                        }
                        else if(i<10)
                        {
                            if (timeArray[2].equals("0"+i))
                            {
                                rerun3=true;
                                second=Integer.parseInt(timeArray[2]);

                            }
                        }

                    }
                }
        }while (!rerun1||!rerun2||!rerun3);

        myComponents components = new myComponents(AMPM, hour, minute, second);
        frame.add(components);
        frame.setVisible(true);

    }

}


mainApplet runs as JApplet, not applet
Code:
import javax.swing.JApplet;
import javax.swing.JOptionPane;

/**
*
* @author Scott Adams
*/
public class mainApplet extends JApplet {

    /**
     * Initialization method that will be called after the applet is loaded
     * into the browser.
     */
    public void init() {
         boolean AMPM=true;
        String input="";
        while (!input.equalsIgnoreCase("AM")&&!input.equalsIgnoreCase("PM"))
            {
                input = JOptionPane.showInputDialog("AM or PM");
                AMPM=input.equalsIgnoreCase("AM");//if clock is AM AMPM=true if PM AMPM=false
            }
        int hour = 0;
        int minute = 60;
        int second = 60;
        String Time="";
        String[] timeArray;
        boolean rerun1=false,rerun2=false,rerun3=false;
        do
            {
                input = JOptionPane.showInputDialog("Enter time in hh:mm:ss");
                Time=input;
                
                timeArray=Time.split(":");
                if (timeArray.length==3)
                {
                    for (int i=1;i<=12;i++)
                    {
                        if (timeArray[0].equals(""+i))
                        {
                            rerun1=true;
                            hour=Integer.parseInt(timeArray[0]);
                        }
                    }
                    for (int i=0;i<60;i++)
                    {
                        if (timeArray[1].equals(""+i))
                        {
                            rerun2=true;
                            minute=Integer.parseInt(timeArray[1]);
                        }
                        else if(i<10)
                        {
                            if (timeArray[1].equals("0"+i))
                            {
                                rerun2=true;
                                minute=Integer.parseInt(timeArray[1]);
                            }
                        }
                        

                    }
                    for (int i=0;i<60;i++)
                    {
                        if (timeArray[2].equals(""+i))
                        {
                            rerun3=true;
                            second=Integer.parseInt(timeArray[2]);
                        }
                        else if(i<10)
                        {
                            if (timeArray[2].equals("0"+i))
                            {
                                rerun3=true;
                                second=Integer.parseInt(timeArray[2]);
                                
                            }
                        }
                        
                    }
                }
        }while (!rerun1||!rerun2||!rerun3);
        
        myComponents components = new myComponents(AMPM, hour, minute, second);
        this.add(components);
        // TODO start asynchronous download of heavy resources
    }

    // TODO overwrite start(), stop() and destroy() methods

}


Displays a Digital clock on the analog clock
Code:
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;



/**
*
* @author Scott Adams
*/
public class DigitalClock
{
String AMPM;
String Sminute, Sseconds;
Font f;

    public void go(int radius,int seconds, int minutes,int hours,boolean ampm,Graphics2D g2)
    {
        
        
        


        if (ampm) AMPM="AM";
        else AMPM="PM";

        if (minutes<10)
        {
            Sminute="0"+minutes;
        }
        else
        {
            Sminute=minutes+"";
        }
        if (seconds<10)
        {
            Sseconds="0"+seconds;
        }
        else
        {
            Sseconds=seconds+"";
        }
        f = new Font("hours",Font.BOLD,(radius/10));
        g2.setFont(f);
        g2.setColor(Color.CYAN);
        if (hours==0)
        {
            hours=12;
        }
        g2.drawString(hours+":"+Sminute+":"+Sseconds+" "+AMPM,(radius)-(radius/4),(radius)+(radius/3));

    }
}


Draws the background of the clock Text

Note that this resizes the clock to the size of the window/frame
Code:
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;

import java.awt.geom.Point2D;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Scott Adams
*/
public class clockBackground
{

    public Point2D[][] draw(int radius, Graphics2D g2)
    {
            Ellipse2D clock_body = new Ellipse2D.Double(0,0,radius*2,radius*2);
            

            g2.draw(clock_body);
            g2.setColor(Color.blue);
            g2.fill(clock_body);
            g2.setColor(Color.WHITE);
            
            Point2D[][] returnPoints= new Point2D.Double[3][60];
            double o = 0,a = 0,angle;
            int xString = 0,yString = 0;

            for (int handCount=0; handCount<3; handCount++)
            {
                for (int count=0;count<60;count++)
                {
                    angle=(count)*6;
                    if (handCount==0)
                    {
                        a=Math.round(Math.cos(Math.toRadians(angle))*radius);
                        o=Math.round(Math.sin(Math.toRadians(angle))*radius);
                    }
                    else if (handCount==1)
                    {
                        a=Math.round(Math.cos(Math.toRadians(angle))*((radius*3)/4));
                        o=Math.round(Math.sin(Math.toRadians(angle))*((radius*3)/4));
                    }
                    else if (handCount==2)
                    {
                        a=Math.round(Math.cos(Math.toRadians(angle))*(radius/2));
                        o=Math.round(Math.sin(Math.toRadians(angle))*(radius/2));
                    }
                    
                    returnPoints[handCount][count] = new Point2D.Double(radius+o,radius-a);

                    

                       if ((count%5)==0&&handCount==1)
                       {
                            xString=(int)(radius+o);
                            if (count<30)xString=(int)(radius+o-(o/10));
                            if (count==0||count==30)
                            {
                                xString=(int)(radius-(radius/40));
                            }
                           // xString=(int)(radius+o);
                            yString=(int) (radius-a);
                            if (count>45||count<15)yString=(int) (radius-a+(a/5));
                            if (count==15||count==45)
                            {
                                yString=(int)(radius+(radius/40));
                            }
                            //yString=(int) (radius-a);
                            Font f = new Font("hours",Font.BOLD,(radius/10));
                            g2.setFont(f);
                            if (count/5==0)
                            {
                                g2.drawString("12", xString, yString);
                            }
                            else
                            {
                                g2.drawString(""+(count/5), xString, yString);
                            }
                        }
                       else if(handCount==0)
                       {
                           xString=(int)(radius+o);
                            if (count<=30)xString=(int)(radius+o-(o/30));
                           // xString=(int)(radius+o);
                            yString=(int) (radius-a);
                            if (count>=45||count<=15)yString=(int) (radius-a+(a/30));

                            //yString=(int) (radius-a);
                            Font f = new Font("seconds",Font.ITALIC,radius/30);
                            g2.setFont(f);
                            g2.drawString(""+(count), xString, yString);

                       }

                }
                    
                    
                    

                }
          
            return returnPoints;
    }
}


Draws the minute, second and hour hands
Code:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;



/**
*
* @author Scott Adams
*/
public class clockHands
{
    Point2D center;
    double r;
    BasicStroke stroke;
    public void draw(int radius,Point2D point,Point2D pointm,Point2D pointh,boolean ampm,Graphics2D g2)
    {

        
        stroke = new BasicStroke(radius/50,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
        r= radius * 1.0;//promotes radius to a double
        center = new Point2D.Double(r,r);
        g2.setStroke(stroke);
        Line2D hour_hand= new Line2D.Double(center,pointh);
        g2.setColor(Color.black);
        g2.draw(hour_hand);

        Line2D minute_hand= new Line2D.Double(center,pointm);
        g2.setColor(Color.green);
        g2.draw(minute_hand);

        stroke = new BasicStroke(radius/90,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
        Line2D second_hand= new Line2D.Double(center,point);
        g2.setStroke(stroke);
        g2.setColor(Color.red);
        g2.draw(second_hand);


    }
  
}


Used with JApplet and JFrame as the one and only JComponent object

Code:
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Point2D;
import javax.swing.JComponent;
import javax.swing.Timer;


/**
*
* @author Scott Adams
*/
public class myComponents extends JComponent
{
        public boolean AMPM;
        public int hour, minutes, seconds;
        public int  radius=0;
        clockBackground bg;
        clockHands ckh;
        DigitalClock dclk;
        public Point2D[][] points;
        Timer t;
        taskPerformer listen;
    public myComponents(boolean Pampm,int Phours,int Pmin,int Psec)
    {
        AMPM=Pampm;
        minutes=Pmin;

        hour=(Phours*5);
        if (hour>59)
        {
            hour=0;
        }
        hour=hour+(minutes/12);//sets the hour hand to the correct position
        
        seconds=Psec;
        bg = new clockBackground();
        dclk = new DigitalClock();
        ckh = new clockHands();
        listen = new taskPerformer();
        t=new Timer(1000,listen);
        t.start();
    }


        
     /**
     *
     * @param g is the graphics object that controls certain aspects of the components
     */
    @Override
    public void paintComponent(Graphics g)
    {

        
        Graphics2D g2= (Graphics2D) g;
        
        if (getWidth()>getHeight())
        {
            radius=getHeight()/2;
        }
        else
        {
            radius=getWidth()/2;
        }
        
        
            points = bg.draw(radius, g2);//draws the background of the clock and returns an array of Point2D that has hand Drawing Info
            dclk.go(radius, seconds, minutes, hour/5, AMPM, g2);
            ckh.draw(radius,points[0][seconds],points[1][minutes],points[2][hour],AMPM,g2);
        
        
    }
  
  
   private class taskPerformer implements ActionListener
  {
      public void actionPerformed(ActionEvent evt)
      {
          seconds++;
         if (seconds>59)
         {
            seconds=0;
            minutes++;

                if (minutes>59)
                {
                    minutes=0;
                    
                }
            if ((minutes)%12==0)
            {

                hour++;
            }
                if (hour>59)
                {
                    hour=0;
                    AMPM=!AMPM;
                }

         }
          repaint();
      }

  }
  
}


Hope this helps somebody.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  why java haphazard 8 1,790 12-12-2011, 03:23 AM
Last Post: RDCA
  Need Java help from java expert. Blazin 2 2,102 09-07-2011, 02:43 PM
Last Post: AceInfinity
  Java help php 1 1,047 04-06-2010, 06:41 AM
Last Post: php
  Java Problem for real java programmer Testgamma1 10 5,206 03-14-2010, 09:08 AM
Last Post: uber1337
  How do i get java Mozz 4 1,616 02-13-2010, 12:29 PM
Last Post: Mozz

Forum Jump:


Users browsing this thread: 1 Guest(s)