
CISC 370 Lecture Notes for Class No. 11, March 14, 2000
| Course Home | Prev Lect | Next Lect | Example Progs | Exercise Solns | CoreJava Progs Documentation |
Gen'l Docs | Java 2 APIs | Java Glossary | Term Proj Info |
In this implementation the paintComponent() method displays a single image and the main application program has control over which images are displayed when and for how long. The code is in ImageGallery class in the directory $CISC370HOME/example-progs/pictureGallery/version1/.
Note that repaint() is called each time the imageNo is changed by the method setImageNo(). This schedules, in a separate thread, a call to paintComponent().
Without the loop and the necessity to clear previously painted areas, the paint() method is much simpler in the new implementation.
The Observable class represents an observable object, or "data" in the model-view paradigm. It can be subclassed to represent an object that the application wants to have observed.
An observable object can have one or more observers. After an observable instance changes, an application calling the Observable's notifyObservers() method causes all of its observers to be notified of the change by a call to their update() method. All observers must implement the Observer interface.
There are two key methods in the Observable class that are
used in the ImageGallery.setImageNo() method. They are:
notifyObservers(Object obj)
If this object has changed, as indicated by the
hasChanged() method,
then notify all of its observers and call the
clearChanged() method to indicate that this object has no longer
changed. Each observer is notified by a call o.update(this,obj)
for each Observer o that has registered with this.
setChanged()
Indicates that this object has changed and must normally be called
before calling notifyObservers().
Things to note about version 2 of this code.
Overview of what must be done to use the observer/observable paradigm
public void update( Observable O, Object arg )
where O is the observable object that calls update
and arg is an optional object that the observed object
can choose to send to the observer. In our example, the new
image is sent via arg.
Back to the
CISC 370 homepage.
This page has been accessed
times since 13 Mar 2000.
Corrections, suggestions and comments to Bob Caviness
Copyright
2000 B. F. Caviness