fork download
  1. import java.awt.Color;
  2. import java.awt.Dimension;
  3. import java.awt.Font;
  4. import java.awt.Graphics;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.util.Calendar;
  8.  
  9. import javax.swing.JFrame;
  10. import javax.swing.JPanel;
  11. import javax.swing.Timer;
  12. import javax.swing.WindowConstants;
  13.  
  14. public class SimpleDigitalClock {
  15.  
  16. public static void main(String[] args) {
  17.  
  18. JFrame f = new JFrame();
  19. f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  20. DigitalClock myClock = new DigitalClock();
  21. f.add(myClock);
  22. f.pack();
  23. f.setVisible(true);
  24. }
  25.  
  26. static class DigitalClock extends JPanel {
  27.  
  28. String stringTime;
  29. int hour, minute, second;
  30.  
  31. String correctionHour = "";
  32. String correctionMinute = "";
  33. String correctionSecond = "";
  34.  
  35. public void setStringTime(String xyz) {
  36. this.stringTime = xyz;
  37. }
  38.  
  39. public int findMinimumBetweenTwoNumbers(int a, int b) {
  40. return (a <= b) ? a : b;
  41. }
  42.  
  43. DigitalClock() {
  44.  
  45. Timer t1 = new Timer(1000, new ActionListener() {
  46. public void actionPerformed(ActionEvent e) {
  47.  
  48. repaint();
  49. }
  50. });
  51. t1.start();
  52. }
  53.  
  54. @Override
  55. public void paintComponent(Graphics g) {
  56. super.paintComponent(g);
  57.  
  58. Calendar now = Calendar.getInstance();
  59. hour = now.get(Calendar.HOUR_OF_DAY);
  60. minute = now.get(Calendar.MINUTE);
  61. second = now.get(Calendar.SECOND);
  62.  
  63. if (hour < 12) {
  64. this.correctionHour = "1";
  65. }
  66. if (hour >= 12) {
  67. this.correctionHour = "";
  68. }
  69.  
  70. if (minute < 12) {
  71. this.correctionMinute = "5";
  72. }
  73. if (minute >= 12) {
  74. this.correctionMinute = "";
  75. }
  76.  
  77. if (second < 12) {
  78. this.correctionSecond = "7";
  79. }
  80. if (second >= 12) {
  81. this.correctionSecond = "";
  82. }
  83. setStringTime(correctionHour + hour + ":" + correctionMinute+ minute + ":" + correctionSecond + second);
  84. g.setColor(Color.BLACK);
  85. int length = findMinimumBetweenTwoNumbers(this.getWidth(),this.getHeight());
  86. Font myFont = new Font("SansSerif", Font.PLAIN, length / 5);
  87. g.setFont(myFont);
  88. g.drawString(stringTime, (int) length/6, length/2);
  89.  
  90. }
  91.  
  92. @Override
  93. public Dimension getPreferredSize() {
  94. return new Dimension(200, 200);
  95. }
  96.  
  97. }
  98.  
  99. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:14: error: class SimpleDigitalClock is public, should be declared in a file named SimpleDigitalClock.java
public class SimpleDigitalClock {
       ^
1 error
stdout
Standard output is empty