Nama : Safa Mashita
NRP : 5025241022
Dalam studi kasus ini, saya ingin membuat sebuah jam digital sederhana yang bisa
-
Menampilkan waktu dalam format HH:MM (24 jam).
-
Bisa berjalan otomatis (berdetik tiap interval waktu).
-
Bisa dikontrol lewat tombol GUI (Start, Stop, Step).
-
Memiliki menu sederhana untuk About dan Quit.
Masalah ini dibagi menjadi beberapa level, yakni sebagai berikut.
-
Level angka tunggal (roda angka) : ditangani oleh Class NumberDisplay.
-
Level jam digital (gabungan jam & menit) : ditangani oleh Class ClockDisplay.
-
Level tampilan dan interaksi pengguna : ditangani oleh Clock (GUI).
Macam-Macam Class
Atribut:
-
limit : batas atas angka.
-
value : nilai saat ini.
Method:
-
getValue () : mengambil angka mentah.
-
getDisplayValue () : mengambil string 2 digit.
-
setValue () : set angka (jika valid).
-
increment() : menambah 1 angka, roll over jika melewati batas.
Note:
Class NumberDisplay tidak tahu apa itu jam. Class tersebut hanya tahu cara menghitung angka terbatas.
2. Class ClockDisplay
Merepresentasikan jam digital 24 jam.
Menggunakan dua buah NumberDisplay: satu untuk jam, satu untuk menit.
Kode :
public class ClockDisplay {
private NumberDisplay hours;
private NumberDisplay minutes;
private String displayString;
public ClockDisplay() {
hours = new NumberDisplay(24);
minutes = new NumberDisplay(60);
updateDisplay();
}
public ClockDisplay(int hour, int minute) {
hours = new NumberDisplay(24);
minutes = new NumberDisplay(60);
setTime(hour, minute);
}
public void timeTick() {
minutes.increment();
if (minutes.getValue() == 0) {
hours.increment();
}
updateDisplay();
}
public void setTime(int hour, int minute) {
hours.setValue(hour);
minutes.setValue(minute);
updateDisplay();
}
public String getTime() {
return displayString;
}
private void updateDisplay() {
displayString = hours.getDisplayValue() + ":" + minutes.getDisplayValue();
}
}
-
hours : NumberDisplay untuk jam (0–23).
-
minutes : NumberDisplay untuk menit (0–59).
-
displayString : string gabungan "HH:MM".
Method:
-
timeTick () : tambah 1 menit, kalau menit reset → jam ikut naik.
-
setTime () : set jam & menit.
-
getTime() : ambil teks waktu.
-
updateDisplay () : refresh displayString.
Note :
Class ini menyatukan 2 angka (jam & menit) jadi waktu.
3. Class Clock
Memberikan antarmuka grafis (GUI) agar user bisa melihat waktu dan mengontrolnya.
Kode :
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;
public Clock() {
makeFrame();
clock = new ClockDisplay();
}
private void start() {
if (!clockRunning) {
clockRunning = true;
timerThread = new TimerThread();
timerThread.start();
}
}
private void stop() {
clockRunning = false;
}
private void step() {
clock.timeTick();
label.setText(clock.getTime());
}
private void showAbout() {
JOptionPane.showMessageDialog(frame,
"Clock Version 1.0\nA simple interface for the clock display project",
"About Clock", JOptionPane.INFORMATION_MESSAGE);
}
private void quit() {
System.exit(0);
}
private void makeFrame() {
frame = new JFrame("Clock");
JPanel contentPane = (JPanel) frame.getContentPane();
contentPane.setBorder(new EmptyBorder(1, 60, 1, 60));
contentPane.setLayout(new BorderLayout(12, 12));
label = new JLabel("00:00", SwingConstants.CENTER);
Font displayFont = label.getFont().deriveFont(96.0f);
label.setFont(displayFont);
contentPane.add(label, BorderLayout.CENTER);
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);
JPanel flow = new JPanel();
flow.add(toolbar);
contentPane.add(flow, BorderLayout.SOUTH);
frame.pack();
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(d.width / 2 - frame.getWidth() / 2, d.height / 2 - frame.getHeight() / 2);
frame.setVisible(true);
makeMenuBar(frame);
}
private void makeMenuBar(JFrame frame) {
final int SHORTCUT_MASK = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
JMenuBar menubar = new JMenuBar();
frame.setJMenuBar(menubar);
JMenu menu;
JMenuItem item;
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);
} catch (InterruptedException exc) {}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new Clock());
}
}
frame : jendela utama.
- label : teks waktu di layar.
clock : objek ClockDisplay.
clockRunning : status on/off.
timerThread : thread untuk update otomatis.
Method:
-
makeFrame () : membuat GUI (label, tombol).
-
makeMenuBar () : membuat menu (About, Quit).
-
start () : menjalankan jam otomatis.
-
stop () : menghentikan jam.
-
step() : menambah 1 menit manual.
-
showAbout() : menampilkan info program.
-
quit() : keluar program.
-
main() : titik masuk program.
Inner Class:
-
TimerThread : thread yang berulang-ulang memanggil
step()tiap 300ms.
Class ini tidak tahu logika jam. Class tersebut hanya tahu cara menampilkan hasil dari ClockDisplay.
Hubungan AntarClass
Clock : menggunakan ClockDisplay.
- ClockDisplay : meggunakan dua NumberDisplay.
NumberDisplay : berdiri sendiri, hanya logika angka.
Tidak ada komentar:
Posting Komentar