Java Analog Clock - Full Source Code Guide

x32x01
  • 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:
  • Display hour, minute, and second hands
  • Draw clock face ticks and labels
  • Animate the clock in real time
  • Support dynamic updates using TimerTask
This makes it perfect for learning Java graphics, geometry transformations, and real-time UI updates.



Key Components of the Clock ⏱️


  1. 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

  1. 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

  1. 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
Geometry is defined using x, y coordinates and transformed with Matrix4x4 for rotation.
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 🔄

  1. Clock geometry is defined in GClock constructor
  2. Hands, ticks, and labels are drawn on the canvas
  3. TimerTask updates angles of hands based on current hour, minute, second
  4. Matrix transformations rotate each hand in real time
  5. 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

💡 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
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:
Related Threads
x32x01
Replies
0
Views
1K
x32x01
x32x01
x32x01
Replies
0
Views
831
x32x01
x32x01
x32x01
Replies
0
Views
876
x32x01
x32x01
x32x01
Replies
0
Views
865
x32x01
x32x01
x32x01
  • x32x01
Replies
0
Views
899
x32x01
x32x01
Register & Login Faster
Forgot your password?
Forum Statistics
Threads
660
Messages
668
Members
67
Latest Member
TraceySet
Back
Top