In addition to using the Runnable method, we can also use another method to create a Thread. The method is Timer, which is located in the javax.swing.Timer class. The advantage of this method is that we do not need to implement it into the main class and the process is not disturbed by other processes.
The constructor of Timer is:
- Timer(int delay, ActionListener listener)
- delay is the waiting time used by the Thread process. The delay value is in milliseconds.
- listener is an ActionListener that will be used to perform the desired process.
To run the Timer, the method used is start(), while to stop it, use the stop() method.
In making this digital clock, an important thing to note is that if the value of time (hours, minutes, or seconds) is less than 10, then the number displayed on the screen is only 1 digit. This can cause changes to the position of the text. To overcome this, if the time value is less than 10, then the value is added 1 digit in front of it in the form of the number 0 (zero), so that the time value displayed on the screen is always two digits.
The complete program listing for the example of making a digital clock is:
import javax.swing.*;
import java.awt.event.*;
import java.util.Date;
import java.awt.*;
public class jamDigital
extends JFrame {
// Deklarasi komponen Label
JLabel tampil_jam = new JLabel();
// Konstruktor
public jamDigital() {
tampil_jam.setFont(new Font("Dialog", 1, 30));
this.getContentPane().setLayout(new FlowLayout());
this.getContentPane().add(tampil_jam);
// ActionListener untuk Keperluan Timer
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
String nol_jam = "";
String nol_menit = "";
String nol_detik = "";
// Membuat Date
Date dt = new Date();
// Mengambil nilaj JAM, MENIT, dan DETIK Sekarang
int nilai_jam = dt.getHours();
int nilai_menit = dt.getMinutes();
int nilai_detik = dt.getSeconds();
// Jika nilai JAM lebih kecil dari 10 (hanya 1 digit)
if (nilai_jam <= 9) {
// Tambahkan "0" didepannya
nol_jam = "0";
}
// Jika nilai MENIT lebih kecil dari 10 (hanya 1 digit)
if (nilai_menit <= 9) {
// Tambahkan "0" didepannya
nol_menit = "0";
}
// Jika nilai DETIK lebih kecil dari 10 (hanya 1 digit)
if (nilai_detik <= 9) {
// Tambahkan "0" didepannya
nol_detik = "0";
}
// Membuat String JAM, MENIT, DETIK
String jam = nol_jam + Integer.toString(nilai_jam);
String menit = nol_menit + Integer.toString(nilai_menit);
String detik = nol_detik + Integer.toString(nilai_detik);
// Menampilkan pada Layar
tampil_jam.setText(" " + jam + " : " + menit + " : " + detik + " ");
}
};
// Timer
new Timer(1000, taskPerformer).start();
}
// MAIN
public static void main(String[] args) {
jamDigital jd = new jamDigital();
jd.setTitle("Jam Digital");
jd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jd.setSize(200, 90);
jd.show();
}
} // Akhir Kelas
Executing and running programs
- javac Digital Clock.java
- java digital clock
From the results of the program execution above, the steps for creating the jamDigital.java program can be explained as follows:
Create a Class
The class name is jamDigital because the name of the saved file is named jamDigital.java. This application is designed using JFrame, so when creating a class, it must be extended to JFrame (extends JFrame).
public class jamDigital extends JFrame {
........
........
}
Digital Clock Process
To handle the Thread process, we will use the Timer method found in the javax.swing.Timer class. Setting up a Timer method involves creating a Timer object, registering an action listener, and running the Timer using the Start method.
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
.........
.........
}
};
New Timer (1000, taskPerformer).start();
The process of creating a digital clock begins by taking the current seconds, minutes and hours values using the getSeconds(), getMinutes(), getHours() methods.
// Membuat Date
Date dt = new Date();
// Mengambil nilaj JAM, MENIT, dan DETIK Sekarang
int nilai_jam = dt.getHours();
int nilai_menit = dt.getMinutes();
int nilai_detik = dt.getSeconds();
Before displaying the current time value on the screen, there are several things that must be checked, namely:
Second value
If the current second value obtained is less than 10, then a zero (0) is added in front of the second value so that it becomes a two-digit number.
if (nilai_detik <= 9) {
nol_detik = "0";
}
Minute value
If the current minute value obtained is less than 10, then a zero (0) is added in front of the minute value so that it becomes a two-digit number.
if (nilai_menit <= 9) {
nol_menit = "0";
}
Hour value
If the current hour value obtained is less than 10, then a zero (0) is added in front of the hour value so that it becomes a two-digit number.
if (nilai_jam <= 9) {
nol_jam = "0";
}
After making a selection using the if conditional method, the last process is to take the current time value and print the value to the screen. To change from an integer type to a string type, the method used is Integer.toString(int i).
String jam = nol_jam + Integer.toString(nilai_jam);
String menit = nol_menit + Integer.toString(nilai_menit);
String detik = nol_detik + Integer.toString(nilai_detik);
// Menampilkan pada Layar
tampil_jam.setText(" " + jam + " : " + menit + " : " + detik + " ");
Basic theory
In the experiment of making a digital clock circuit, we use a circuit consisting of a counter/counter: MOD10 for units (hours, minutes and seconds), MOD6 for tens (minutes and seconds) and MOD2 (for tens of hours), the decoder type is Binary to 7Segment, and a display/viewer.
The working principle used is the principle of the 7-Segment Decoder. Where in the design of this digital clock there are parts of the time function. Namely Hours: Minutes: Seconds. In making this digital clock, a second, minute, and hour counter will be used. Which will run the digital clock process.
To display the display of hours:minutes:seconds, a 7-Segment Display Decoder is used, where the display represents hours:minutes:seconds as follows:
The truth table of the display is as follows:
Initial Design and Preparation Tasks for Practical Work
The circuit design to be tested is:
Analysis of Practical Results
The following is a series of digital watches tested.
A very interesting counter and decode process is in the design of a digital clock. The power supply for a regular clock (hours, minutes, seconds) is a commercial 120 Volt, 0.6 kHz alternating current power supply. To obtain pulses that occur at a rate of 1 second it is necessary to divide the 0.6 kHz power source by 60. If the 0.01 kHz square wave is divided again by 60, the resulting square wave is 1 cycle per minute. If divided again by 60 it will produce a square wave of 1 cycle per hour.
The block diagram above shows the counting functions. The first :60 counter divides a 0.6 kHz power signal into 0.01 kHz square waves. The second :60 counter changes state once every second and has 60 discrete states, and can thus be decoded to produce a signal that represents the seconds. This counter is then called the seconds counter. The third :60 counter changes state once every minute and has 60 discrete states. This counter can thus be decoded to produce a signal that represents the minutes. This counter is called the minutes counter. The last counter changes state once every 60 minutes (every hour). And produces a signal that represents the hours, called the hours counter.
To set a digital clock, a setting is needed. Therefore, a means to set the clock must be obtained, because if the power is turned off and then turned back on, the flip-flop will be in a random state. The setting is done by using the set push button. The setting is done by pressing the set hour, set minute, set second buttons.
In this way the Digital Clock can be run.
Source
Document Copyright © 2007 javaku.wordpress.com and Adi Purnomo
This document may be used, modified, or distributed freely for any purpose, whether commercial or non-commercial, provided that the author's name and the original title of this article are always included.