/* * 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()); } public void setValue(int newVal) { if (second != newVal) { second = newVal; repaint(); } } public int getValue() { return second; } 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); } }