/** * Illustrates the paint method for Container objects (MainWindow * is a great-grandchild subclass of Container, and thus * has a paint method). * * 0. Compile and run PaintedWindowTester. Cover up the output window with * some other window and then bring the output window forward again. * Isn't that nice? * * 1. First of all, PaintedWindow is a kind of MainWindow--technically, * we say that PaintedWindow is a "subclass" of MainWindow. Therefore, * any method you can call for a MainWindow object, you can also call * for a PaintedWindow object. Note the "extends" construction that sets * up this class/subclass (or parent/child) relationship. * * 2. Since PaintedWindow is a MainWindow, and MainWindow has several * constructors, PaintedWindow might want to invoke one of those constructors. * To invoke a constructor from the parent class, you use the "super" * construction. If you comment out the "super( title );" line below, * what happens? * * 3. Note that you can put setVisible( true ) in the constructor. This makes * your window a little less of a hassle to use. * * 4. Try changing the color in the setBackground method call. * * 5. Go to Wu's javabook documentation and take a look at the MainWindow * class. At the top of the page, you should see MainWindow's geneology. * For example, MainWindow is a subclass of Frame. Since a PaintedWindow is * a MainWindow and a MainWindow is a Frame, a PaintedWindow is a Frame * (and a Window, and a Container, and...). Look through the MainWindow * documentation--which ancestor class provides the "paint" method? * Open Sun's Java documentation and find the paint method. Is its description * helpful? * * 6. Put System.out.println( "Painting" ); inside the paint method and run * the program. Experiment with moving windows around on top of the PaintedWindow. * When does paint get called? When does it not get called? * * Also shows how to call a superclass constructor. * * @author Jeff Ondich */ import java.awt.*; import javabook.*; class PaintedWindow extends MainWindow { public PaintedWindow( String title ) { super( title ); setVisible( true ); setBackground( Color.white ); } // Whenever a portion of the window object is uncovered, // this method gets called. public void paint( Graphics g ) { g.setColor( Color.cyan ); g.fillOval( 300, 300, 150, 150 ); } }