This is an example of a Java solution to Exercise[20].
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* The class Timer and its constructor may be
* used to produces instances of objects of
* type Timer. An object of type Timer has
* methods start() and stop() that start
* and stop a timer, a method called reset
* that resets a timer, and a method called
* elaspedTimeInMs that returns an amount of
* elapsed time. If a timer has been started
* but not stopped, then elapsedTimeInMs
* returns the amount of time (in milliseconds)
* that has elapsed since the timer was started.
* If a timer has been started and stopped,
* then elapsedTimeInMs returns the amount of
* time that elapsed between the time the
* timer was started and stopped.
*/
class Timer {
long timeStarted;
long timeStopped;
Timer() {
this.reset();
}
void reset() {
this.timeStarted = 0;
this.timeStopped = 0;
}
void start() {
this.timeStarted = System.currentTimeMillis();
}
void stop() {
this.timeStopped = System.currentTimeMillis();
}
long elapsedTimeInMs() {
if (this.timeStarted != 0) {
if (this.timeStopped != 0) {
return this.timeStopped - this.timeStarted;
}
else {
return System.currentTimeMillis() - this.timeStarted;
}
}
else {
return -1;
}
}
}
/**
* ProgramController is a class that may be
* used to control the execution of a Java
* program. ProgramController objects have
* the following methods:
*
* pause is a method defined in terms
* of a parameter called timeInMs that
* specifies the number of milliseconds
* that the Java program should pause
* and do nothing before resuming. The
* method does not return a value.
*
* quit is a method that, when called, causes
* the Java program to terminate. The
* method is not defined in terms of any
* parameters and does not return a value.
*/
class ProgramController {
/*
* pause does nothing for timeInMs millisends
* before returning control to the code that
* called pause. Internally, pause uses a
* Timer object to "spin" in a trivial while
* loop the specified amount of time before
* completing.
*/
void pause(long timeInMs) {
// Previously missing code starts here:
Timer timer = new Timer();
timer.start();
// Previously missing code ends here.
while (timer.elapsedTimeInMs() < timeInMs);
}
/*
* Calling quit makes the Java program
* terminate.
*/
void quit() {
System.exit(0);
}
}
/**
* This is an extremely scaled-down sketching canvas; with it you
* can only scribble thin black lines. For simplicity the window
* contents are never refreshed when they are uncovered.
*
* This implementation of TrivialSketcher and the corresponding
* code in the static method QuickSketch.main below are based on
* code found at:
*
* http://cs.lmu.edu/~ray/notes/javagraphics/
*/
class TrivialSketcher extends JPanel {
/**
* Keeps track of the last point to draw the next line from.
*/
private Point lastPoint;
/**
* Constructs a panel, registering listeners for the mouse.
*/
public TrivialSketcher() {
// When the mouse button goes down, set the current point
// to the location at which the mouse was pressed.
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
lastPoint = new Point(e.getX(), e.getY());
}
});
// When the mouse is dragged, draw a line from the old point
// to the new point and update the value of lastPoint to hold
// the new current point.
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
Graphics g = getGraphics();
g.drawLine(lastPoint.x, lastPoint.y, e.getX(), e.getY());
lastPoint = new Point(e.getX(), e.getY());
g.dispose();
}
});
}
}
public class QuickSketch {
public static void main(String[] args) {
// Display the sketcher
JFrame frame = new JFrame("Ten Second Sketch");
frame.getContentPane().add(
new TrivialSketcher(), BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setVisible(true);
// The final countdown...
ProgramController controller = new ProgramController();
controller.pause(10000);
controller.quit(); // The end!
}
}