Creating a 2D Analog Clock Application using Java Programming
In this Article, I'll Demonstrate you How to code a 2D Analog Clock in Java with Source Code. As we all know Java is one of the top most programming language in market.
Big Companies, Industries and MNCs uses java as the top-most option. Here today we'll create 2D Analog Clock Application using Java Programming. This is a Basic type of Project a beginner can opt for.
Creativity and Useful Product Making is a Basic Need of Programming.
In this 2D Analog Clock Program you'll need to import java.awt.Color, java.awt.Font, java.awt.Graphics, java.util.Date, javax.swing.JPanel, javax.swing.WindowConstants
So, Let's Get Started:
So, Here's the Output or Layout of an Analog Clock after the Implementation of Code.
Output for the Code |
So, Let's See the Source code for this Analog Clock -
Source Code:
import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.util.Date;import javax.swing.JPanel;import javax.swing.WindowConstants;
public class Clock extends javax.swing.JFrame { public int hour;
public int min; public int sec; ClockDial cd; public Clock() { setSize(510,530); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); cd=new ClockDial(this); getContentPane().add(cd); Date curr=new Date(); String time=curr.toString(); hour=Integer.parseInt(time.substring(11,13)); min=Integer.parseInt(time.substring(14,16)); sec=Integer.parseInt(time.substring(17,19)); ClockEngine.setPriority(ClockEngine.getPriority()+3); ClockEngine.start(); }
public static void main(String args[]) { new Clock().setVisible(true); }Thread ClockEngine=new Thread()
{
int newsec,newmin;
public void run()
{ while(true)
{ newsec=(sec+1)%60;
newmin=(min+(sec+1)/60)%60; hour=(hour+(min+(sec+1)/60)/60)%12; sec=newsec; min=newmin; try {
Thread.sleep(1000);
} catch (InterruptedException ex) {} cd.repaint();
} } }; }
class ClockDial extends JPanel{ Clock parent;
public ClockDial(Clock pt){ setSize(520,530); parent=pt; }
@Override
public void paintComponent(Graphics g) { g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(Color.WHITE); g.fillOval(5, 5,480,480); g.setColor(Color.BLACK); g.fillOval(10, 10,470,470); g.setColor(Color.WHITE); g.fillOval(237,237,15,15); g.setFont(g.getFont().deriveFont(Font.BOLD,32));for(int i=1;i<=12;i++) g.drawString(Integer.toString(i),240-(i/12)*11+(int)(210*Math.sin(i*Math.PI/6)),253-(int)(210*Math.cos(i*Math.PI/6)));double minsecdeg=(double)Math.PI/30;double hrdeg=(double)Math.PI/6;int tx,ty;int xpoints[]=new int[3];int ypoints[]=new int[3];
//second hand
tx=245+(int)(210*Math.sin(parent.sec*minsecdeg));ty=245-(int)(210*Math.cos(parent.sec*minsecdeg));
g.drawLine(245,245,tx,ty); //minute hand
tx=245+(int)(190*Math.sin(parent.min*minsecdeg));ty=245-(int)(190*Math.cos(parent.min*minsecdeg));xpoints[0]=245;xpoints[1]=tx+2;xpoints[2]=tx-2;ypoints[0]=245;ypoints[1]=ty+2;ypoints[2]=ty-2;g.fillPolygon(xpoints, ypoints,3); //hour hand
tx=245+(int)(160*Math.sin(parent.hour*hrdeg+parent.min*Math.PI/360));ty=245-(int)(160*Math.cos(parent.hour*hrdeg+parent.min*Math.PI/360));xpoints[1]=tx+4;xpoints[2]=tx-4;ypoints[1]=ty-4;ypoints[2]=ty+4;g.fillPolygon(xpoints, ypoints, 3); }}
So, this was an article based on creating a 2D analog clock using Java. This was one of the basic type of code writing in Java.
It is not enough for code to work. I'm not a great programmer; I'm just a good programmer with great habits. Truth can only be found in one place: the code. We want to get engineers to think about something else. - Bill Gates
For Any Queries in Written Code, Please Comment Down Below.