Advertisement
mnaufaldillah

DigitPane Tugas 2

Oct 18th, 2020
553
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.19 KB | None | 0 0
  1.  
  2. /**
  3.  * Class untuk menampilkan timer
  4.  */
  5.  
  6. import java.awt.*;  
  7. import javax.swing.*;
  8. public class DigitPane extends JPanel
  9. {
  10.     private int second;
  11.    
  12.     public Dimension getPrefereedSize()
  13.     {
  14.         FontMetrics fm = getFontMetrics(getFont());
  15.         return new Dimension(fm.stringWidth("00"), fm.getHeight());
  16.     }
  17.    
  18.     //function untuk mengubag nilai timer dan mengupdate tampilan
  19.     public void setValue(int newVal)
  20.     {
  21.         if (second != newVal)
  22.         {
  23.             second = newVal;
  24.             repaint();
  25.         }
  26.     }
  27.    
  28.     public int getValue()
  29.     {
  30.         return second;
  31.     }
  32.    
  33.     //function untuk menampilkan integer sebagai string
  34.     private String pad(int value)
  35.     {
  36.         return String.format("%02d", value);
  37.     }
  38.    
  39.     protected void paintComponent(Graphics g)
  40.     {
  41.         super.paintComponent(g);
  42.         g.setFont(new Font("LCD", Font.PLAIN, 24));
  43.         FontMetrics fm = getFontMetrics(g.getFont());
  44.         String text = pad(getValue());
  45.         int x = (getWidth() - fm.stringWidth(text)) / 2;
  46.         int y = ((getHeight() - fm.getHeight()) / 2) + fm.getAscent();
  47.         g.drawString(text, x, y);
  48.     }
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement