Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import javax.swing.*;
- import java.awt.event.*;
- /**
- * Write a description of class Waktu here.
- * Sebagai tampilan timer
- * @author Thomas Felix
- * @version 19 Oktober 2020
- */
- public class Waktu extends JPanel
- {
- private int detik;
- public Dimension getPreferredSize()
- {
- FontMetrics fontmet = getFontMetrics(getFont());
- return new Dimension(fontmet.stringWidth("00"), fontmet.getHeight());
- }
- public void setNilai(int newVal)
- {
- if (detik != newVal)
- {
- detik = newVal;
- repaint();
- }
- }
- public int getNilai()
- {
- return detik;
- }
- private String pad(int val)
- {
- return String.format("%02d", val);
- }
- protected void paintComponent(Graphics graph)
- {
- super.paintComponent(graph);
- graph.setFont(new Font("LCD", Font.PLAIN, 15));
- FontMetrics fontmet = getFontMetrics(graph.getFont());
- String text = pad(getNilai());
- int x = (getWidth() - fontmet.stringWidth(text)) / 2;
- int y = ((getHeight() - fontmet.getHeight()) / 2) + fontmet.getAscent();
- graph.drawString(text, x, y);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment