Guest User

Digit Pane Java

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