Friday, November 04, 2005

Java SimpleAnalogClock

This is an implementation of a simple analog clock using Java. Uses just one file. The code is commented well enough to understand, IMHO.
package name.shabda.clock;

import javax.swing.*;
import java.awt.*;
import java.util.Date;
import java.awt.event.*;
/**
*@author shabda raaj and ashwini kumar
*this is our implementation of a simple analog clock
*the idea is simple:
*we get the system time.Class clock gui extends JLabel
*and overrides is paintComponent method to do its
*custom painting.
**/
public class ClockGui extends JLabel{
int centre=getHeight()/2;
Date date=new Date();//date to use
int radiu=(int)(getHeight()*0.4);//radius of clock(outer circle)
int h=getWidth()/2;//x cord of centre of clock
int k=getHeight()/2;//y cord
Timer t=new Timer(1000,new ActionListener(){
public void actionPerformed(ActionEvent e){
repaint();
}
});//t.start repaints the clock every second
Timer t1=new Timer(1000,new ActionListener(){
public void actionPerformed(ActionEvent e){
check();
}
});//t1.start will check for alarm condition
/**
*this does the painting for the clock
*and is fired every second.
**/
public void paintComponent(final Graphics g){
super.paintComponent(g);
g.setColor(Color.cyan);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(Color.red);
for(double i=Math.PI/6;i<(2*Math.PI+Math.PI/6);i+=Math.PI/720){
int x=(int)(radiu*Math.cos(i)+h)-3;
int y=(int)(radiu*Math.sin(i)+k)-3;
radiu=(int)(getHeight()*0.4);
h=getWidth()/2;
k=getHeight()/2;
g.setColor(Color.green);
g.fillOval(x,y,4,4);
}//this draws the outer circle by drawing some very close
//circles.
for(double i=Math.PI/6;i<(2*Math.PI+Math.PI/6);i+=Math.PI/6){
int x=(int)(radiu*Math.cos(i)+h)-3;
int y=(int)(radiu*Math.sin(i)+k)-3;
radiu=(int)(getHeight()*0.4);
h=getWidth()/2;
k=getHeight()/2;
g.setColor(Color.blue);
g.fillOval(x-5,y-5,10,10);
}//this draws the points showing the hours
for(double i=Math.PI/6;i<(2*Math.PI+Math.PI/6);i+=Math.PI/30){
int x=(int)(radiu*Math.cos(i)+h)-3;
int y=(int)(radiu*Math.sin(i)+k)-3;
radiu=(int)(getHeight()*0.4);
h=getWidth()/2;
k=getHeight()/2;
g.setColor(Color.blue);
g.fillOval(x,y,3,3);
}//this draws the points showing the minutes
Date dd=new Date();
String ss=dd.toString().substring(0,10);
Font font = new Font("SansSerif", Font.BOLD, (getHeight())/16);
g.setColor(Color.red);
g.setFont(font);
//write the day and the date
g.drawString(ss,(int)(getWidth()/1.88),(int)(getHeight()/1.95));
drawSecLine(g);//draw second line
drawMinLine(g);//draw minute line
drawHrsLine(g);//draw hour line
g.setColor(Color.red);
int centreX1=getWidth()/2;
int centreY1=getHeight()/2;
g.fillOval(centreX1-6,centreY1-6,12,12);
}//draw a circle in the centre
public ClockGui(String s){
super(s);
Dimension d=new Dimension(300,300);
setPreferredSize(d);
t.start();
t1.start();
}
public void drawSecLine(Graphics g){//function to draw seconds line
g.setColor(Color.red);
date=new Date();
int sec=date.getSeconds();
double angle=Math.PI/2+sec*Math.PI/30-Math.PI;
int endx=(int)(h+radiu*Math.cos(angle));
int endy=(int)(k+radiu*Math.sin(angle));
g.drawLine(h,k,endx,endy);
}
public void drawMinLine(Graphics g){//function to draw minute line
g.setColor(Color.blue);
date=new Date();
int min=date.getMinutes();
double angle=Math.PI/2+min*Math.PI/30-Math.PI;
int radi=(int)(getHeight()/3);
int endx=(int)(h+radi*Math.cos(angle));
int endy=(int)(k+radi*Math.sin(angle));
g.drawLine(h,k,endx,endy);
g.drawLine(h-2,k,endx-2,endy);
g.drawLine(h+2,k,endx+2,endy);
g.drawLine(h+1,k,endx+1,endy);
g.drawLine(h-1,k,endx-1,endy);
}
public void drawHrsLine(Graphics g){//function to draw hour line
g.setColor(Color.magenta);
date=new Date();
int hrs=date.getHours();
int min=date.getMinutes();
int radi=(int)(getHeight()/4);
double angle=(hrs-3)*Math.PI/6+min*Math.PI/360;
int endx=(int)(h+radi*Math.cos(angle));
int endy=(int)(k+radi*Math.sin(angle));
g.drawLine(h+0,k,endx+0,endy);
g.drawLine(h+3,k,endx+3,endy);
g.drawLine(h-3,k,endx-3,endy);
g.drawLine(h+1,k,endx+1,endy);
g.drawLine(h+2,k,endx+2,endy);
g.drawLine(h-1,k,endx-1,endy);
g.drawLine(h-2,k,endx-2,endy);
}
public void check(){
System.out.println("*");
}
public static void main(String[] args){
JFrame frame=new JFrame("AlarmClock v1.0");
ClockGui gui=new ClockGui("");
frame.getContentPane().add(gui);
frame.pack();
frame.show();
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}

});
}
}

0 Comments:

Post a Comment

<< Home