Problem 7.3
Three ways to implement event listeners
Although we are not covering graphics in the course, in this assignment we'll
look at a program with a Graphical User Interface (GUI) to get some insight into
how event listeners can be implemented in Java. We will not be concerned
with the details of drawing or managing windows, just the software
architecture of the event listener.
Chapter 14 discusses the event listener model.
ReboundGUI.java is a graphical program
that uses a listener to do a simple animation.
In this example, the event is a timer going off, and the listener redraws
an image at a new location to implement the animation.
In addition to the timer, there is another event and listener:
the application listens for mouse clicks and stops the animation or
restarts it whenever the mouse is clicked. The angle of travel of the bouncing
ball is also changed at each click.
The implementation of listeners in this program uses inner classes.
But this is not the only way to implement listeners.
There are other ways that event listeners can be built besides using
an inner class. In this problem you will change the mouse listener
implementation in the application described above to two other ways
to implement an event listener.
This requires you to consider the relationship of objects of different
class types.
The other ways that you will implement the listeners are:
-
using an external event listener class
-
use the GUI class itself as the event listener
The two modified implementations should behave exactly like the original.
You are only changing the implementation details.
Note that you only have to change the implementation of the mouse listener.
The timer listener stays the same for all versions.
Steps for this assignment:
-
Download ReboundGUI.java and
happyface.gif (right click in your browser on these links to download). Compile ReboundGUI.java and make
sure it runs. You should see a happy face bouncing around the screen. If you
see a black screen, you may have forgotten to download the gif file.
Click to see what happens with each mouse click.
This implementation is similar to the examples in the book, using an inner
class for ReboundMouseListener. According to the access rules in Java,
the inner class has access to the
ReboundGUI class and its toggle method to stop/restart
the animation.
-
Now make a copy of ReboundGUI.java
called ReboundGUI2.java and change the class name to ReboundGUI2.
Modify ReboundGUI2.java to use an external class called
ReboundMouseListener.
That is, remove the inner class and instead implement a similar
external class in a separate Java file named ReboundMouseListener.java.
Fix up code in ReboundGUI2 so that it all works - there may be several
things you have to change. Again you should see a
bouncing happy face and the mouse should stop/start the animation.
Don't forget that you will replace
new ReboundGUI()
with
new ReboundGUI2()
in the original main method as well as any other place
that the class name ReboundGUI appears.
Make sure that you implement the listener in
a separate Java file named ReboundMouseListener.java,
and remove the inner class ReboundMouseListener definition from ReboundGUI2.java.
-
Now make another copy of the original ReboundGUI.java,
but this time call it ReboundGUI3.java, and change
the class name to ReboundGUI3.
Modify ReboundGUI3.java so that it does not need the separate class
ReboundMouseListener at all.
To do this, change the ReboundGUI3 class to implement the
MouseListener interface itself.
Fix up code in ReboundGUI3 so that it all works.
As above you will replace
new ReboundGUI()
with
new ReboundGUI3()
in the original main method as well as any other place
that the class name ReboundGUI appears.
Make sure that you remove the inner class ReboundMouseListener
definition from ReboundGUI3.java,
and that there is no class named ReboundMouseListener.
Things to think about
What are the advantages/disadvantages of these different ways of implementing
the event listener design in Java? In each case, what did you have to change
about the GUI class and/or the listener class? Did you strengthen or weaken
encapsulation? Is one implementation simpler than another?
Turn in
Turn-in: the source code for ReboundGUI2.java, ReboundMouseListener.java,,
and ReboundGUI3.java. Your two versions of ReboundGUI should have
exactly the
same external behavior as the original version. It is only their internals
that have changed.