Analog Clock
Hello friends, we are going to make an Analog Clock in this project. Open your IntelliJ or any other IDE and create a new Project. Now, name this project whatever you likes and go to Main.java file.
Now, write the following code in Main.java file.
Main.java :
import javax.swing.*;
import java.awt.*;
public class Main {
public static int height = 600;
public static int width = 600;
public static void main(String[] args) {
JFrame obj = new JFrame();
Analog analog = new Analog();
obj.setBounds(100,100,width,height);
obj.setTitle("Clock");
obj.setBackground(Color.BLACK);
obj.setResizable(false);
obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
obj.add(analog);
obj.setVisible(true);
}
}
import java.awt.*;
public class Main {
public static int height = 600;
public static int width = 600;
public static void main(String[] args) {
JFrame obj = new JFrame();
Analog analog = new Analog();
obj.setBounds(100,100,width,height);
obj.setTitle("Clock");
obj.setBackground(Color.BLACK);
obj.setResizable(false);
obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
obj.add(analog);
obj.setVisible(true);
}
}
Now, go to Projects tab.
Right Click on src folder, go to New and Click on JAVA Class.
Name the new JAVA Class as Analog.java
Write the following code in Analog.java.
Analog.java :
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
public class Analog extends JPanel {
int x, y;
double angle;
public int interval = 1000;
public Timer timer;
public Analog() {
timer = new Timer(interval, new TimerAction());
timer.start();
}
class TimerAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
repaint();
}
}
public void paint(Graphics g) {
Calendar time = Calendar.getInstance();
int hour = time.get(Calendar.HOUR_OF_DAY);
int minute = time.get(Calendar.MINUTE);
int second = time.get(Calendar.SECOND);
if (hour > 12) {
hour -= 12;
}
g.setColor(Color.WHITE);
g.fillOval(100, 100, 400, 400);
g.setColor(Color.BLACK);
g.drawString("12", 294, 120);
g.drawString("9", 120, 304);
g.drawString("6", 297, 480);
g.drawString("3", 480, 304);{
angle = Math.toRadians((15 - second) * 6);
x = (int)(Math.cos(angle) * 200);
y = (int)(Math.sin(angle) * 200);
g.setColor(Color.RED);
g.drawLine(300, 300, 300 + x, 300 - y);
angle = Math.toRadians((15 - minute) * 6);
x = (int)(Math.cos(angle) * 150);
y = (int)(Math.sin(angle) * 150);
g.setColor(Color.BLUE);
g.drawLine(300, 300, 300 + x, 300 - y);
angle = Math.toRadians((15 - (hour * 5)) * 6);
x = (int)(Math.cos(angle) * 100);
y = (int)(Math.sin(angle) * 100);
g.setColor(Color.BLACK);
g.drawLine(300, 300, 300 + x, 300 - y);
}
}
}
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
public class Analog extends JPanel {
int x, y;
double angle;
public int interval = 1000;
public Timer timer;
public Analog() {
timer = new Timer(interval, new TimerAction());
timer.start();
}
class TimerAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
repaint();
}
}
public void paint(Graphics g) {
Calendar time = Calendar.getInstance();
int hour = time.get(Calendar.HOUR_OF_DAY);
int minute = time.get(Calendar.MINUTE);
int second = time.get(Calendar.SECOND);
if (hour > 12) {
hour -= 12;
}
g.setColor(Color.WHITE);
g.fillOval(100, 100, 400, 400);
g.setColor(Color.BLACK);
g.drawString("12", 294, 120);
g.drawString("9", 120, 304);
g.drawString("6", 297, 480);
g.drawString("3", 480, 304);{
angle = Math.toRadians((15 - second) * 6);
x = (int)(Math.cos(angle) * 200);
y = (int)(Math.sin(angle) * 200);
g.setColor(Color.RED);
g.drawLine(300, 300, 300 + x, 300 - y);
angle = Math.toRadians((15 - minute) * 6);
x = (int)(Math.cos(angle) * 150);
y = (int)(Math.sin(angle) * 150);
g.setColor(Color.BLUE);
g.drawLine(300, 300, 300 + x, 300 - y);
angle = Math.toRadians((15 - (hour * 5)) * 6);
x = (int)(Math.cos(angle) * 100);
y = (int)(Math.sin(angle) * 100);
g.setColor(Color.BLACK);
g.drawLine(300, 300, 300 + x, 300 - y);
}
}
}
Other JAVA projects :
Ball Game using JAVA
Notepad using JAVA
Chat Application using JAVA
Bouncing Ball using JAVA