
CISC 370 Lecture Notes for Class No. 8, March 2, 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 | OOP Guidelines |
In the directories $CISC370HOME/example-progs/callback1.1 and $CISC370HOME/example-progs/callback2.1 we provide other versions of the generic sort programs discussed last time. In this case, the code more closely implements the physical model of machines plugged together . In the version callback2.1, the class GenericSortMachine has a new method setComparatorMachine() that provides for a new comparator machine to be "plugged into" a generic sort machine. This is used in the callback2.1/GenericSortTester class to use a single generic sort machine to sort both strings and names.
When an object is created, all its fields are set to default initial values for their respective types (zero for all numeric types, false for boolean, \u0000 for char, and null for object references). Then the constructor is invoked. Each constructor has three phases.
In C++, one cannot initialize member fields of a class with an initializer as can be done in Java - see item (ii) above and the example with BaseClass and ExtendedClass below.
When you extend a class, the new class must choose one of its superclass's constructors to invoke (or else the default constructor of the super class will be called by default). The part of the object controlled by the super class must be initialized properly, and this is done by the super constructor. In addition, the constructor of the derived class must ensure a correct initial state for the subclass's added instance fields.
In a subclass constructor, you can directly invoke one of the super class's constructors using a super() construct as is done in the four param constructor in the class sg.Trapezoid .
If you do not invoke a super class constructor explicitly, the super class's no-arg constructor is automatically invoked before any statements of the new constructor are executed. If you use super(), with or without args, it must be the first statement of the new constructor.
An example that demonstrates the order in which instance fields are initialized is given in the directory CISC370HOME/example-progs/superConstructors/ . In this example there are two classes
---------------
| |
| BaseClass |
| |
---------------
^
|
|
-------------------
| |
| ExtendedClass |
| |
___________________
plus a Demo class. These classes demonstrate the three phases in
the initialization of an instance variable.
See also the use of super() in the constructor in the Manager class on p. 164 of Horstmann & Cornell.
In Java, as mentioned before, there are two fundamental styles of programs: applets and applications. Most of our programs to date have been applications. An exception was our "Hello World" applet . Now we want to write the analogous program as an application. One of the primary differences between the two is that with an applet the browser provides a graphics device on which to draw. With an application, we must first provide such a device. We use objects from the java.awt.* and javax.swing.* packages to do this.
The awt (= Abstract Windowing Toolkit) contains a large set of classes and interfaces that can be confusing (and at times a pain in the neck!). In Java 2.0, the Java Foundation Classes (JFC aka Swing) add even more graphical functionality. We can view the awt package as roughly divided into four categories.
In Java 1.1, the java.awt.* package was the key package of graphics related classes. With Java 2.0 the Java graphics facilities were significantly expanded and enhanced with the addition of the javax.swing.* package, but the java.awt.* package remains important. The awt components have the look and field of the host machine. Thus, an awt button on Windows will look like a Windows button, on a Mac will look like a Mac button, etc. But there turned out to be too many platfrom dependencies with the awt approach. The Swing classes provide a purely Java solution that minimize the dependencies on the Java host machine. The Swing classes,that is, the non-peer based GUI (= Graphics User Interface) tools are a part of the Java Foundation Classes (JFC) that include the additional 2D API and a drag-and-drop API among other things.
Now back to the hello world application. We give two versions of this application. A first, simpler one that is analogous to our earlier applet and then a more complex, more professional one.
The first one, found in the directory $CISC370HOME/example-progs/helloWorld-app/version1/ contains three key steps that are common to many graphics applications.
JFrame frame = new HelloWorldFrame(); // Create new frame
in the main method of the HelloWorld class that creates a
Hello World Frame. The HelloWorldFrame class is derived from
the javax.swing.JFrame class.
setSize(400,200); // Set size of frame
which makes the frame 400 pixels wide and 200 high. By default
a frame is 0x0 pixels and, hence, invisible.
Container contentPane = getContentPane();// Need content pane of JFrame
The content pane is an object of type Container. We cannot
paint directly on a container so we need a paintable component. A
JPanel is such a component, so we use the code
contentPane.add(new HelloWorldPanel()); // Must add JPanel on which
to add a HelloWorldPanel to the content pane. The class
HelloWorldPanel is derived from the JPanel class.
A frame constructor frequently puts the frame title in the title bar as is done in this case by the code
setTitle("HelloWorld Application"); // Put title at top of frame
In addition, to these things the HelloWorld constructor also adds an event handler to handle closing events on our frame. Event handlers will be covered later.
Note that the paintComponent() method is not called explicitly. It gets called by show(); it also gets called when the window is resized or otherwise needs to be refreshed. We include a debugging statement in the paintComponent() method that prints a message each time paintComponent() is called.
Back to the
CISC 370 homepage.
This page has been accessed
times since 2 Mar 2000.
Corrections, suggestions and comments to Bob Caviness
Copyright
2000 B. F. Caviness