7

Date&Time Clock

Créez un dossier dt.

  1. package dt;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Dimension;
  5. import java.awt.Font;
  6. import java.awt.FontMetrics;
  7. import java.awt.Graphics;
  8. import java.awt.Graphics2D;
  9. import java.awt.RenderingHints;
  10.  
  11. import java.awt.event.MouseAdapter;
  12. import java.awt.event.MouseEvent;
  13.  
  14. import java.util.Calendar;
  15.  
  16. import javax.swing.JComponent;
  17.  
  18. @SuppressWarnings("serial")
  19. public class Clock implements Runnable {
  20.     private volatile Thread thread = null;
  21.  
  22.     private long lasttime = System.currentTimeMillis();
  23.  
  24.     private boolean stopped = true;
  25.  
  26.     private ClockDelegate delegate;
  27.  
  28.     private DigitalView view;
  29.  
  30.     private int hh, mm, ss;
  31.  
  32.     Clock() {
  33.         Calendar now = Calendar.getInstance();
  34.  
  35.         this.hh = now.get(Calendar.HOUR_OF_DAY);
  36.         this.mm = now.get(Calendar.MINUTE);
  37.         this.ss = now.get(Calendar.SECOND);
  38.     }
  39.  
  40.     Clock(int hh, int mm, int ss) {
  41.         initTime(hh, mm, ss);
  42.     }
  43.  
  44.     public boolean isStopped() {
  45.         return stopped;
  46.     }
  47.  
  48.     public void setDelegate(ClockDelegate d) {
  49.         this.delegate = d;
  50.     }
  51.  
  52.     public ClockDelegate getDelegate() {
  53.         return this.delegate;
  54.     }
  55.  
  56.     public void setTime(int hh, int mm, int ss) {
  57.         initTime(hh, mm, ss);
  58.  
  59.         lasttime = System.currentTimeMillis(); // reset last time internal
  60.         // clock was changed
  61.  
  62.         if (view != null)
  63.             view.repaint();
  64.     }
  65.  
  66.     public void resetTime() {
  67.         Calendar now = Calendar.getInstance();
  68.  
  69.         int hh = now.get(Calendar.HOUR_OF_DAY);
  70.         int mm = now.get(Calendar.MINUTE);
  71.         int ss = now.get(Calendar.SECOND);
  72.  
  73.         setTime(hh, mm, ss);
  74.     }
  75.  
  76.     public int getHours() {
  77.         return this.hh;
  78.     }
  79.  
  80.     public int getMinutes() {
  81.         return this.mm;
  82.     }
  83.  
  84.     public int getSeconds() {
  85.         return this.ss;
  86.     }
  87.  
  88.     private void initTime(int hh, int mm, int ss) {
  89.         if (hh < 0)
  90.             hh = 0;
  91.         else if (hh > 23)
  92.             hh = 23;
  93.  
  94.         this.hh = hh;
  95.  
  96.         if (mm < 0)
  97.             mm = 0;
  98.         else if (mm > 59)
  99.             mm = 59;
  100.  
  101.         this.mm = mm;
  102.  
  103.         if (ss < 0)
  104.             ss = 0;
  105.         else if (ss > 59)
  106.             ss = 59;
  107.  
  108.         this.ss = ss;
  109.     }
  110.  
  111.     private void addTime(int nsecs) {
  112.         int secs = hh * 3600 + mm * 60 + ss + nsecs;
  113.  
  114.         hh = secs / (60 * 60);
  115.         secs -= hh * (60 * 60);
  116.         hh %= 24;
  117.         mm = secs / 60;
  118.         secs -= mm * 60;
  119.         ss = secs;
  120.     }
  121.  
  122.     public JComponent createComponent() {
  123.         view = new DigitalView();
  124.  
  125.         return view;
  126.     }
  127.  
  128.     public void start() {
  129.         if (stopped) {
  130.             if (thread == null) {
  131.                 thread = new Thread(this, "Clock");
  132.                 thread.start();
  133.             }
  134.             stopped = false;
  135.         }
  136.     }
  137.  
  138.     public void stop() {
  139.         if (!stopped) {
  140.             stopped = true;
  141.             thread = null;
  142.         }
  143.     }
  144.  
  145.     public void run() {
  146.         while (this.thread == Thread.currentThread()) {
  147.             if (view != null)
  148.                 view.repaint();
  149.             try {
  150.                 long timenow = System.currentTimeMillis();
  151.  
  152.                 int delay = (int) (timenow - lasttime);
  153.                 int nsecs = delay / 1000;
  154.                 int nmillisecs = delay % 1000;
  155.  
  156.                 addTime(nsecs);
  157.  
  158.                 lasttime += nsecs * 1000;
  159.  
  160.                 Thread.sleep(1000 - nmillisecs);
  161.             }
  162.             catch (InterruptedException e) {
  163.             }
  164.         }
  165.     }
  166.  
  167.     @SuppressWarnings("serial")
  168.     private class DigitalView extends JComponent {
  169.         protected Color fg = Color.BLUE, bg = null;
  170.  
  171.         protected Font font = new Font("Sans-Serif", Font.BOLD, 36);
  172.  
  173.         DigitalView() {
  174.             FontMetrics fm = getFontMetrics(font);
  175.             Dimension size = new Dimension(fm.stringWidth("00:00:00"), fm.getHeight());
  176.  
  177.             setPreferredSize(size);
  178.             setMinimumSize(size);
  179.  
  180.             addMouseListener(new MouseAdapter() {
  181.                 public void mouseClicked(MouseEvent e) {
  182.                     if (stopped) {
  183.                         start();
  184.                         if (delegate != null)
  185.                             delegate.clockStarted(Clock.this);
  186.                     }
  187.                     else {
  188.                         stop();
  189.                         if (delegate != null)
  190.                             delegate.clockStopped(Clock.this);
  191.                     }
  192.                 }
  193.             });
  194.         }
  195.  
  196.         public void paintComponent(Graphics g) {
  197.             Dimension size = getSize();
  198.  
  199.             Graphics2D g2d = (Graphics2D) g.create();
  200.  
  201.             g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
  202.  
  203.             java.util.Formatter formatter = new java.util.Formatter();
  204.             String s = formatter.format("%02d:%02d:%02d", hh, mm, ss).toString();
  205.  
  206.             g2d.setFont(font);
  207.  
  208.             FontMetrics fm = g2d.getFontMetrics();
  209.  
  210.             int x = (size.width - fm.stringWidth(s)) / 2;
  211.             int y = (size.height - fm.getHeight()) / 2 + fm.getAscent();
  212.  
  213.             if (bg != null) {
  214.                 g2d.setColor(bg);
  215.                 g2d.fillRect(0, 0, size.width, size.height);
  216.             }
  217.  
  218.             g2d.setColor(fg);
  219.             g2d.drawString(s, x, y);
  220.         }
  221.     }
  222. }
  1. package dt;
  2.  
  3. public interface ClockDelegate {
  4.     void clockStarted(Clock clk);
  5.  
  6.     void clockStopped(Clock clk);
  7. }
  1. package dt;
  2.  
  3. import java.awt.Dimension;
  4. import java.awt.Toolkit;
  5.  
  6. import java.awt.GridLayout;
  7.  
  8. import javax.swing.JFrame;
  9.  
  10. public class ClockTester implements ClockDelegate {
  11.  
  12.     public JFrame createFrame(String title) {
  13.         Clock clock1 = new Clock();
  14.         clock1.setDelegate(this);
  15.         clock1.start();
  16.  
  17.         Clock clock2 = new Clock(0, 0, 0);
  18.         clock2.setDelegate(this);
  19.         clock2.start();
  20.         Clock clock3 = new Clock(0, 0, 0);
  21.         clock3.setDelegate(this);
  22.         clock3.start();
  23.  
  24.         Clock clock4 = new Clock(23, 59, 50);
  25.         clock4.setDelegate(this);
  26.         clock4.start();
  27.  
  28.         JFrame frame = new JFrame(title);
  29.         frame.setLayout(new GridLayout(0, 1));
  30.  
  31.         frame.add(clock1.createComponent());
  32.         frame.add(clock2.createComponent());
  33.         frame.add(clock3.createComponent());
  34.         frame.add(clock4.createComponent());
  35.  
  36.         return frame;
  37.     }
  38.  
  39.     public void clockStarted(Clock clk) {
  40.         System.out.println("[started]");
  41.     }
  42.  
  43.     public void clockStopped(Clock clk) {
  44.         System.out.println("[stopped]");
  45.     }
  46.  
  47.     private static void createAndShowGUI() {
  48.         JFrame.setDefaultLookAndFeelDecorated(true);
  49.  
  50.         ClockTester tester = new ClockTester();
  51.  
  52.         JFrame frame = tester.createFrame("Clock");
  53.  
  54.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  55.  
  56.         frame.pack();
  57.  
  58.         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  59.  
  60.         int width = screenSize.width / 3;
  61.         int height = screenSize.height / 4;
  62.         int x = (screenSize.width - width) / 2;
  63.         int y = (screenSize.height - height) / 3;
  64.  
  65.         frame.setLocation(x, y);
  66.         frame.setResizable(true);
  67.         frame.setVisible(true);
  68.     }
  69.  
  70.     public static void main(String[] args) {
  71.         javax.swing.SwingUtilities.invokeLater(new Runnable() {
  72.             public void run() {
  73.                 createAndShowGUI();
  74.             }
  75.         });
  76.     }
  77. }

Commentaires

Votre commentaire :
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip aide 2000

Entrez un maximum de 2000 caractères.
Améliorez la présentation de votre texte avec les balises de formatage suivantes :
[p]paragraphe[/p], [b]gras[/b], [i]italique[/i], [u]souligné[/u], [s]barré[/s], [quote]citation[/quote], [pre]tel quel[/pre], [br]à la ligne,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]commande[/code], [code=langage]code source en c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].