first commit
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=Clock()
|
||||
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ Clock\n
|
||||
comment1.params=
|
||||
comment1.target=void\ start()
|
||||
comment1.text=\n\ \n
|
||||
comment2.params=
|
||||
comment2.target=void\ stop()
|
||||
comment2.text=\n\ \n
|
||||
comment3.params=
|
||||
comment3.target=void\ step()
|
||||
comment3.text=\n\ \n
|
||||
comment4.params=
|
||||
comment4.target=void\ showAbout()
|
||||
comment4.text=\n\ 'About'\ function\:\ show\ the\ 'about'\ box.\n
|
||||
comment5.params=
|
||||
comment5.target=void\ quit()
|
||||
comment5.text=\n\ Quit\ function\:\ quit\ the\ application.\n
|
||||
comment6.params=
|
||||
comment6.target=void\ makeFrame()
|
||||
comment6.text=\n\ Create\ the\ Swing\ frame\ and\ its\ content.\n
|
||||
comment7.params=frame
|
||||
comment7.target=void\ makeMenuBar(javax.swing.JFrame)
|
||||
comment7.text=\n\ Create\ the\ main\ frame's\ menu\ bar.\n\ \n\ @param\ frame\ \ \ The\ frame\ that\ the\ menu\ bar\ should\ be\ added\ to.\n
|
||||
numComments=8
|
@@ -0,0 +1,182 @@
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import javax.swing.*;
|
||||
import javax.swing.border.*;
|
||||
|
||||
/**
|
||||
* A very simple GUI (graphical user interface) for the clock display.
|
||||
* In this implementation, time runs at about 3 minutes per second, so that
|
||||
* testing the display is a little quicker.
|
||||
*
|
||||
* @author Michael Kölling and David J. Barnes
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class Clock
|
||||
{
|
||||
private JFrame frame;
|
||||
private JLabel label;
|
||||
private ClockDisplay clock;
|
||||
private boolean clockRunning = false;
|
||||
private TimerThread timerThread;
|
||||
|
||||
/**
|
||||
* Constructor for objects of class Clock
|
||||
*/
|
||||
public Clock()
|
||||
{
|
||||
makeFrame();
|
||||
clock = new ClockDisplay();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private void start()
|
||||
{
|
||||
clockRunning = true;
|
||||
timerThread = new TimerThread();
|
||||
timerThread.start();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private void stop()
|
||||
{
|
||||
clockRunning = false;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private void step()
|
||||
{
|
||||
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("00:00", 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(e -> start());
|
||||
toolbar.add(startButton);
|
||||
|
||||
JButton stopButton = new JButton("Stop");
|
||||
stopButton.addActionListener(e -> stop());
|
||||
toolbar.add(stopButton);
|
||||
|
||||
JButton stepButton = new JButton("Step");
|
||||
stepButton.addActionListener(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(e -> showAbout());
|
||||
menu.add(item);
|
||||
|
||||
menu.addSeparator();
|
||||
|
||||
item = new JMenuItem("Quit");
|
||||
item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, SHORTCUT_MASK));
|
||||
item.addActionListener(e -> quit());
|
||||
menu.add(item);
|
||||
}
|
||||
|
||||
class TimerThread extends Thread
|
||||
{
|
||||
public void run()
|
||||
{
|
||||
while (clockRunning) {
|
||||
step();
|
||||
pause();
|
||||
}
|
||||
}
|
||||
|
||||
private void pause()
|
||||
{
|
||||
try {
|
||||
Thread.sleep(300); // pause for 300 milliseconds
|
||||
}
|
||||
catch (InterruptedException exc) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,20 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=ClockDisplay()
|
||||
comment0.text=\n\ Constructor\ for\ ClockDisplay\ objects.\ This\ constructor\ \n\ creates\ a\ new\ clock\ set\ at\ 00\:00.\n
|
||||
comment1.params=hour\ minute
|
||||
comment1.target=ClockDisplay(int,\ int)
|
||||
comment1.text=\n\ Constructor\ for\ ClockDisplay\ objects.\ This\ constructor\n\ creates\ a\ new\ clock\ set\ at\ the\ time\ specified\ by\ the\ \n\ parameters.\n
|
||||
comment2.params=
|
||||
comment2.target=void\ timeTick()
|
||||
comment2.text=\n\ This\ method\ should\ get\ called\ once\ every\ minute\ -\ it\ makes\n\ the\ clock\ display\ go\ one\ minute\ forward.\n
|
||||
comment3.params=hour\ minute
|
||||
comment3.target=void\ setTime(int,\ int)
|
||||
comment3.text=\n\ Set\ the\ time\ of\ the\ display\ to\ the\ specified\ hour\ and\n\ minute.\n
|
||||
comment4.params=
|
||||
comment4.target=java.lang.String\ getTime()
|
||||
comment4.text=\n\ Return\ the\ current\ time\ of\ this\ display\ in\ the\ format\ HH\:MM.\n
|
||||
comment5.params=
|
||||
comment5.target=void\ updateDisplay()
|
||||
comment5.text=\n\ Update\ the\ internal\ string\ that\ represents\ the\ display.\n
|
||||
numComments=6
|
@@ -0,0 +1,84 @@
|
||||
|
||||
/**
|
||||
* The ClockDisplay class implements a digital clock display for a
|
||||
* European-style 24 hour clock. The clock shows hours and minutes. The
|
||||
* range of the clock is 00:00 (midnight) to 23:59 (one minute before
|
||||
* midnight).
|
||||
*
|
||||
* The clock display receives "ticks" (via the timeTick method) every minute
|
||||
* and reacts by incrementing the display. This is done in the usual clock
|
||||
* fashion: the hour increments when the minutes roll over to zero.
|
||||
*
|
||||
* @author Michael Kölling and David J. Barnes
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class ClockDisplay
|
||||
{
|
||||
private NumberDisplay hours;
|
||||
private NumberDisplay minutes;
|
||||
private String displayString; // simulates the actual display
|
||||
|
||||
/**
|
||||
* Constructor for ClockDisplay objects. This constructor
|
||||
* creates a new clock set at 00:00.
|
||||
*/
|
||||
public ClockDisplay()
|
||||
{
|
||||
hours = new NumberDisplay(24);
|
||||
minutes = new NumberDisplay(60);
|
||||
updateDisplay();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for ClockDisplay objects. This constructor
|
||||
* creates a new clock set at the time specified by the
|
||||
* parameters.
|
||||
*/
|
||||
public ClockDisplay(int hour, int minute)
|
||||
{
|
||||
hours = new NumberDisplay(24);
|
||||
minutes = new NumberDisplay(60);
|
||||
setTime(hour, minute);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method should get called once every minute - it makes
|
||||
* the clock display go one minute forward.
|
||||
*/
|
||||
public void timeTick()
|
||||
{
|
||||
minutes.increment();
|
||||
if(minutes.getValue() == 0) { // it just rolled over!
|
||||
hours.increment();
|
||||
}
|
||||
updateDisplay();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the time of the display to the specified hour and
|
||||
* minute.
|
||||
*/
|
||||
public void setTime(int hour, int minute)
|
||||
{
|
||||
hours.setValue(hour);
|
||||
minutes.setValue(minute);
|
||||
updateDisplay();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the current time of this display in the format HH:MM.
|
||||
*/
|
||||
public String getTime()
|
||||
{
|
||||
return displayString;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the internal string that represents the display.
|
||||
*/
|
||||
private void updateDisplay()
|
||||
{
|
||||
displayString = hours.getDisplayValue() + ":" +
|
||||
minutes.getDisplayValue();
|
||||
}
|
||||
}
|
@@ -0,0 +1,17 @@
|
||||
#BlueJ class context
|
||||
comment0.params=rollOverLimit
|
||||
comment0.target=NumberDisplay(int)
|
||||
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ NumberDisplay.\n\ Set\ the\ limit\ at\ which\ the\ display\ rolls\ over.\n
|
||||
comment1.params=
|
||||
comment1.target=int\ getValue()
|
||||
comment1.text=\n\ Return\ the\ current\ value.\n
|
||||
comment2.params=
|
||||
comment2.target=java.lang.String\ getDisplayValue()
|
||||
comment2.text=\n\ Return\ the\ display\ value\ (that\ is,\ the\ current\ value\ as\ a\ two-digit\n\ String.\ If\ the\ value\ is\ less\ than\ ten,\ it\ will\ be\ padded\ with\ a\ leading\n\ zero).\n
|
||||
comment3.params=replacementValue
|
||||
comment3.target=void\ setValue(int)
|
||||
comment3.text=\n\ Set\ the\ value\ of\ the\ display\ to\ the\ new\ specified\ value.\ If\ the\ new\n\ value\ is\ less\ than\ zero\ or\ over\ the\ limit,\ do\ nothing.\n
|
||||
comment4.params=
|
||||
comment4.target=void\ increment()
|
||||
comment4.text=\n\ Increment\ the\ display\ value\ by\ one,\ rolling\ over\ to\ zero\ if\ the\n\ limit\ is\ reached.\n
|
||||
numComments=5
|
@@ -0,0 +1,70 @@
|
||||
|
||||
/**
|
||||
* The NumberDisplay class represents a digital number display that can hold
|
||||
* values from zero to a given limit. The limit can be specified when creating
|
||||
* the display. The values range from zero (inclusive) to limit-1. If used,
|
||||
* for example, for the seconds on a digital clock, the limit would be 60,
|
||||
* resulting in display values from 0 to 59. When incremented, the display
|
||||
* automatically rolls over to zero when reaching the limit.
|
||||
*
|
||||
* @author Michael Kölling and David J. Barnes
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class NumberDisplay
|
||||
{
|
||||
private int limit;
|
||||
private int value;
|
||||
|
||||
/**
|
||||
* Constructor for objects of class NumberDisplay.
|
||||
* Set the limit at which the display rolls over.
|
||||
*/
|
||||
public NumberDisplay(int rollOverLimit)
|
||||
{
|
||||
limit = rollOverLimit;
|
||||
value = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the current value.
|
||||
*/
|
||||
public int getValue()
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the display value (that is, the current value as a two-digit
|
||||
* String. If the value is less than ten, it will be padded with a leading
|
||||
* zero).
|
||||
*/
|
||||
public String getDisplayValue()
|
||||
{
|
||||
if(value < 10) {
|
||||
return "0" + value;
|
||||
}
|
||||
else {
|
||||
return "" + value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value of the display to the new specified value. If the new
|
||||
* value is less than zero or over the limit, do nothing.
|
||||
*/
|
||||
public void setValue(int replacementValue)
|
||||
{
|
||||
if((replacementValue >= 0) && (replacementValue < limit)) {
|
||||
value = replacementValue;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Increment the display value by one, rolling over to zero if the
|
||||
* limit is reached.
|
||||
*/
|
||||
public void increment()
|
||||
{
|
||||
value = (value + 1) % limit;
|
||||
}
|
||||
}
|
@@ -0,0 +1,16 @@
|
||||
Project: clock-display-with-GUI
|
||||
Authors: Michael Kölling and David J. Barnes
|
||||
|
||||
This project is part of the material for the book
|
||||
|
||||
Objects First with Java - A Practical Introduction using BlueJ
|
||||
Sixth edition
|
||||
David J. Barnes and Michael Kölling
|
||||
Pearson Education, 2016
|
||||
|
||||
It is discussed in chapter 3.
|
||||
|
||||
This project adds a simple GUI (graphical user interface) to the
|
||||
clock display project. When the clock is run, it runs at approximately
|
||||
3 minutes per second, so that the display can be tested a little more
|
||||
easily.
|
@@ -0,0 +1,61 @@
|
||||
#BlueJ package file
|
||||
dependency1.from=Clock
|
||||
dependency1.to=ClockDisplay
|
||||
dependency1.type=UsesDependency
|
||||
dependency2.from=ClockDisplay
|
||||
dependency2.to=NumberDisplay
|
||||
dependency2.type=UsesDependency
|
||||
objectbench.height=100
|
||||
objectbench.width=484
|
||||
package.editor.height=394
|
||||
package.editor.width=681
|
||||
package.editor.x=70
|
||||
package.editor.y=80
|
||||
package.numDependencies=2
|
||||
package.numTargets=3
|
||||
package.showExtends=true
|
||||
package.showUses=true
|
||||
project.charset=UTF-8
|
||||
readme.editor.height=606
|
||||
readme.editor.width=838
|
||||
readme.editor.x=53
|
||||
readme.editor.y=67
|
||||
target1.editor.height=700
|
||||
target1.editor.width=900
|
||||
target1.editor.x=60
|
||||
target1.editor.y=96
|
||||
target1.height=50
|
||||
target1.name=ClockDisplay
|
||||
target1.naviview.expanded=true
|
||||
target1.showInterface=false
|
||||
target1.type=ClassTarget
|
||||
target1.typeParameters=
|
||||
target1.width=110
|
||||
target1.x=230
|
||||
target1.y=140
|
||||
target2.editor.height=700
|
||||
target2.editor.width=900
|
||||
target2.editor.x=145
|
||||
target2.editor.y=101
|
||||
target2.height=50
|
||||
target2.name=NumberDisplay
|
||||
target2.naviview.expanded=true
|
||||
target2.showInterface=false
|
||||
target2.type=ClassTarget
|
||||
target2.typeParameters=
|
||||
target2.width=120
|
||||
target2.x=350
|
||||
target2.y=230
|
||||
target3.editor.height=843
|
||||
target3.editor.width=976
|
||||
target3.editor.x=101
|
||||
target3.editor.y=35
|
||||
target3.height=50
|
||||
target3.name=Clock
|
||||
target3.naviview.expanded=true
|
||||
target3.showInterface=false
|
||||
target3.type=ClassTarget
|
||||
target3.typeParameters=
|
||||
target3.width=80
|
||||
target3.x=110
|
||||
target3.y=50
|
Reference in New Issue
Block a user