Java Clock

Here is the source code for a clock I designed in java. The picture for the clockface should be in the same directory as *.java files during compilation, but your compiler will probably make a copy for the binary output directory also. If not make sure that the file is in the same directory as the compiled file *.class files generated or the picture will not appear. The picture file is called clockfacegraphic.png and the program will use it to display the clockface. You can download it here… clockface1transsmallembs

I designed and created the clockface picture myself in photoshop in about a half an hour today (November 19th) and liked it so much that I wanted to share the whole code with you. You can make any clockface you’d like for you own clock or use mine.

If you do not have a picture available you can remove the three lines after “//experiment” and restore the lines which draw the circle and the numbers for the clock face which are marked for that purpose There is also a single optional line to show a digital clock instead or along with the regular clock. I haven’t had much time to test the clock, but one thing I dislike is that the hands move from one position to the next in whole numbers. That means that the hour hand looks like it’s broken until the hour changes. This is easily overcome by rewriting the code which determines the angle of rotation to include the seconds in the minute equation and the minute equation in the hour equation. I decided not to do that however because I wanted to keep the program as simple as possible using as few methods and classes as possible avoiding long lines as much as possible. The side effect however is not undesirable. The clock is easier to read since the hour hand point exactly to the hours until the hour changes. I hope you enjoy the code. I will be making many more clocks. Some will include celestial events and constellations.

The method I used to derive the current time is deprecated, meaning that it is not the currently recommended method for obtaining time in java, but it was simple and it works so I preferred it over other methods.

P.S. I had to add a 1 second thread sleeper to the loop to relieve the processor on Windows Xp, but in linux the program worked fine without it. Also if you paste this file from here into your notepad or eclipse compiler there is a good chance that double quotes ” will paste as two single quotes ‘ ‘ instead. So you’ll have to check for them. The compiler will not compile unless you fix that.

P.P.S. I have rewritten the code on November 21st 2014 to clean it up. Before I had three separate graphical controllers for each hand and this was not necessary. The program will run more efficiently now. I also broke the code up into proper class files. Each class file is placed here in different colors so that it is easy to distinguish between them. To compile separate the text into three files named,, and compile Note: at 12:29 PM I changed the code so that only one object of the type Date is needed to get the time from rather than three.

The code begins on the next line… enjoy.

//this text should be in

import javax.swing.*;

public class CleanYourClock extends JFrame{

    public static void main(String[] args){
        MyJFrame myJFrame = new MyJFrame();
        while (true){
            try {
            } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            }//ends the true block

//this text should be in

import java.awt.BorderLayout;

import javax.swing.*;

public class MyJFrame extends JFrame{{
this.setSize(240, 260);
this.setTitle(“Clock Face”);
this.add(new PaintSurface(), BorderLayout.CENTER);

import java.awt.*;
import java.awt.geom.*;
import java.sql.*;
import javax.swing.*;
import java.util.Date;

public class PaintSurface extends Component {

Shape longarm = new Line2D.Float(0,0, 0, -90);
Shape mediumarm = new Line2D.Float(0,0,0,-75);
Shape shortarm = new Line2D.Float(0,0,0,-50);

public void paint(Graphics g){
Graphics2D graphicscontroller = (Graphics2D)g;
graphicscontroller.setStroke(new BasicStroke(2));
graphicscontroller.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

int halfsurfacesize = this.getSize().width / 2;
graphicscontroller.translate(halfsurfacesize, halfsurfacesize);

//this block draws a circle and numbers if a graphic is not used for the clockface
//remove the comment marks to enable it.
Shape mycircle = new Ellipse2D.Float(-100,-100,200,200);
clockfacegraphic.drawString(“9″, -95, 0);
clockfacegraphic.drawString(“6″, 0, 95);
clockfacegraphic.drawString(“12″, 0, -90);
clockfacegraphic.drawString(“1″, 40, -70);
clockfacegraphic.drawString(“2″, 70, -40);
clockfacegraphic.drawString(“3″, 90, 0);
clockfacegraphic.drawString(“4″, 70, 40);
clockfacegraphic.drawString(“5″, 40, 70);*/

/*shows a digital clock on top of the normal one. Useful for debugging.
clockfacegraphic.drawString(new Date().toString(), -100,-30);//shows a digital clock

//remove the three lines below to disable the picture of the clock.
ImageIcon myIcon = new ImageIcon(“clockfacegraphic.png”);
Image myImage = myIcon.getImage();
g.drawImage(myImage, -100, -100, 200,200, this);

AffineTransform resettransform = graphicscontroller.getTransform();//stores the current transformation to use for resets.
Date currenttime = new Date();//an object from the class Date to get the time from

//draws the second hand
double getsecs = currenttime.getSeconds();
graphicscontroller.rotate((Math.toRadians(getsecs * 6)));

double getmins = currenttime.getMinutes();
graphicscontroller.rotate((Math.toRadians(getmins * 6)));

double gethours = currenttime.getHours();
graphicscontroller.rotate((Math.toRadians(gethours * 30)));



Leave a Reply