ghifariastaudi

Untitled

Oct 19th, 2020
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.48 KB | None | 0 0
  1.  
  2. /**
  3.  * Write a description of class TestTimer here.
  4.  * Class untuk waktu rambu lalu lintas
  5.  * @Ghifari Astaudi'Ukumullah
  6.  * @19 Okt 2020
  7.  */
  8.  
  9. import java.awt.*;
  10. import javax.swing.*;
  11. import java.awt.event.*;
  12.  
  13. public class TestTimer extends JPanel
  14. {
  15.     //inisasi detik
  16.     private int detik;
  17.     //kelas dimensi untuk mengatur ukuran yang disukai
  18.     public Dimension getPreferredSize(){
  19.         //besar panjang dan lebar suatu string
  20.         FontMetrics font = getFontMetrics (getFont());
  21.         return new Dimension (font.stringWidth("00"), font.getHeight());
  22.     }
  23.    
  24.     public void setValue (int newnilai){
  25.         //kondisi bahwa detik tidak sama dengan newvalue
  26.         if (detik != newnilai){
  27.             detik = newnilai;
  28.             repaint(); //menggambar ulang panel
  29.         }
  30.     }
  31.  
  32.     public int getvalue(){
  33.         return detik;
  34.     }
  35.     //fungsi menampilkan integer sebagai string
  36.     private String pad (int value){
  37.         return String.format ("%02d",value);
  38.     }
  39.    
  40.     protected void paintComponent(Graphics grafik){
  41.         super.paintComponent(grafik);
  42.         //font waktu dan ukuran waktu 25
  43.         grafik.setFont (new Font ("LCD",Font.PLAIN,25));
  44.         FontMetrics font = getFontMetrics(grafik.getFont());
  45.         String text = pad (getvalue());
  46.         int x = (getWidth () - font.stringWidth(text)) / 2;
  47.         int y = ((getHeight() - font.getHeight())/2 + font.getAscent());
  48.         grafik.drawString(text,x,y);
  49.     }
  50. }
  51.  
Advertisement
Add Comment
Please, Sign In to add comment