Find Us On: Facebook Twitter Rss Feed Blog
Need To Register? Sign Up Login

Java - CountDown

craigster
craigster 


Count Down Clocks are a standard thing to animate—they change at regular intervals. In this lab, you will write an applet that displays a simple “clock” that just counts down from 10. Three files are provided: a. DigitalDisplay.java creates the clock. b. CountDown.java contains the main program of creating the applet window. c. CountDownPanel.java contains the detail of the animation. Copy these files to your directory, compile and run the program. It should just display the clock with the number 10. Now do the following to make the clock count down, stop when it hits 0, and reset to 10 if the user clicks the mouse. All the work should be done within CountDownPanel.java. 1. Add an inner class named CountListener that implements ActionListener. In actionPerformed method (refer our in-class example ReboundPanel), • Decrement the clock value. To do this use the decrement method in the DigitalDisplay class. (Look at its header in DigitalDisplay about how to use it.) • If the display value of the clock is negative, stop the timer; otherwise repaint the panel. 2. In the constructor, set up the timer (refer to ReboundPanel example again). Do not forget to start the timer at the end of the constructor. 3. Compile and run the program to see if it works. 4. (Optional Practice) Now add code to let a mouse click have some control over the clock. In particular, if the clock is running when the mouse is clicked the clock should be stopped (stop the timer). If the clock is not running, it should be reset to 10 and started. To add the ability of the panel to respond to mouse clicks, do the following: • Add an inner class that implements the MouseListener interface. In mouseClicked, if the timer is running, stop it; otherwise reset the clock to 10 (there is a method in the DigitalDisplay class to do this), start the timer, and repaint the applet. The bodies of the other methods in the MouseListener interface will be empty. • Add a second parameter of type JApplet to the constructor for CountDownPanel. In your constructor, add the mouse listener to the applet. • Modify the program CountDown.java to send the applet (this) as the second parameter for the CountDownPanel constructor. Attachments Tags Clarifications

Answers

CountDown - Solution

mariobeka
mariobeka 



Below you will find the implemented code explained by comments. Also some screen shots of the program running. Attachments
Screen Shot 2014-04-29 at 11.26.06 PM.png (15 K) Download Preview

Screen Shot 2014-04-29 at 11.26.39 PM.png (16 K) Download Preview

CountDown.java (1 K) Download Preview
--------------------------------------------------------- // Start the animation when the applet is started. // --------------------------------------------------------- public void start() { timer.start(); } // --------------------------------------------------------- // Stop the animation when the applet is stopped. // --------------------------------------------------------- public

CountDownPanel.java (2 K) Download Preview
private final int DELAY = 1000; private final int COUNT_START = 10; private DigitalDisplay clock; private Timer timer; // -------------------------------------------------------- // Set up the applet. // -------------------------------------------------------- public CountDownPanel(Timer countdown, JApplet mouse) { // Your Code: Set up the timer here

DigitalDisplay.java (2 K) Download Preview
// ---------------------------- // return the display value // ---------------------------- public int getVal() { return displayVal; } // ------------------------------------------------------ // reset the display value to that given in the parameter // ------------------------------------------------------ public void reset (int val) { displayVal

Screenshots
Screenshot
Screenshot
Purchase Answer