- by x32x01 ||
Create a Digital Clock in Java - Full Code Guide
If you’re learning Java GUI programming or graphics programming, building a digital/analog clock is a great project!
In this guide, we’ll show you how to create a real-time clock using Java Swing and GeoSoft graphics library. You’ll learn how to animate hour, minute, and second hands, dynamically update the clock, and use threading with TimerTask.
What is GClock in Java?
GClock is a custom Java class extending GObject from the GeoSoft graphics library, designed to:
Key Components of the Clock
Clock Hands – Hour, Minute, Second
Timer and Dynamic Updates
Ticker class:
GUI Setup with JFrame
How It Works
Benefits of This Java Clock Project
Tip: You can customize the clock by changing radius, colors, or hand lengths to design your own analog clock.
Full Code Reference
The complete code is provided in the
If you’re learning Java GUI programming or graphics programming, building a digital/analog clock is a great project!
What is GClock in Java?
GClock is a custom Java class extending GObject from the GeoSoft graphics library, designed to:- Display hour, minute, and second hands
- Draw clock face ticks and labels
- Animate the clock in real time
- Support dynamic updates using TimerTask
Key Components of the Clock
- Clock Face:
Java:
GSegment background_ = new GSegment();
backgroundStyle.setBackgroundColor(new Color(122, 136, 161));
backgroundStyle.setForegroundColor(new Color(0,0,0));
background_.setStyle(backgroundStyle);
addSegment(background_); - Draws the main background circle
- Sets foreground and background colors
- Clock Disc:
Java:
GSegment disc_ = new GSegment();
discStyle.setBackgroundColor(new Color(255, 255, 255));
discStyle.setForegroundColor(new Color(255, 255, 255));
disc_.setStyle(discStyle);
addSegment(disc_); - Represents the white inner circle
- Separates clock hands from the background
- Ticks and Labels:
Java:
ticks_ = new GSegment[60];
labels_ = new GSegment[12]; - Ticks: minute and hour marks
- Labels: numbers 1 to 12 for hours
Clock Hands – Hour, Minute, Second
Java:
hourHandle_ = new GSegment();
minuteHandle_ = new GSegment();
secondHandle_ = new GSegment(); - Hour Hand: smaller and shorter
- Minute Hand: longer and thinner
- Second Hand: red color for better visibility
Java:
Matrix4x4 m = new Matrix4x4();
m.rotateZ(hourAngle);
m.translate(x0_, y0_, 0.0);
m.transformXyPoints(geometry);
hourHandle_.setGeometryXy(geometry); - Rotates each hand according to current time
- Updates position every second
Timer and Dynamic Updates
Java:
Timer timer = new Timer(true);
timer.schedule(new Ticker(clock1), 0, 1000); - TimerTask updates the clock every 1 second
- Ensures real-time movement of hour, minute, and second hands
Ticker class:
Java:
private class Ticker extends TimerTask {
private GClock clock_;
public Ticker(GClock clock) { clock_ = clock; }
public void run() { clock_.update(); clock_.refresh(); }
} GUI Setup with JFrame
Java:
public class Demo23 extends JFrame {
public Demo23() {
super("G Graphics Library - Demo 23");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GWindow window = new GWindow(new Color(210, 235, 255));
getContentPane().add(window.getCanvas());
pack();
setSize(new Dimension(500, 500));
setVisible(true);
window.startInteraction(new ZoomInteraction(scene));
}
} - Creates a window with custom background
- Adds GClock to the scene
- Sets size, visibility, and interaction support
How It Works
- Clock geometry is defined in GClock constructor
- Hands, ticks, and labels are drawn on the canvas
- TimerTask updates angles of hands based on current hour, minute, second
- Matrix transformations rotate each hand in real time
- Scene refreshes every second, creating a smooth analog clock animation
Benefits of This Java Clock Project
- Learn Java Swing and custom graphics programming
- Understand matrix transformations for 2D objects
- Practice threading and TimerTasks for real-time updates
- Create interactive and visually appealing clocks for desktop apps
Full Code Reference
The complete code is provided in the Code:
section above. Copy it to your IDE and run it to see a [B]fully functional analog clock[/B] in Java. Experiment with [B]GClock parameters[/B] to create multiple clocks with different sizes and colors! 🕒 Last edited: