Pada post kali ini, kami ingin mendemonstrasikan sebuah program Clock Display, yaitu sebuah tampilan jam digital sederhana. Program ini ditulis menggunakan bahasa pemrograman Java. Tampilan jam ditunjukkan dalam format 24 jam dengan gaya Eropa. Dan jam digital ditampilkan dalam satuan jam dan menit.
Clock Display
Berikut adalah diagram Clock Display.
The NumberDisplay class
Kelas NumberDisplay merepresentasikan tampilan angka digital yang dapat menampungnilai dari nol sampai batas tertentu. Batas dapat ditentukan saat membuat display. Saat mencapai limit, tampilan otomatis berputar ke nol kembali.
Code 3.3 Implementation of the NumberDisplay class
/**
* class NumberDisplay merepresentasikan tampilan angka digital yang dapat menampung
* nilai dari nol sampai batas tertentu. Batas dapat ditentukan saat membuat display.
* Nilai berkisar dari nol (inklusif) sampai batas-1. Jika digunakan, misalnya, untuk
* detik pada sebuah jam digital, batasnya adalah 60, menghasilkan nilai display dari
* 0 sampai 59. Saat bertambah, tampilan otomatis berputar ke nol saat mencapai limit.
*
* @author Ramadhan Arif Hardijansyah
* @version 0.1 17 October 2020
*/
public class NumberDisplay
{
private int limit;
private int value;
/**
* Konstruktor untuk objek dari class NumberDisplay.
* Menetapkan limit di mana display berputar kembali ke nol.
*/
public NumberDisplay(int rollOverLimit)
{
limit = rollOverLimit;
value = 0;
}
/**
* Mengembalikan nilai (value) saat ini.
*/
public int getValue()
{
return value;
}
/**
* Mengembalikan nilai display (yaitu, nilai saat ini sebagai dua digit String.
* Jika nilainya kurang dari sepuluh, maka akan ditambahkan angka nol di depan.
*/
public String getDisplayValue()
{
if(value < 10) {
return "0" + value;
}
else {
return "" + value;
}
}
/**
* Menyetel nilai display ke nilai baru yang ditentukan. Jika nilai baru
* kurang dari nol atau melebihi batas, jangan melakukan apa pun.
*/
public void setValue(int replacementValue)
{
if((replacementValue >= 0) && (replacementValue < limit)) {
value = replacementValue;
}
}
/**
* Menaikkan nilai display sebanyak satu, berputar ke nol jika
* batasnya tercapai.
*/
public void increment()
{
value = (value + 1) % limit;
}
}
The ClockDisplay class
Kelas ClockDisplay mengimplementasikan sebuah tampilan jam digital untuk waktu 24 jam bergaya Eropa. Jam menunjukkan satuan jam dan menit. Kisaran jam adalah 00:00 (tengah malam) hingga 23:59 (satu menit sebelum tengah malam).
Code 3.3 Implementation of the ClockDisplay class
/**
* class ClockDisplay mengimplementasikan sebuah tampilan jam digital untuk
* waktu 24 jam bergaya Eropa. Jam menunjukkan satuan jam dan menit.
* Kisaran jam adalah 00:00 (tengah malam) hingga 23:59 (satu menit sebelum
* tengah malam).
*
* Tampilan jam menerima "ticks" (melalui method timeTick) setiap menit
* dan bereaksi dengan menaikkan display. Ini dilakukan dengan mode jam biasa:
* satuan jam bertambah saat satuan menit berputar ke nol.
*
* @author Ramadhan Arif Hardijansyah
* @version 0.1 17 October 2020
*/
public class ClockDisplay
{
private NumberDisplay hours;
private NumberDisplay minutes;
private String displayString; // mensimulasikan tampilan sebenarnya
/**
* Konstruktor untuk objek ClockDisplay. Konstruktor ini
* membuat jam baru yang di-set pada 00:00.
*/
public ClockDisplay()
{
hours = new NumberDisplay(24);
minutes = new NumberDisplay(60);
updateDisplay();
}
/**
* Konstruktor untuk objek ClockDisplay. Konstruktor ini
* membuat jam baru yang diatur pada waktu yang ditentukan oleh
* parameter.
*/
public ClockDisplay(int hour, int minute)
{
hours = new NumberDisplay(24);
minutes = new NumberDisplay(60);
setTime(hour, minute);
}
/**
* Method ini harus dipanggil sekali setiap menit - ini membuat
* tampilan jam berjalan satu menit kedepan.
*/
public void timeTick()
{
minutes.increment();
if(minutes.getValue() == 0) { // it just rolled over!
hours.increment();
}
updateDisplay();
}
/**
* Setel waktu tampilan ke jam dan menit yang ditentukan.
*/
public void setTime(int hour, int minute)
{
hours.setValue(hour);
minutes.setValue(minute);
updateDisplay();
}
/**
* Mengembalikan waktu saat ini dari display dalam format HH: MM.
*/
public String getTime()
{
return displayString;
}
/**
* Update string internal yang merepresentasikan tampilan.
*/
private void updateDisplay()
{
int hour = hours.getValue();
String suffix;
if (hour >= 12) {
suffix = "pm";
}
else {
suffix = "am";
}
if (hour >= 12) {
hour -= 12;
}
if (hour == 0) {
hour = 12;
}
displayString = hour + ":" +
minutes.getDisplayValue() + suffix;
}
}
The TestClockDisplay class
Sebuah kelas untuk menguji perubahan pada kelas ClockDisplay.
/**
* A class to test changes to the ClockDisplay class.
*
* @author Ramadhan Arif Hardijansyah
* @version 0.1 17 October 2020
*/
public class TestClockDisplay
{
// instance variables - replace the example below with your own
private int x;
/**
* Constructor for objects of class TestClockDisplay
*/
public TestClockDisplay()
{
}
public void test()
{
ClockDisplay clock = new ClockDisplay();
clock.setTime(22, 30);
System.out.println(clock.getTime());
clock.setTime(10, 30);
System.out.println(clock.getTime());
clock.setTime(0, 0);
System.out.println(clock.getTime());
clock.setTime(12, 0);
System.out.println(clock.getTime());
}
}
Berikut ini adalah hasil keluaran terminal dari Clock Display di BlueJ.
Clock Display with GUI
Berikut adalah diagram Clock Display with GUI. Terdiri dari kelas NumberDisplay, ClockDisplay, dan Clock. Untuk kelas NumberDisplay dan ClockDisplay, kode program mirip seperti Project sebelumnya sehingga akan ditampilkan untuk kode kelas Clock saja.
The Clock class
Sebuah GUI (graphical user interface) yang sangat sederhana untuk tampilan jam. Pada implementasi ini, waktu berjalan sekitar 3 menit per detik, sehingga pengujian tampilan sedikit lebih cepat.
/**
* Sebuah GUI (graphical user interface) yang sangat sederhana untuk tampilan jam.
* Pada implementasi ini, waktu berjalan sekitar 3 menit per detik, sehingga
* pengujian tampilan sedikit lebih cepat.
*
* @author Ramadhan Arif Hardijansyah
* @version 0.1 17 October 2020
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class Clock
{
private JFrame frame;
private JLabel label;
private ClockDisplay clock;
private boolean clockRunning = false;
private TimerThread timerThread;
/**
* Constructor for objects of class Clock
*/
public Clock()
{
makeFrame();
clock = new ClockDisplay();
}
/**
*
*/
private void start()
{
clockRunning = true;
timerThread = new TimerThread();
timerThread.start();
}
/**
*
*/
private void stop()
{
clockRunning = false;
}
/**
*
*/
private void step()
{
clock.timeTick();
label.setText(clock.getTime());
}
/**
* 'About' function: show the 'about' box.
*/
private void showAbout()
{
JOptionPane.showMessageDialog (frame,
"Clock Version 1.0\n" +
"A simple interface for the 'Objects First' clock display project",
"About Clock",
JOptionPane.INFORMATION_MESSAGE);
}
/**
* Quit function: keluar dari aplikasi.
*/
private void quit()
{
System.exit(0);
}
/**
* Membuat Swing frame dan kontennya.
*/
private void makeFrame()
{
frame = new JFrame("Clock");
JPanel contentPane = (JPanel)frame.getContentPane();
contentPane.setBorder(new EmptyBorder(1, 60, 1, 60));
makeMenuBar(frame);
// Menetapkan layout manager dengan spacing yang bagus
contentPane.setLayout(new BorderLayout(12, 12));
// Membuat panel gambar di tengah
label = new JLabel("00:00", SwingConstants.CENTER);
Font displayFont = label.getFont().deriveFont(96.0f);
label.setFont(displayFont);
//imagePanel.setBorder(new EtchedBorder());
contentPane.add(label, BorderLayout.CENTER);
// Membuat toolbar dengan tombol-tombol
JPanel toolbar = new JPanel();
toolbar.setLayout(new GridLayout(1, 0));
JButton startButton = new JButton("Start");
startButton.addActionListener(e -> start());
toolbar.add(startButton);
JButton stopButton = new JButton("Stop");
stopButton.addActionListener(e -> stop());
toolbar.add(stopButton);
JButton stepButton = new JButton("Step");
stepButton.addActionListener(e -> step());
toolbar.add(stepButton);
// Menambahkan toolbar ke panel dengan flow layout untuk spacing
JPanel flow = new JPanel();
flow.add(toolbar);
contentPane.add(flow, BorderLayout.SOUTH);
// building selesai - mengatur komponen
frame.pack();
// taruh frame di tengah layar dan perlihatkan
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(d.width/2 - frame.getWidth()/2, d.height/2 - frame.getHeight()/2);
frame.setVisible(true);
}
/**
* Membuat menu bar untuk main frame
*
* @param frame Sebuah frame yang mana menu bar harus ditambahkan.
*/
private void makeMenuBar(JFrame frame)
{
final int SHORTCUT_MASK =
Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
JMenuBar menubar = new JMenuBar();
frame.setJMenuBar(menubar);
JMenu menu;
JMenuItem item;
// membuat File menu
menu = new JMenu("File");
menubar.add(menu);
item = new JMenuItem("About Clock...");
item.addActionListener(e -> showAbout());
menu.add(item);
menu.addSeparator();
item = new JMenuItem("Quit");
item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, SHORTCUT_MASK));
item.addActionListener(e -> quit());
menu.add(item);
}
class TimerThread extends Thread
{
public void run()
{
while (clockRunning) {
step();
pause();
}
}
private void pause()
{
try {
Thread.sleep(300); // pause 300 milliseconds
}
catch (InterruptedException exc) {
}
}
}
}
Berikut ini adalah hasil keluaran jendela Clock Display with GUI dengan BlueJ. Terlihat ada tombol Start, Stop, dan Step.
Sekian post tentang Java Clock Display. Semoga bermanfaat.
Comments
Post a Comment