Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Class untuk menampilkan timer
- */
- import java.awt.*;
- import javax.swing.*;
- public class DigitPane extends JPanel {
- private int second;
- public Dimension getPreferredSize(){
- FontMetrics fm = getFontMetrics(getFont());
- return new Dimension(fm.stringWidth("00"), fm.getHeight());
- }
- // function untuk mengubah nilai timer dan mengupdate tampilan
- public void setValue(int newVal){
- if (second != newVal) {
- second = newVal;
- repaint();
- }
- }
- public int getValue(){
- return second;
- }
- // function u/ menampilkan integer sebagai string
- private String pad(int value){
- return String.format("%02d", value);
- }
- protected void paintComponent(Graphics g){
- super.paintComponent(g);
- g.setFont(new Font("LCD", Font.PLAIN, 24));
- FontMetrics fm = getFontMetrics(g.getFont());
- String text = pad(getValue());
- int x = (getWidth() - fm.stringWidth(text)) / 2;
- int y = ((getHeight()- fm.getHeight()) / 2) + fm.getAscent();
- g.drawString(text, x, y);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment