Programming / Computing

Digital Clock Project

 This program demonstrates a graphical Digital Clock Project....................................................    

 

The code for the same is as follows................................................................................ 

 

import java.awt.*;

import java.awt.event.*;

import java.util.*;

 

class Display extends Frame implements Runnable

{

Thread timer;

int h1x;

boolean bl;

int bx,by,bx2,by2;

int h1y,h2y,m1y,m2y;

//boolean white;

int k;

//int x,y,w,h;

Graphics g;

int bsec;

int sec;

int bmin;

int min;

int co;

public Display()

{

super("Clock");

MyWindowAdapter adapter=new MyWindowAdapter(this);

addWindowListener(adapter);

timer= new Thread(this);

timer.start();

bsec=0;

h1x=100;

h1y=h2y=m1y=m2y=280; ///////// 1st digit

bx=0;

by=0;

k=1;

bl=true;

co=0;

bx2=by2=0;

}

public void run()

{

Thread me = Thread.currentThread();

        while (true) 

        {

            try

            {

              //Thread.currentThread()

              

              me.sleep(800);

              

            } catch (InterruptedException e) {}

            Date d=new Date();

min=d.getMinutes();

sec=d.getSeconds();

if(min!=bmin) 

{ repaint();bmin=min;}

if(co%2==0) 

{

bl=!bl;co=1;repaint();//repaint(bx-2,bx-2,20,130);

}

//repaint(200,bx,by,50,300);

co++;

            

        

     }

}

public void paint(Graphics g)

{

Date d=new Date();

//int sec=d.getSeconds();

//if(sec==bsec) return;

//bsec=sec;

int hour=d.getHours();

if(hour>12) hour=hour-12;

int h1,h2,m1,m2;

if(hour>9)

{

h2=hour%10;

h1=hour/10;

}

else 

{

h1=0;

h2=hour;

}

int minutes=d.getMinutes();

if(minutes>9)

{

m2=minutes%10;

m1=minutes/10;

}

else 

{

m1=0;

m2=minutes;

}

//int h1x=100;

//int h1y,h2y,m1y,m2y;

//h1y=h2y=m1y=m2y=280; ///////// 1st digit

//int h1=8;

int h2x=h1x+200;///////// 2nd digit

//int h2=8;

int m1x=h2x+300; //////// 3rd Digit

//int m1=8;

int m2x=m1x+200;

//int m2=8;

// to make the Blinkers

bx=(h2x+80+m1x)/2;

by=h2y+30;

setBackground(Color.pink);

if(bl==true) g.setColor(Color.red);

else g.setColor(Color.pink);

bx2=(h2x+90+m1x)/2;

by2=h2y+180;

g.fillRect(bx,by,30,30);

g.fillRect(bx2,by2,30,30);

/* k=1;

int h=1;

while(h<=999999999)

{

h++;

}

k=k*(-1);

if(k<0) g.setColor(Color.black);

else g.setColor(Color.white);

*/

//blinker();

g.setColor(Color.black);

// to draw 4 digits

drawDigit(h1x,h1y,h1,g);

drawDigit(h2x,h2y,h2,g);

drawDigit(m1x,m1y,m1,g);

drawDigit(m2x,m2y,m2,g);

//g.drawString(" "+code,300,100);

 

}

public void drawDigit(int kx,int ky,int n,Graphics g)

{

//int kx,ky;

this.g=g;

Box b1=new Box();

b1.fillBox(kx,ky,20,100,g);

Box b2=new Box();

b2.fillBox(kx+100,ky,20,100,g);

Box b3=new Box();

b3.fillBox(kx,ky+130,20,100,g);

Box b4=new Box();

b4.fillBox(kx+100,ky+130,20,100,g);

Box b5=new Box();

b5.fillBox(kx+20,ky-25,80,20,g);

Box b6=new Box();

b6.fillBox(kx+20,ky+105,80,20,g);

Box b7=new Box();

b7.fillBox(kx+20,ky+235,80,20,g);

b1.setVisible(false);

b2.setVisible(false);

b3.setVisible(false);

b4.setVisible(false);

b5.setVisible(false);

b6.setVisible(false);

b7.setVisible(false);

//int n=8;

switch(n)

{

case 1: b2.setVisible(true);b4.setVisible(true);break;

case 2:b5.setVisible(true);b2.setVisible(true);b3.setVisible(true);

  b6.setVisible(true);b7.setVisible(true);break;

case 3:b5.setVisible(true);b2.setVisible(true);b4.setVisible(true);

  b6.setVisible(true);b7.setVisible(true);break;

case 4:b1.setVisible(true);b2.setVisible(true);b6.setVisible(true);

  b4.setVisible(true);break;

case 5:b1.setVisible(true);b4.setVisible(true);b5.setVisible(true);

  b6.setVisible(true);b7.setVisible(true);break;

case 6:b1.setVisible(true);b4.setVisible(true);b5.setVisible(true);

  b6.setVisible(true);b7.setVisible(true);b3.setVisible(true);break;     

case 7:b5.setVisible(true);b2.setVisible(true);b4.setVisible(true);break;

case 8:b1.setVisible(true);b4.setVisible(true);b5.setVisible(true);

  b6.setVisible(true);b7.setVisible(true);b2.setVisible(true);

  b3.setVisible(true);break;

case 9:b1.setVisible(true);b4.setVisible(true);b5.setVisible(true);

  b6.setVisible(true);b2.setVisible(true);b7.setVisible(true);break;

case 0:b1.setVisible(true);b4.setVisible(true);b5.setVisible(true);

  b2.setVisible(true);b7.setVisible(true);b3.setVisible(true);break;

}

 

}

}

//   //////////////////////   I was here

class Box 

{

int x,y,w,h;

Graphics g;

public Box()

{

x=y=w=h=10;

}

public void fillBox(int x,int y,int w,int h,Graphics k) 

{

g=k;

this.x=x;this.y=y;this.w=w;this.h=h;

g.fillRect(x,y,w,h);

}

public void setVisible(boolean visible)

{

//visible=false;

if(visible==true)

{

 g.setColor(Color.black);

 g.fillRect(x,y,w,h);

}

else

{

g.setColor(Color.pink);

g.fillRect(x,y,w,h);

}

g.setColor(Color.black);

}

}

class Digit

{

public static void main(String arg[])

© 2010 All rights reserved.

Make a free websiteWebnode