Minggu, 30 September 2018

Membuat jam digital dan GUI

Nama : Hafidz Firman Asqalany
NRP   : 05111740000195
Kelas  : PBO A

source code Clock Display
 
 /**  
  * Write a description of class ClockDisplay here.  
  * Program membuat jam digital  
  * @author (Hafidz Firman Asqalany)  
  * @version (4.1.3)  
  */  
 public class ClockDisplay  
 {  
   private NumberDisplay hours;  
   private NumberDisplay minutes;  
   private String displayString;// menstimulasi tampilan sebenarnya  
   /** perintah untuk objek clockDisplay   
    * perintah ini untuk membuat set jam baru pada pukul 00.00  
    */  
   public ClockDisplay()  
   {  
     hours = new NumberDisplay(24);  
     minutes = new NumberDisplay(60);  
     updateDisplay();  
   }  
   /**  
    * Perintah untuk objek ClockDisplay   
    * Perintah ini untuk membuat waktu baru   
    * pada spesifikasi oleh parameter  
   */  
   public ClockDisplay (int hour, int minute)  
   {  
     hours = new NumberDisplay(24);  
     minutes = new NumberDisplay(60);  
     setTime(hour, minute);  
   }  
   /**  
    * Perintah ini harus dipanggil setiap menit  
    * perintah ini untuk membuat tampilan jam   
    * menjadi satu menit kedepan  
   */  
   public void timeTick()  
   {  
     minutes.increment();  
     if(minutes.getValue() == 0) {  
       hours.increment();  
     }  
     updateDisplay();  
   }  
   /**  
    * untuk mengatur waktu pada tampilan  
    * pada jam dan menit secara spesifik  
   */  
   public void setTime(int hour, int minute)  
   {  
     hours.setValue(hour);  
     minutes.setValue(minute);  
     updateDisplay();  
   }  
   /**  
    * mengembalikan pada waktu saat ini pada format yaitu  
    * HH:MM  
   */  
   public String getTime()  
   {  
     return displayString;  
   }  
   /**  
    * mengupdate pada internal string yang direpresentasikan  
    * oleh display  
   */  
   private void updateDisplay ()  
   {  
     int hour = hours.getValue();  
     displayString = hours.getDisplayValue () + ":" +   
             minutes.getDisplayValue();  
   }  
 }  

source code NumberDisplay

 /**  
  * Write a description of class ClockDisplay here.  
  * Program membuat jam digital  
  * @author (Hafidz Firman Asqalany)  
  * @version (4.1.3)  
  */  
 public class NumberDisplay  
 {  
   private int limit;  
   private int value;  
   /**  
    * Constructor for objects of class NumberDisplay  
    */  
   public NumberDisplay(int rollOverlimit)  
   {  
     limit = rollOverlimit;  
     value = 0;  
   }  
   public int getValue()  
   {  
     return value;  
   }  
   /**  
    * Atur nilai tampilan ke nilai yang ditentukan yang baru.   
    * Jika nilai baru kurang dari nol atau melebihi batas  
    * Jangan melakukan apa-apa  
    */  
   public void setValue(int replacementValue)  
   {  
     if((replacementValue >= 0) &&   
       (replacementValue < limit))  
     {  
       value = replacementValue;  
     }  
   }  
   /**  
    * Kembalikan nilai tampilan (yaitu, nilai saat ini sebagai String dua digit.   
    * Jika nilainya kurang dari sepuluh, itu akan mudah dengan nol terkemuka)  
    */  
   public String getDisplayValue()  
   {  
     if(value < 10)  
     {  
       return "0" + value;  
     }  
     else  
     {  
       return "" + value;  
     }  
   }  
   /**    
   * Menaikkan nilai tampilan dengan satu dan dinaikkan menjadi nol  
   * jika batas tercapai  
   */  
   public void increment ()  
   {  
     value = (value +1) % limit;  
   }  
 }  

source code TestClockDisplay

 /**  
  * Write a description of class ClockDisplay here.  
  * Program membuat jam digital  
  * @author (Hafidz Firman Asqalany)  
  * @version (4.1.3)  
  */  
 public class TestClockDisplay  
  {  
   public void test()  
   {  
     ClockDisplay clock = new ClockDisplay();  
     clock.setTime(17,45);  
     System.out.println(clock.getTime());  
     clock.setTime(00,15);  
     System.out.println(clock.getTime());  
     clock.setTime(15,00);  
     System.out.println(clock.getTime());  
   }  
  }   

