thomasfelix

PBO 2

Oct 19th, 2020 (edited)
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.24 KB | None | 0 0
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.awt.event.*;
  4.  
  5. /**
  6.  * Write a description of class Waktu here.
  7.  * Sebagai tampilan timer
  8.  * @author Thomas Felix
  9.  * @version 19 Oktober 2020
  10.  */
  11.  
  12. public class Waktu extends JPanel
  13. {
  14.     private int detik;
  15.    
  16.     public Dimension getPreferredSize()
  17.     {
  18.         FontMetrics fontmet = getFontMetrics(getFont());
  19.         return new Dimension(fontmet.stringWidth("00"), fontmet.getHeight());
  20.     }
  21.    
  22.     public void setNilai(int newVal)
  23.     {
  24.         if (detik != newVal)
  25.         {
  26.             detik = newVal;
  27.             repaint();
  28.         }
  29.     }
  30.    
  31.     public int getNilai()
  32.     {
  33.         return detik;
  34.     }
  35.    
  36.     private String pad(int val)
  37.     {
  38.         return String.format("%02d", val);
  39.     }
  40.    
  41.     protected void paintComponent(Graphics graph)
  42.     {
  43.         super.paintComponent(graph);
  44.         graph.setFont(new Font("LCD", Font.PLAIN, 15));
  45.         FontMetrics fontmet = getFontMetrics(graph.getFont());
  46.        
  47.         String text = pad(getNilai());
  48.         int x = (getWidth() - fontmet.stringWidth(text)) / 2;
  49.         int y = ((getHeight() - fontmet.getHeight()) / 2) + fontmet.getAscent();
  50.        
  51.         graph.drawString(text, x, y);
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment