fajarbaskoro80

Clock3

Sep 16th, 2025
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Date;
  5. import java.util.Random;
  6.  
  7. public class ModernClockDisplay extends JFrame {
  8. private JLabel timeLabel;
  9. private JLabel dateLabel;
  10. private JLabel tempLabel;
  11. private JLabel iconLabel;
  12. private Random random;
  13.  
  14. public ModernClockDisplay() {
  15. setTitle("Modern Clock Display");
  16. setSize(500, 300);
  17. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  18. setLayout(new BorderLayout());
  19.  
  20. // Panel utama dengan background gradasi
  21. JPanel mainPanel = new JPanel() {
  22. @Override
  23. protected void paintComponent(Graphics g) {
  24. super.paintComponent(g);
  25. Graphics2D g2d = (Graphics2D) g;
  26. GradientPaint gradient = new GradientPaint(
  27. 0, 0, new Color(30, 30, 60),
  28. 0, getHeight(), new Color(10, 10, 30)
  29. );
  30. g2d.setPaint(gradient);
  31. g2d.fillRect(0, 0, getWidth(), getHeight());
  32. }
  33. };
  34. mainPanel.setLayout(new GridLayout(4, 1));
  35. add(mainPanel, BorderLayout.CENTER);
  36.  
  37. // Ikon cuaca
  38. iconLabel = new JLabel("☀", SwingConstants.CENTER); // default matahari
  39. iconLabel.setFont(new Font("Segoe UI Emoji", Font.PLAIN, 48));
  40. iconLabel.setForeground(Color.YELLOW);
  41.  
  42. // Jam digital
  43. timeLabel = new JLabel("", SwingConstants.CENTER);
  44. timeLabel.setFont(new Font("Consolas", Font.BOLD, 48));
  45. timeLabel.setForeground(Color.CYAN);
  46.  
  47. // Tanggal
  48. dateLabel = new JLabel("", SwingConstants.CENTER);
  49. dateLabel.setFont(new Font("Arial", Font.PLAIN, 22));
  50. dateLabel.setForeground(Color.WHITE);
  51.  
  52. // Suhu
  53. tempLabel = new JLabel("", SwingConstants.CENTER);
  54. tempLabel.setFont(new Font("Arial", Font.ITALIC, 20));
  55. tempLabel.setForeground(Color.ORANGE);
  56.  
  57. // Tambahkan komponen ke panel
  58. mainPanel.add(iconLabel);
  59. mainPanel.add(timeLabel);
  60. mainPanel.add(dateLabel);
  61. mainPanel.add(tempLabel);
  62.  
  63. random = new Random();
  64.  
  65. // Timer update tiap detik
  66. Timer timer = new Timer(1000, e -> updateDisplay());
  67. timer.start();
  68. }
  69.  
  70. private void updateDisplay() {
  71. // Format jam
  72. SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
  73. timeLabel.setText(timeFormat.format(new Date()));
  74.  
  75. // Format tanggal
  76. SimpleDateFormat dateFormat = new SimpleDateFormat("EEEE, dd MMMM yyyy");
  77. dateLabel.setText(dateFormat.format(new Date()));
  78.  
  79. // Suhu random
  80. double temp = 20 + (10 * random.nextDouble());
  81. tempLabel.setText(String.format("Suhu Ruangan: %.2f °C", temp));
  82.  
  83. // Ganti ikon sesuai suhu
  84. if (temp < 23) {
  85. iconLabel.setText("❄"); // dingin
  86. iconLabel.setForeground(Color.CYAN);
  87. } else if (temp < 27) {
  88. iconLabel.setText("☀"); // normal
  89. iconLabel.setForeground(Color.YELLOW);
  90. } else {
  91. iconLabel.setText("🔥"); // panas
  92. iconLabel.setForeground(Color.RED);
  93. }
  94. }
  95.  
  96. public static void main(String[] args) {
  97. SwingUtilities.invokeLater(() -> {
  98. ModernClockDisplay app = new ModernClockDisplay();
  99. app.setVisible(true);
  100. });
  101. }
  102. }
  103.  
Advertisement
Add Comment
Please, Sign In to add comment