Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * A very simple GUI for the clock display.
- * In this implementation, time runs at about 3 minutes per second,
- * so that testing the display is a little faster.
- * @author Michael Kölling and David J. Barnes
- * @version 2011.07.31
- * Adapted by B. Kleinen to make Model (ClockDisplay) independent from view.
- */
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import javax.swing.border.*;
- public class Clock
- {
- private JFrame frame;
- private JLabel label;
- private ClockDisplay clock;
- private boolean clockRunning = false;
- private boolean threadRunning = false;
- private TimerThread timerThread;
- /**
- * Constructor for objects of class Clock
- */
- public Clock()
- {
- this (new ClockDisplay());
- }
- /**
- * Constructor for objects of class Clock
- */
- public Clock (ClockDisplay clock)
- {
- this.clock = clock;
- makeFrame();
- startSyncThread();
- }
- public ClockDisplay getClockDisplay()
- {
- return clock;
- }
- private void setTimeFromClockDisplay()
- {
- label.setText (clock.getTime());
- }
- private void startSyncThread()
- {
- threadRunning = true;
- timerThread = new TimerThread();
- timerThread.start();
- }
- private void start()
- {
- clockRunning = true;
- }
- private void stop()
- {
- clockRunning = false;
- }
- private void step()
- {
- tick();
- }
- public void tick()
- {
- clock.timeTick();
- label.setText (clock.getTime());
- }
- /**
- * 'About' function: show the 'about' box.
- */
- private void showAbout()
- {
- JOptionPane.showMessageDialog (frame,
- "Clock Version 1.0\n" +
- "A simple interface for the 'Objects First' clock display project",
- "About Clock",
- JOptionPane.INFORMATION_MESSAGE);
- }
- /**
- * Quit function: quit the application.
- */
- private void quit()
- {
- System.exit(0);
- }
- /**
- * Create the Swing frame and its content.
- */
- private void makeFrame()
- {
- frame = new JFrame ("Clock");
- JPanel contentPane = (JPanel)frame.getContentPane();
- contentPane.setBorder (new EmptyBorder (1, 60, 1, 60));
- makeMenuBar (frame);
- // Specify the layout manager with nice spacing
- contentPane.setLayout (new BorderLayout (12,12));
- // Create the image pane in the center
- label = new JLabel (clock.getTime(), SwingConstants.CENTER);
- Font displayFont = label.getFont().deriveFont (96.0f);
- label.setFont (displayFont);
- //imagePanel.setBorder (new EtchedBorder());
- contentPane.add (label, BorderLayout.CENTER);
- // Create the toolbar with the buttons
- JPanel toolbar = new JPanel();
- toolbar.setLayout (new GridLayout (1, 0));
- JButton startButton = new JButton ("Start");
- startButton.addActionListener (new ActionListener()
- {
- public void actionPerformed (ActionEvent e)
- {
- start();
- }
- });
- toolbar.add (startButton);
- JButton stopButton = new JButton ("Stop");
- stopButton.addActionListener (new ActionListener()
- {
- public void actionPerformed (ActionEvent e)
- {
- stop();
- }
- });
- toolbar.add (stopButton);
- JButton stepButton = new JButton ("Tick");
- stepButton.addActionListener (new ActionListener()
- {
- public void actionPerformed (ActionEvent e)
- {
- step();
- }
- });
- toolbar.add (stepButton);
- // Add toolbar into panel with flow layout for spacing
- JPanel flow = new JPanel();
- flow.add (toolbar);
- contentPane.add (flow, BorderLayout.SOUTH);
- // Building is done - arrange the components
- frame.pack();
- // Place the frame at the center of the screen and show
- Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
- frame.setLocation (d.width/2 - frame.getWidth()/2, d.height/2 - frame.getHeight()/2);
- frame.setVisible (true);
- }
- /**
- * Create the main frame's menu bar.
- *
- * @param frame : The frame that the menu bar should be added to.
- */
- private void makeMenuBar (JFrame frame)
- {
- final int SHORTCUT_MASK =
- Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
- JMenuBar menubar = new JMenuBar();
- frame.setJMenuBar (menubar);
- JMenu menu;
- JMenuItem item;
- // Create the File menu
- menu = new JMenu ("File");
- menubar.add (menu);
- item = new JMenuItem ("About Clock...");
- item.addActionListener (new ActionListener()
- {
- public void actionPerformed (ActionEvent e)
- {
- showAbout();
- }
- });
- menu.add (item);
- menu.addSeparator();
- item = new JMenuItem ("Quit");
- item.setAccelerator (KeyStroke.getKeyStroke (KeyEvent.VK_Q, SHORTCUT_MASK));
- item.addActionListener (new ActionListener()
- {
- public void actionPerformed (ActionEvent e)
- {
- quit();
- }
- });
- menu.add(item);
- }
- class TimerThread extends Thread
- {
- public void run()
- {
- while (threadRunning)
- {
- if (clockRunning)
- step();
- setTimeFromClockDisplay();
- pause();
- }
- }
- private void pause()
- {
- try
- {
- Thread.sleep (300); // pause for 300 miliseconds
- }
- catch (InterruptedException exc)
- {
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment