Here is a simple Java code for an analog clock using Java 2D features in Swing.
Please adjust the code according to your needs. This code will create a new JFrame
and continually update it every second with the current time.
package org.kodejava.swing;
import javax.swing.*;
import java.awt.*;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class AnalogClock extends JPanel {
public AnalogClock() {
setPreferredSize(new Dimension(400, 300));
setBackground(Color.WHITE);
new Timer(1000, e -> repaint()).start();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int side = Math.min(getWidth(), getHeight());
int centerX = getWidth() / 2;
int centerY = getHeight() / 2;
GregorianCalendar time = new GregorianCalendar();
int second = time.get(Calendar.SECOND);
int minute = time.get(Calendar.MINUTE);
int hour = time.get(Calendar.HOUR_OF_DAY);
drawHand(g2d, side/2 - 10, second / 60.0, 0.5f, Color.RED);
drawHand(g2d, side/2 - 20, minute / 60.0, 2.0f, Color.BLUE);
drawHand(g2d, side/2 - 40, hour / 12.0, 4.0f, Color.BLACK);
// Draw clock numbers and circle
drawClockFace(g2d, centerX, centerY, side/2 - 40);
}
private void drawHand(Graphics2D g2d, int length, double value, float stroke, Color color) {
double angle = Math.PI * 2 * (value - 0.25);
int endX = (int) (getWidth() / 2 + length * Math.cos(angle));
int endY = (int) (getHeight() / 2 + length * Math.sin(angle));
g2d.setColor(color);
g2d.setStroke(new BasicStroke(stroke));
g2d.drawLine(getWidth() / 2, getHeight() / 2, endX, endY);
}
// Added method to draw the clock face and numbers
private void drawClockFace(Graphics2D g2d, int centerX, int centerY, int radius) {
g2d.setStroke(new BasicStroke(2.0f));
g2d.setColor(Color.BLACK);
g2d.drawOval(centerX - radius, centerY - radius, 2 * radius, 2 * radius);
for (int i = 1; i <= 12; i++) {
double angle = Math.PI * 2 * (i / 12.0 - 0.25);
int dx = centerX + (int) ((radius + 20) * Math.cos(angle));
int dy = centerY + (int) ((radius + 20) * Math.sin(angle));
g2d.drawString(Integer.toString(i), dx, dy);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Analog Clock");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new AnalogClock());
frame.pack();
frame.setVisible(true);
}
}
In this code, each “hand” of the clock is moved by calculating its angle according to the current time. The drawHand()
method takes the length of the hand, the proportion of its rotation (for an hour hand, this would be the current hour divided by 12), the stroke width to draw with, and the color to draw, then calculates the end point of the hand line and draws it from the center of the clock.
The drawClockFace()
, which draws a circle using the drawOval()
method (because a circle is a type of oval), and adds numbers around the edge of the circle using the drawString()
method. In both cases, the position of each element is calculated based on the sine and cosine of the angle that represents each hour on the clock face.
Note: Swing is an old but reliable technology that allows coding of GUI elements, however, it’s no longer actively developed. If you are developing a new project, consider using JavaFX as it is more modern and actively developed.