tag:blogger.com,1999:blog-18646242.post-1131123052809563342005-11-04T07:41:00.000-08:002005-11-04T08:50:52.843-08:00Java SimpleAnalogClockThis is an implementation of a simple analog clock using Java. Uses just one file. The code is commented well enough to understand, IMHO.<br />package name.shabda.clock;<br /><br />import javax.swing.*;<br />import java.awt.*;<br />import java.util.Date;<br />import java.awt.event.*;<br />/**<br /> *@author shabda raaj and ashwini kumar<br /> *this is our implementation of a simple analog clock<br /> *the idea is simple:<br /> *we get the system time.Class clock gui extends JLabel<br /> *and overrides is paintComponent method to do its<br /> *custom painting.<br /> **/<br />public class ClockGui extends JLabel{<br /> int centre=getHeight()/2;<br /> Date date=new Date();//date to use<br /> int radiu=(int)(getHeight()*0.4);//radius of clock(outer circle)<br /> int h=getWidth()/2;//x cord of centre of clock<br /> int k=getHeight()/2;//y cord<br /> Timer t=new Timer(1000,new ActionListener(){<br /> public void actionPerformed(ActionEvent e){<br /> repaint();<br /> }<br /> });//t.start repaints the clock every second<br /> Timer t1=new Timer(1000,new ActionListener(){<br /> public void actionPerformed(ActionEvent e){<br /> check();<br /> }<br /> });//t1.start will check for alarm condition<br /> /**<br /> *this does the painting for the clock<br /> *and is fired every second.<br /> **/<br /> public void paintComponent(final Graphics g){<br /> super.paintComponent(g);<br /> g.setColor(Color.cyan);<br /> g.fillRect(0,0,getWidth(),getHeight());<br /> g.setColor(Color.red);<br /> for(double i=Math.PI/6;i<(2*Math.PI+Math.PI/6);i+=Math.PI/720){<br /> int x=(int)(radiu*Math.cos(i)+h)-3;<br /> int y=(int)(radiu*Math.sin(i)+k)-3;<br /> radiu=(int)(getHeight()*0.4);<br /> h=getWidth()/2;<br /> k=getHeight()/2;<br /> g.setColor(Color.green);<br /> g.fillOval(x,y,4,4);<br /> }//this draws the outer circle by drawing some very close<br /> //circles.<br /> for(double i=Math.PI/6;i<(2*Math.PI+Math.PI/6);i+=Math.PI/6){<br /> int x=(int)(radiu*Math.cos(i)+h)-3;<br /> int y=(int)(radiu*Math.sin(i)+k)-3;<br /> radiu=(int)(getHeight()*0.4);<br /> h=getWidth()/2;<br /> k=getHeight()/2;<br /> g.setColor(Color.blue);<br /> g.fillOval(x-5,y-5,10,10);<br /> }//this draws the points showing the hours<br /> for(double i=Math.PI/6;i<(2*Math.PI+Math.PI/6);i+=Math.PI/30){<br /> int x=(int)(radiu*Math.cos(i)+h)-3;<br /> int y=(int)(radiu*Math.sin(i)+k)-3;<br /> radiu=(int)(getHeight()*0.4);<br /> h=getWidth()/2;<br /> k=getHeight()/2;<br /> g.setColor(Color.blue);<br /> g.fillOval(x,y,3,3);<br /> }//this draws the points showing the minutes<br /> Date dd=new Date();<br /> String ss=dd.toString().substring(0,10);<br /> Font font = new Font("SansSerif", Font.BOLD, (getHeight())/16); <br /> g.setColor(Color.red);<br /> g.setFont(font);<br /> //write the day and the date<br /> g.drawString(ss,(int)(getWidth()/1.88),(int)(getHeight()/1.95));<br /> drawSecLine(g);//draw second line<br /> drawMinLine(g);//draw minute line<br /> drawHrsLine(g);//draw hour line<br /> g.setColor(Color.red);<br /> int centreX1=getWidth()/2;<br /> int centreY1=getHeight()/2;<br /> g.fillOval(centreX1-6,centreY1-6,12,12);<br /> }//draw a circle in the centre<br /> public ClockGui(String s){<br /> super(s);<br /> Dimension d=new Dimension(300,300);<br /> setPreferredSize(d);<br /> t.start();<br /> t1.start();<br /> }<br /> public void drawSecLine(Graphics g){//function to draw seconds line<br /> g.setColor(Color.red);<br /> date=new Date();<br /> int sec=date.getSeconds();<br /> double angle=Math.PI/2+sec*Math.PI/30-Math.PI;<br /> int endx=(int)(h+radiu*Math.cos(angle));<br /> int endy=(int)(k+radiu*Math.sin(angle));<br /> g.drawLine(h,k,endx,endy);<br /> }<br /> public void drawMinLine(Graphics g){//function to draw minute line<br /> g.setColor(Color.blue);<br /> date=new Date();<br /> int min=date.getMinutes();<br /> double angle=Math.PI/2+min*Math.PI/30-Math.PI;<br /> int radi=(int)(getHeight()/3);<br /> int endx=(int)(h+radi*Math.cos(angle));<br /> int endy=(int)(k+radi*Math.sin(angle));<br /> g.drawLine(h,k,endx,endy);<br /> g.drawLine(h-2,k,endx-2,endy);<br /> g.drawLine(h+2,k,endx+2,endy);<br /> g.drawLine(h+1,k,endx+1,endy);<br /> g.drawLine(h-1,k,endx-1,endy);<br /> }<br /> public void drawHrsLine(Graphics g){//function to draw hour line<br /> g.setColor(Color.magenta);<br /> date=new Date();<br /> int hrs=date.getHours();<br /> int min=date.getMinutes();<br /> int radi=(int)(getHeight()/4);<br /> double angle=(hrs-3)*Math.PI/6+min*Math.PI/360;<br /> int endx=(int)(h+radi*Math.cos(angle));<br /> int endy=(int)(k+radi*Math.sin(angle));<br /> g.drawLine(h+0,k,endx+0,endy);<br /> g.drawLine(h+3,k,endx+3,endy);<br /> g.drawLine(h-3,k,endx-3,endy);<br /> g.drawLine(h+1,k,endx+1,endy);<br /> g.drawLine(h+2,k,endx+2,endy);<br /> g.drawLine(h-1,k,endx-1,endy);<br /> g.drawLine(h-2,k,endx-2,endy);<br /> }<br /> public void check(){<br /> System.out.println("*");<br /> }<br /> public static void main(String[] args){<br /> JFrame frame=new JFrame("AlarmClock v1.0");<br /> ClockGui gui=new ClockGui("");<br /> frame.getContentPane().add(gui);<br /> frame.pack();<br /> frame.show();<br /> frame.addWindowListener(new WindowAdapter(){<br /> public void windowClosing(WindowEvent e){<br /> System.exit(0);<br /> }<br /> <br /> });<br /> }<br /> }<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/18646242-113112305280956334?l=bytecodes.blogspot.com'/></div>shabdahttp://www.blogger.com/profile/07961528262493927188noreply@blogger.com0