Simple Digital Clock

Kali ini saya ingin membagikan source code tentang aplikasi Digital Clock. Jadi aplikasi ini merupakan jam digital yang bisa dimanfaatkan pastinya. Software yang digunakan adalah BlueJ, dan bahasa pemrograman yang digunakan adalah java. Berikut adalah preview jam digital dan source code nya.
Gambar Visual Jam (Jam : Menit)
Cara Pemakaian :
  • Start untuk Menjalankan Jam Digital
  • Stop untuk Menghentikan Jam Digital
  • Step-Min untuk mengubah menitnya sebanyak 1 (naik)
  • Step-Hour untuk mengubah jamnya sebanyak 1 (naik)

Class Clock (Untuk Menjalankan Jam Digital)
 import java.awt.*;  
 import java.awt.event.*;  
 import javax.swing.*;  
 import javax.swing.border.*;  
 import java.util.Scanner;  
 /**  
  * Simple Digital Clock  
  *  
  * @author Ahmad Yahya Abdul Aziz  
  * @version 2018.09.30  
  */  
 public class Clock  
 {  
   Scanner sc = new Scanner(System.in);  
   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()  
   {  
     clockRunning = true;  
     timerThread = new TimerThread();  
     timerThread.start();  
   }  
   private void stop()  
   {  
     clockRunning = false;  
   }  
   private void stepMinute()  
   {  
     clock.timeTickMinute();  
     label.setText(clock.getTime());  
   }  
   private void stepHour()  
   {  
     clock.timeTickHour();  
     label.setText(clock.getTime());  
   }  
   private void showAbout()  
   {  
     JOptionPane.showMessageDialog (frame,   
       "Digital Clock\n",   
       "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, 50, 1, 50));  
     makeMenuBar(frame);  
     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 stepButtonM = new JButton("STEP-MIN");  
     stepButtonM.addActionListener(e -> stepMinute());  
     toolbar.add(stepButtonM);  
     JButton setButtonH = new JButton("STEP-HOUR");  
     setButtonH.addActionListener(e -> stepHour());  
     toolbar.add(setButtonH);  
     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);  
   }  
   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) {  
         stepMinute();  
         pause();  
       }  
     }  
     private void pause()  
     {  
       try {  
         Thread.sleep(60000);  
       }  
       catch (InterruptedException exc) {  
       }  
     }  
   }  
 }  

Class ClockDisplay (Untuk Setting Visual Jam)
 public class ClockDisplay  
 {  
   private NumberDisplay hours;  
   private NumberDisplay minutes;  
   private String displayString;  
   /**  
     * new clock set at 00:00.  
   */  
   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 timeTickMinute()  
   {  
     minutes.increment();  
     if(minutes.getValue() == 0) {  
       hours.increment();  
     }  
     updateDisplay();  
   }  
   public void timeTickHour()  
   {  
     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();  
   }  
 }  

Class NumberDisplay (Untuk Operasi Perubahan Menit dan Jam)
 public class NumberDisplay  
 {  
    private int limit;  
    private int value;  
    public NumberDisplay(int rollOverLimit)  
    {  
      limit = rollOverLimit;  
      value = 0;  
    }  
    public int getValue()  
    {  
      return value;  
    }  
    public String getDisplayValue()  
    {  
      if(value < 10) {  
        return "0" + value;  
      }  
      else {  
        return "" + value;  
      }  
    }  
    public void setValue(int replacementValue)  
    {  
      if((replacementValue >= 0) && (replacementValue < limit)) {  
        value = replacementValue;  
      }  
    }  
    public void increment()  
    {    
      value = (value + 1) % limit;  
    }  
 }  

Nama : Ahmad Yahya Abdul Aziz
NRP   : 05111740000128
Kelas  : PBO - A

Comments

Popular posts from this blog

Visualisasi Java - Membuat Rumah

Program Mesin Penjual Tiket

Notion