The Black Box


Most of us have no idea how buttons and listeners actually work.  They are like a black box to us, in that their private data and their implementation code are hidden and encapsulated

A programmer uses black box objects without knowing how they are doing what they are doing.  Furthermore, we don't care how they work!  (If you really want to know how buttons and listeners work, go look at the Java source code.)
Text Box:  4  import javax.swing.*;   5  import java.awt.*;   6  import java.awt.event.*;   7  public class Panel04 extends JPanel   8   {   9    private Odometer odometer;  10    public Panel04()  11     {  12      setLayout(new FlowLayout());  13  14      odometer = new Odometer();  15      add(odometer);  16  17      JButton button = new JButton('Step');  18      button.addActionListener(new Listener());  19      add(button);  20     }  21    private class Listener implements ActionListener  22     {  23      public void actionPerformed(ActionEvent e)  24       {  25         odometer.update();  26       }  27     }  28   }In this lab, you get a chance to do your own black box programming.  Driver04 looks exactly like all the other drivers.  It has a Panel04, which has already been written and compiled for you, but is shown to the left.  Do not type it in!
Lines 4-6:  Import some mysterious classes.
Line 9:  declares a private field of type Odometer. 
Lines 14-15: The panel instantiates and adds an odometer object to itself.   What is an odometer object?  The panel doesn’t know! 
Lines 17 – 19: create a button, as usual.
Line 25:  When the button is clicked, the update() method in the odomoter object is called.  What is update()?  We don't know.  Only the odometer knows.
Your job, should you decide to accept it, will be to program the Odometer class.


usess
 
 

 

 

Lab04: Odometer

Objective: Look inside a black box.  Modulus and Integer Division ( % and / )
Background

An odometer is an object that displays numbers. Clearly, the Odometer class should extend the JPanel class.  Since Odometer isa JPanel, set the layout.  Set the background to black.  Add the GUI components to make the odometer panel look like an odometer.  Which kind of components do you need?  How many?

Default labels are "clear".  There is a way to make the label "opaque." You'll also have to set the background and the foreground colors.  Look at the Java API.
An odometer also keeps count. You will have to add a counter to the odometer.
Someone using Panel04 will click on the "step" button.  That action causes the odometer’s update() method to be called.  When update() is called, it increments the counter and displays the result in the labels.  In the case shown, the counter variable is storing 57.  To display the result in the labels you will have to use / and %.
Once again we have an example of encapsulation at work.  Updating is something Odometer does; button-clicking is something Panel04 does; and the frame holds it all

Specification
Filename Unit3\Lab04\Odometer.java (load).  Complete the implementation of the Odometer class as described above. 
Filename Unit3\Lab04\Panel04.  This file is given to you in compiled form.  Don’t overwrite it!
Filename Unit3\Lab04\Driver04.java (load).  Just run the driver and your Odometer.class file will be found by the system.