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

Assignment

No new assignment.

Today's Lecture Topics

  1. More on Generic components, interfaces, and specific components to plug into generic components via the interface specifications. A powerful design pattern for large software projects.

    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.

  2. More on constructors in extended classes and the use of super(). (This material is adapted from Arnold & Gosling, The Java Programming Language, 2nd edition, section 3.3)

    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.

    1. Invoke a superclass's constructor.
    2. Initialize the fields using their initializers and any initialization blocks. This is something that cannot be done in C++.
    3. Execute the body of the constructor.

    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.

  3. Graphics programming

    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.

    1. The container classes: Window (and its subclasses of JFrame, Dialog, and FileDialog), Panel, and ScollPanel.
    2. The component classes: Containers contain components. There are a large number of possible component classes such as JButton, JList, JTree, etc. from the javax.swing.* package. Containers themselves are also components so a container can contain other instances of containers.
    3. Painting or graphics classes: These classes define colors, fonts, images, polygons, and include the Graphics class itself. These are found in the java.awt.* package.
    4. Layout managers: components must be arranged within a container. The layout managers help (and sometimes frustrate) to do this.

    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.

    1. Provide a frame for displaying the graphics. In the applet the browser provides a frame (or often called a window, but in Java there is a difference between a window and a frame. A browser window is more like a Java frame) for displaying the graphics. In an application, the application program has to provide its own frame which, in Java, is a window with a "frame" around it and a title bar. This is done by the code
              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.

    2. Initialization of the frame. Before the frame can be used, it needs to be initialized in several ways. In our example, the initializations are carried out in the HelloWorldFrame constructor as is appropriate for a constructor. The important initializations carried out in this example are:

      1. Setting the size of the frame. This is done with the code
                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.

      2. Adding a panel to the content pane of the frame (Note the two different, but similar, words panel and pane). JFrames are made up of four different overlaid panes. The only pane needed by most application programs is the content pane so our program must get a handle for this pane. This is done by the code
                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.

    3. Implementing the paintComponent() method. In the class that extends JPanel, HelloWorldPanel in this case, the paintComponent() method must be over ridden to paint the graphics on the panel. In this case, its implementation is very similar to the implementation of the paint() method used in the Hello World applet.

      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