import java.awt.Font;
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

/**
 * ExtendsStuff - Show how Java's inheritance capability
 * makes writing seemingly complex/powerful programs easy.
 * 

* This class is used to introduce the concept of inheritance, * building on top of the existing things that are sort-of * what you want to do. In the lesson "What's Java?," the * Edit-Compile-Execute process was introduced with a simple * application that used System.out.println( ) to display a * String. This class provides a mechanism to display a String * in a pop-up window by invoking the method: printInWindow( ). *

* @author Guy Haas */ public class ExtendsStuff extends Frame implements WindowListener { // Symbolic Constants /** * Default Font Size for Displaying Strings */ public static final int FONT_SIZE = 36; /** * Default Height of Pop-Up Window for Displaying Strings */ public static final int WINDOW_HEIGHT = 200; /** * Default Width of Pop-Up Window for Displaying Strings */ public static final int WINDOW_WIDTH = 400; // Variables Used Across Methods private Font font; private int fontSize; private int windowHeight; private int windowWidth; private Label label; private StringBuffer textContainer; // WindowListener Interface Stuff public void windowActivated(WindowEvent we) {} public void windowClosed(WindowEvent we) {} public void windowClosing(WindowEvent we) { System.exit(0); } public void windowDeactivated(WindowEvent we) {} public void windowDeiconified(WindowEvent we) {} public void windowIconified(WindowEvent we) {} public void windowOpened(WindowEvent we) {} // Constructor public ExtendsStuff() { super("ExtendsStuff Example"); fontSize = FONT_SIZE; windowHeight = WINDOW_HEIGHT; windowWidth = WINDOW_WIDTH; label = new Label(); textContainer = new StringBuffer(); setSize( windowWidth, windowHeight ); setVisible( true ); addWindowListener( this ); } // Methods only Visible Within ExtendsStuff private void setFont() { Font font = new Font( "SansSerif", Font.PLAIN, fontSize ); label.setFont( font ); label.invalidate(); } // end setFont() // Accessible Methods /** * Print some text in a window. *

* Append the supplied text to a StringBuffer and * create a new Label consisting of its contents. * The new Label will use the current FontSize, * which can be changed via setFontSize() method. *

* The size of the Frame and the attributes of the * "SansSerif" Font (size and style) are also set * based on current values of private fields. These * fields may be changed via setXxx() invocations. * @param message String of text to be displayed in a window. * @see #setFontSize * @see #setWindowHeight * @see #setWindowWidth */ public void printInWindow( String text ) { textContainer.append( text ); if ( label != null ) remove( label ); label = new Label( textContainer.toString(), Label.CENTER ); setFont(); add( label, "Center" ); validate(); } /** * Set the height of the font to be used to display a * String by invoking printInWindow(). * @param size Height of the font in points. * @see #printInWindow */ public void setFontSize( int size ) { fontSize = size; setFont(); validate(); } // end setFontSize() /** * Set the height of the window containing a String * displayed by printInWindow(). * @param height number of pixels on window's Y axis. * @see #printInWindow * @see #setWindowWidth */ public void setWindowHeight( int height ) { if ( height != windowHeight ) { windowHeight = height; setSize( windowWidth, windowHeight ); } } // end setWindowHeight() /** * Set the width of the window containing a String * displayed by with printInWindow(). * @param width number of pixels on window's X axis. * @see #printInWindow * @see #setWindowHeight */ public void setWindowWidth( int width ) { if ( width != windowWidth ) { windowWidth = width; setSize( windowWidth, windowHeight ); } } // end setWindowWidth() } // end class ExtendsStuff