Jumat, 28 September 2018

Tugas 5 PBO-A Membuat Jam Sederhana dengan GUI

Berikut Source code dari Jam sederhananya :

Clock GUI :

/**  
  * Tugas 5 PBO-A Clock 
  * author M. Fatih 
  * 29/09/2018  
  */  
 import javax.swing.*;
 import javax.swing.border.*;
 import java.awt.*;
 import java.awt.event.*;
 
 public class ClockGUI  
 {  
   private JFrame frame;  
   private JLabel label;  
   private ClockDisplay clock;  
   private boolean clockOn = false;  
   private TimerThread timerThread;  
   
   public void Clock()  
   {  
     makeFrame();  
     clock = new ClockDisplay();  
   }  
   
   private void start()  
   {  
     clockOn = true;  
     timerThread = new TimerThread();  
     timerThread.start();  
   }  
   
   private void stop()  
   {  
     clockOn = false;  
   }  
   
   private void step()  
   {  
     clock.timeTick();  
     label.setText(clock.getTime());  
   }  
   
   private void showTentang()  
   {  
     JOptionPane.showMessageDialog (frame, "Jam Digital\n" +   
     "Jam digital dengan Bahasa Java.",  
     "Tentang",
     JOptionPane.INFORMATION_MESSAGE);  
   }  
   
   private void quit()  
   {  
     System.exit(0);  
   }  
   
   private void makeFrame()  
   {  
     frame = new JFrame("Jam");  
     JPanel contentPane = (JPanel)frame.getContentPane();  
     contentPane.setBorder(new EmptyBorder(1,60,1,60));  
     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 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);  
   }  
   
   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("Tentang Jam...");  
       item.addActionListener(e->showTentang());  
     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(clockOn)  
         {  
           step();  
           tunda();  
         }  
       }  
       private void tunda()  
       {  
         try   
         {  
           Thread.sleep(900);  
         }  
         catch(InterruptedException exc)  
         {  
         }  
     }  
   }  
 }  


Clock Display :



/**  
  * Tugas 5 PBO-A Clock 
  * author M. Fatih 
  * 29/09/2018  
  */  
 public class ClockDisplay  
 {  
   private NumberDisplay jam;  
   private NumberDisplay menit;  
   private String displayString; 
   
   public ClockDisplay()  
   {  
     jam = new NumberDisplay(24);  
     menit = new NumberDisplay(60);  
     updateDisplay();  
   }  
   
   public ClockDisplay(int hour, int minute)  
   {  
     jam = new NumberDisplay(24);  
     menit = new NumberDisplay(60);
     setTime(hour,minute);  
   }  
   
   public void timeTick()  
   {  
     menit.increment();  
     if(menit.getValue() == 0)  
     {  
       jam.increment();  
     }  
     updateDisplay();  
   }  
   
   public void setTime(int hour, int minute)  
   {  
     jam.setValue(hour);  
     menit.setValue(minute);  
     updateDisplay();  
   }  
   
   public String getTime()  
   {  
     return displayString;  
   }  
   
   private void updateDisplay()  
   {  
     displayString = jam.getDisplayValue() + ":" + menit.getDisplayValue();  
   }  
 }  

Number Display :



/**  
  * Tugas 5 PBO-A Clock 
  * author M. Fatih 
  * 29/09/2018  
  */  
 public class NumberDisplay  
 {  
   private int limit;  
   private int nilai;  
   
   public NumberDisplay(int Maksimal)  
   {  
     limit = Maksimal;  
     nilai = 0;  
   }  
   
   public int getValue()  
   {  
     return nilai;  
   }  
   
   public void increment()  
   {  
     nilai = (nilai + 1) % limit;  
   }  
   
   public String getDisplayValue()  
   {  
     if(nilai < 10)  
     {  
       return "0" + nilai;  
     }  
     else  
     {  
       return "" + nilai;  
     }  
   }  
   
   public void setValue(int replacementValue)  
   {  
     if((replacementValue >= 0) && (replacementValue < limit))  
     {  
       nilai = replacementValue;  
     }  
   }  
 }  

Set Clock Display :



/**  
  * Tugas 5 PBO-A Clock 
  * author M. Fatih 
  * 29/09/2018  
  */ 
public class TestClockDisplay  
 {  
   public TestClockDisplay()  
   {  
   }  
   public void test()  
   {  
     ClockDisplay clock = new ClockDisplay();  
     clock.setTime(01,52);  
     System.out.println(clock.getTime());  
     clock.setTime(01,9);  
     System.out.println(clock.getTime()); 
     clock.setTime(00,34);  
     System.out.println(clock.getTime());
     clock.setTime(06,30);  
     System.out.println(clock.getTime());
   }  
 }  


Berikut adalah outputnya
Saat menjalankan Test Clock Display :


Saat Menjalankan Clock GUI :


Saat Clock GUI dijalankan dan di berhentikan pada menit ke 1 dan detik ke 52 :


Saat menu file dibuka dan dipilih "Tentang" :



Sekian Terimakasih :)

Tidak ada komentar:

Posting Komentar

Batman Begins - Help Select