BenitoDannes

PBO-3.3-Clock

Mar 11th, 2017
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.21 KB | None | 0 0
  1. /**
  2.  * A very simple GUI for the clock display.
  3.  * In this implementation, time runs at about 3 minutes per second,
  4.  * so that testing the display is a little faster.
  5.  
  6.  * @author Michael Kölling and David J. Barnes
  7.  * @version 2011.07.31
  8.  * Adapted by B. Kleinen to make Model (ClockDisplay) independent from view.
  9.  */
  10.  
  11. import java.awt.*;
  12. import java.awt.event.*;
  13. import javax.swing.*;
  14. import javax.swing.border.*;
  15.  
  16. public class Clock
  17. {
  18.     private JFrame frame;
  19.     private JLabel label;
  20.     private ClockDisplay clock;
  21.     private boolean clockRunning = false;
  22.     private boolean threadRunning = false;
  23.     private TimerThread timerThread;
  24.    
  25.     /**
  26.      * Constructor for objects of class Clock
  27.      */
  28.     public Clock()
  29.     {
  30.         this (new ClockDisplay());
  31.     }
  32.    
  33.     /**
  34.      * Constructor for objects of class Clock
  35.      */
  36.     public Clock (ClockDisplay clock)
  37.     {
  38.         this.clock = clock;
  39.         makeFrame();
  40.         startSyncThread();
  41.     }
  42.    
  43.     public ClockDisplay getClockDisplay()
  44.     {
  45.         return clock;
  46.     }
  47.    
  48.     private void setTimeFromClockDisplay()
  49.     {
  50.         label.setText (clock.getTime());
  51.     }
  52.    
  53.     private void startSyncThread()
  54.     {
  55.         threadRunning = true;
  56.         timerThread = new TimerThread();
  57.         timerThread.start();
  58.     }
  59.    
  60.     private void start()
  61.     {
  62.         clockRunning = true;
  63.     }
  64.    
  65.     private void stop()
  66.     {
  67.         clockRunning = false;
  68.     }
  69.    
  70.     private void step()
  71.     {
  72.         tick();
  73.     }
  74.    
  75.     public void tick()
  76.     {
  77.         clock.timeTick();
  78.         label.setText (clock.getTime());
  79.     }
  80.    
  81.     /**
  82.      * 'About' function: show the 'about' box.
  83.      */
  84.     private void showAbout()
  85.     {
  86.         JOptionPane.showMessageDialog (frame,
  87.             "Clock Version 1.0\n" +
  88.             "A simple interface for the 'Objects First' clock display project",
  89.             "About Clock",
  90.             JOptionPane.INFORMATION_MESSAGE);
  91.     }
  92.    
  93.     /**
  94.      * Quit function: quit the application.
  95.      */
  96.     private void quit()
  97.     {
  98.         System.exit(0);
  99.     }
  100.    
  101.     /**
  102.      * Create the Swing frame and its content.
  103.      */
  104.     private void makeFrame()
  105.     {
  106.         frame = new JFrame ("Clock");
  107.         JPanel contentPane = (JPanel)frame.getContentPane();
  108.         contentPane.setBorder (new EmptyBorder (1, 60, 1, 60));
  109.        
  110.         makeMenuBar (frame);
  111.        
  112.         // Specify the layout manager with nice spacing
  113.         contentPane.setLayout (new BorderLayout (12,12));
  114.        
  115.         // Create the image pane in the center
  116.         label = new JLabel (clock.getTime(), SwingConstants.CENTER);
  117.         Font displayFont = label.getFont().deriveFont (96.0f);
  118.         label.setFont (displayFont);
  119.         //imagePanel.setBorder (new EtchedBorder());
  120.         contentPane.add (label, BorderLayout.CENTER);
  121.        
  122.         // Create the toolbar with the buttons
  123.         JPanel toolbar = new JPanel();
  124.         toolbar.setLayout (new GridLayout (1, 0));
  125.        
  126.         JButton startButton = new JButton ("Start");
  127.         startButton.addActionListener (new ActionListener()
  128.         {
  129.             public void actionPerformed (ActionEvent e)
  130.             {
  131.                 start();
  132.             }
  133.         });
  134.         toolbar.add (startButton);
  135.        
  136.         JButton stopButton = new JButton ("Stop");
  137.         stopButton.addActionListener (new ActionListener()
  138.         {
  139.             public void actionPerformed (ActionEvent e)
  140.             {
  141.                 stop();
  142.             }
  143.         });
  144.         toolbar.add (stopButton);
  145.        
  146.         JButton stepButton = new JButton ("Tick");
  147.         stepButton.addActionListener (new ActionListener()
  148.         {
  149.             public void actionPerformed (ActionEvent e)
  150.             {
  151.                 step();
  152.             }
  153.         });
  154.         toolbar.add (stepButton);
  155.        
  156.         // Add toolbar into panel with flow layout for spacing
  157.         JPanel flow = new JPanel();
  158.         flow.add (toolbar);
  159.        
  160.         contentPane.add (flow, BorderLayout.SOUTH);
  161.        
  162.         // Building is done - arrange the components
  163.         frame.pack();
  164.        
  165.         // Place the frame at the center of the screen and show
  166.         Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  167.         frame.setLocation (d.width/2 - frame.getWidth()/2, d.height/2 - frame.getHeight()/2);
  168.         frame.setVisible (true);
  169.     }
  170.    
  171.     /**
  172.      * Create the main frame's menu bar.
  173.      *
  174.      * @param frame : The frame that the menu bar should be added to.
  175.      */
  176.     private void makeMenuBar (JFrame frame)
  177.     {
  178.         final int SHORTCUT_MASK =
  179.             Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
  180.            
  181.         JMenuBar menubar = new JMenuBar();
  182.         frame.setJMenuBar (menubar);
  183.        
  184.         JMenu menu;
  185.         JMenuItem item;
  186.        
  187.         // Create the File menu
  188.         menu = new JMenu ("File");
  189.         menubar.add (menu);
  190.        
  191.         item = new JMenuItem ("About Clock...");
  192.         item.addActionListener (new ActionListener()
  193.         {
  194.             public void actionPerformed (ActionEvent e)
  195.             {
  196.                 showAbout();
  197.             }
  198.         });
  199.         menu.add (item);
  200.        
  201.         menu.addSeparator();
  202.        
  203.         item = new JMenuItem ("Quit");
  204.         item.setAccelerator (KeyStroke.getKeyStroke (KeyEvent.VK_Q, SHORTCUT_MASK));
  205.         item.addActionListener (new ActionListener()
  206.         {
  207.             public void actionPerformed (ActionEvent e)
  208.             {
  209.                 quit();
  210.             }
  211.         });
  212.         menu.add(item);
  213.     }
  214.    
  215.     class TimerThread extends Thread
  216.     {
  217.         public void run()
  218.         {
  219.             while (threadRunning)
  220.             {
  221.                 if (clockRunning)
  222.                     step();
  223.                 setTimeFromClockDisplay();
  224.                 pause();
  225.             }
  226.         }
  227.        
  228.         private void pause()
  229.         {
  230.             try
  231.             {
  232.                 Thread.sleep (300); // pause for 300 miliseconds
  233.             }
  234.             catch (InterruptedException exc)
  235.             {
  236.             }
  237.         }
  238.     }
  239. }
Advertisement
Add Comment
Please, Sign In to add comment