PBO 4

  muhammad rafi budi purnama

5025221307

PBO 4


Pada tugas 4 kita akan membuat sebuah jam yang memiliki tampilan. Implementasi pada bahasa Java adalah seperti berikut


gambar diatas adalah class diagram nya. Implementasi setiap class ditunjukkan seperti berikut

Clock

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.time.LocalDate;

import java.time.format.DateTimeFormatter;


public class DigitalClockGUI {

    private ClockDisplay clockDisplay;

    private JFrame frame;

    private JLabel timeLabel;

    private JLabel dateLabel;

    private JButton startButton;

    private JButton stopButton;

    private JButton stepButton;

    private Timer timer;

    private boolean isRunning;

    

    public DigitalClockGUI() {

        clockDisplay = new ClockDisplay();

        createGUI();

        updateDisplay();

    }

    

    public DigitalClockGUI(int hour, int minute, int second, int day, int month, int year) {

        clockDisplay = new ClockDisplay(hour, minute, second);

        createGUI();

        updateDisplay();

    }

    

    private void createGUI() {

        // Create main frame

        frame = new JFrame("CloudDisplay");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setLayout(new BorderLayout());

        frame.setSize(400, 300);

        

        // Create main panel with dark background

        JPanel mainPanel = new JPanel();

        mainPanel.setLayout(new BorderLayout());

        mainPanel.setBackground(new Color(30, 30, 50));

        mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        

        // Time display (large digital font)

        timeLabel = new JLabel("", JLabel.CENTER);

        timeLabel.setFont(new Font("Digital-7", Font.BOLD, 48));

        timeLabel.setForeground(new Color(0, 255, 255)); // Cyan color

        timeLabel.setBackground(new Color(20, 20, 40));

        timeLabel.setOpaque(true);

        timeLabel.setBorder(BorderFactory.createLineBorder(new Color(0, 150, 150), 2));

        

        // Date display

        dateLabel = new JLabel("", JLabel.CENTER);

        dateLabel.setFont(new Font("Arial", Font.PLAIN, 18));

        dateLabel.setForeground(Color.WHITE);

        

        // Control buttons panel

        JPanel buttonPanel = new JPanel();

        buttonPanel.setLayout(new FlowLayout());

        buttonPanel.setBackground(new Color(30, 30, 50));

        

        // Start Button

        startButton = new JButton("Start");

        startButton.setFont(new Font("Arial", Font.BOLD, 14));

        startButton.setBackground(new Color(0, 150, 0));

        startButton.setForeground(Color.WHITE);

        startButton.addActionListener(e -> startClock());

        

        // Stop Button

        stopButton = new JButton("Stop");

        stopButton.setFont(new Font("Arial", Font.BOLD, 14));

        stopButton.setBackground(new Color(150, 0, 0));

        stopButton.setForeground(Color.WHITE);

        stopButton.addActionListener(e -> stopClock());

        

        // Step Button

        stepButton = new JButton("Step");

        stepButton.setFont(new Font("Arial", Font.BOLD, 14));

        stepButton.setBackground(new Color(0, 100, 200));

        stepButton.setForeground(Color.WHITE);

        stepButton.addActionListener(e -> stepClock());

        

        // Add buttons to panel

        buttonPanel.add(startButton);

        buttonPanel.add(stopButton);

        buttonPanel.add(stepButton);

        

        // Add components to main panel

        mainPanel.add(timeLabel, BorderLayout.CENTER);

        mainPanel.add(dateLabel, BorderLayout.NORTH);

        mainPanel.add(buttonPanel, BorderLayout.SOUTH);

        

        frame.add(mainPanel);

        

        // Create timer for clock updates (1 second interval)

        timer = new Timer(1000, e -> {

            clockDisplay.timeTick();

            updateDisplay();

        });

        

        isRunning = false;

    }

    

    private void updateDisplay() {

        // Update time display

        timeLabel.setText(clockDisplay.getTime());

        

        // Update date display (current date)

        LocalDate currentDate = LocalDate.now();

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");

        dateLabel.setText(currentDate.format(formatter));

    }

    

    private void startClock() {

        if (!isRunning) {

            timer.start();

            isRunning = true;

            startButton.setEnabled(false);

            stopButton.setEnabled(true);

        }

    }

    

    private void stopClock() {

        if (isRunning) {

            timer.stop();

            isRunning = false;

            startButton.setEnabled(true);

            stopButton.setEnabled(false);

        }

    }

    

    private void stepClock() {

        clockDisplay.timeTick();

        updateDisplay();

    }

    

    public void show() {

        frame.setVisible(true);

    }

    

    // Method untuk testing di BlueJ

    public static void testDigitalClock() {

        DigitalClockGUI clock = new DigitalClockGUI(14, 36, 28, 16, 9, 2025);

        clock.show();

    }

    

    public static void testCurrentTime() {

        DigitalClockGUI clock = new DigitalClockGUI();

        clock.show();

    }

}


ClockDisplay

// ClockDisplay.java

import java.time.LocalDateTime;

import java.time.format.DateTimeFormatter;


public class ClockDisplay {

    private NumberDisplay hours;

    private NumberDisplay minutes;

    private NumberDisplay seconds;

    private String displayString;


    public ClockDisplay() {

        hours = new NumberDisplay(24);

        minutes = new NumberDisplay(60);

        seconds = new NumberDisplay(60);

        updateDisplay();

    }


    public ClockDisplay(int hour, int minute, int second) {

        hours = new NumberDisplay(24);

        minutes = new NumberDisplay(60);

        seconds = new NumberDisplay(60);

        setTime(hour, minute, second);

    }


    public void setTime(int hour, int minute, int second) {

        hours.setValue(hour);

        minutes.setValue(minute);

        seconds.setValue(second);

        updateDisplay();

    }


    public void timeTick() {

        seconds.increment();

        if (seconds.getValue() == 0) {

            minutes.increment();

            if (minutes.getValue() == 0) {

                hours.increment();

            }

        }

        updateDisplay();

    }


    public String getTime() {

        return displayString;

    }


    private void updateDisplay() {

        displayString = hours.getDisplayValue() + ":" + 

                       minutes.getDisplayValue() + ":" + 

                       seconds.getDisplayValue();

    }


    public void printTime() {

        System.out.println(displayString);

    }

}


NumberDisplay

// NumberDisplay.java

public class NumberDisplay {

    private int value;

    private int limit;


    public NumberDisplay(int rollOverLimit) {

        this.limit = rollOverLimit;

        this.value = 0;

    }


    public int getValue() {

        return value;

    }


    public void setValue(int replacementValue) {

        if (replacementValue >= 0 && replacementValue < limit) {

            value = replacementValue;

        }

    }


    public String getDisplayValue() {

        if (value < 10) {

            return "0" + value;

        } else {

            return "" + value;

        }

    }


    public void increment() {

        value = (value + 1) % limit;

    }

}






Gambar diatas adalah hasil dari implementasi program clock-gui

Komentar

Postingan populer dari blog ini

TUGAS 2 : Jettpack compose : Hello, World!

Pertemuan 3 PPB (A) - Mengenal Composable Aplikasi Selamat Ulang Tahun

Pertemuan 5 - Aplikasi Kalkulator Sederhana