source code GUI

 /**  
  * Write a description of class ClockDisplay here.  
  * Program membuat jam digital  
  * @author (Hafidz Firman Asqalany)  
  * @version (4.1.3)  
  */   
 import java.awt.*;   
  import java.awt.event.*;   
  import javax.swing.*;   
  import javax.swing.border.*;   
  public class ClockGUI   
  {   
   private JFrame frame;   
   private JLabel label;   
   private ClockDisplay clock;   
   private boolean clockOn = false;   
   private TimerThread timerThread;   
   //fungsi untuk membuat sebuah objek dari class Clock   
   public void Clock()   
   {   
    makeFrame();   
    clock = new ClockDisplay();   
   }   
   //fungsi untuk memulai jam.   
   private void start()   
   {   
    clockOn = true;   
    timerThread = new TimerThread();   
    timerThread.start();   
   }   
   //fungsi untuk memberhentikan jam.   
   private void stop()   
   {   
    clockOn = false;   
   }   
   //fungsi untuk menjalankan jam secara perlahan (naik per menitnya).   
   private void step()   
   {   
    clock.timeTick();   
    label.setText(clock.getTime());   
   }   
   //fungsi untuk menampilkan program Clock ini.   
   private void showAbout()   
   {   
    JOptionPane.showMessageDialog (frame, "Clock Version 0.1\n" +    
    "Membuat jam digital simpel dengan Java.",   
    "About Clock",   
    JOptionPane.INFORMATION_MESSAGE);   
   }   
   //fungsi untuk keluar dari program.   
   private void quit()   
   {   
    System.exit(0);   
   }   
   //fungsi untuk membuat frame dan konten di dalamnya.   
   private void makeFrame()   
   {   
    frame = new JFrame("Clock");   
    JPanel contentPane = (JPanel)frame.getContentPane();   
    contentPane.setBorder(new EmptyBorder(1,60,1,60));   
    makeMenuBar(frame);   
    //secara spesifik mengatur tampilan dengan jarak.   
    contentPane.setLayout(new BorderLayout(12,12));   
    //membuat tampilan di tengah frame.   
    label = new JLabel("00:00", SwingConstants.CENTER);   
    Font displayFont = label.getFont().deriveFont(96.0f);   
    label.setFont(displayFont);   
    contentPane.add(label, BorderLayout.CENTER);   
    //membuat toolbar dengan tombol   
    JPanel toolbar = new JPanel();   
    toolbar.setLayout(new GridLayout(1,0));   
    //membuat tombol Start untuk memulai jam.   
    JButton startButton = new JButton("Start");   
    startButton.addActionListener(e->start());   
    toolbar.add(startButton);   
    //membuat tombol Stop untuk menghentikan jam.   
    JButton stopButton = new JButton("Stop");   
    stopButton.addActionListener(e->stop());   
    toolbar.add(stopButton);   
    //membuat tombol Step untuk menambah 1 menit pada jam.   
    JButton stepButton = new JButton("Step");   
    stepButton.addActionListener(e->step());   
    toolbar.add(stepButton);   
    //untuk memasukkan toolbar ke dalam panel disertai dengan jarak   
    JPanel flow = new JPanel();   
    flow.add(toolbar);   
    contentPane.add(flow, BorderLayout.SOUTH);   
    //menandakan bahwa frame sudah selesai - mengatur komponen di dalamnya   
    frame.pack();   
    //memposisikan frame di tengah layar serta menampilkannya   
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();   
    frame.setLocation(d.width/2 - frame.getWidth()/2, d.height/2 - frame.getHeight()/2);   
    frame.setVisible(true);   
   }   
   //fungsi yang membuat menubar di frame utama   
   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   
   {   
    //fungsi saat jam sedang dijalankan.   
    public void run()   
     {   
      //selama jam jalan, maka akan dilakukan Step(menambah nilai setiap menitnya).   
      //pause (berhenti selama beberapa saat sehingga nilai sekarang dapat terlihat.   
      while(clockOn)   
      {   
       step();   
       pause();   
      }   
     }   
     private void pause()   
     {   
      try    
      {   
       Thread.sleep(900);   
      }   
      catch(InterruptedException exc)   
      {   
      }   
    }   
   }   
  }   

dan ini merupakan hasil programnya



Tidak ada komentar:

Posting Komentar