/*
 * File: p03.java
 *
 * Description:  Open up a window and display an arbitrary string in it.
 *
 * Modifications:  11/2/95 CValcarcel Created.
 *
 * NO WARRANTY, EXPRESSED OR IMPLIED, BLAH, BLAH, BLAH.
 *
 * THIS IS EXAMPLE CODE AND SHOULD NOT BE USED FOR ANYTHING
 * EXCEPT INSTRUCTIONAL PURPOSES.
 *
 */

//
// The import command acts like the #include preprocessor
// directive of C in that it is looking for information
// that will allow it to resolve any unknown references
// in this file.  In C that would entail resolving forward
// references, structure declarations, enums, #defines, etc.
// Java is only looking for class declarations.  It will
// resolve all references to classes and static variables.
//
// In the last two examples we did not import java.lang.System
// because the javac compiler does that by default.
//
import java.awt.Frame;
import java.awt.Label;

//
// EchoWindow is a Frame and has a Label.  In C++-speak that
// means subclass off of Frame and include Label as a member.
//
class EchoWindow extends Frame {
    Label labelOutput;

    //
    // Our constructor.  Even though I originally wrote this on
    // a Windows95 machine the beta release has removed the need
    // to explicitly start a WServer.  The code is much more
    // elegant and easier to follow.
    //
    // Constructors in Java don't return anything but themselves
    // so the return type is implicit in the call.  The variables
    // title and message use the internal type String which is a
    // read-only class that allows for the manipulation, as well
    // as storage, of an array of characters.  For read and write
    // capability use class StringBuffer.
    //
    public EchoWindow( String title,
                       String message )
    {
        //
        // This calls the constructor of the Frame object
        // and gives it the title "title".  This must be
        // called before any other code is executed.  Order
        // of execution is very important to Java as objects
        // will be initialized in a particular order and anything
        // that goes wrong at a super class will bring down
        // the application mighty fast.
        //
        super( title );

        //
        // Where, oh, where shall we put this string?  I'm
        // sure there are many places to do that.  Not wanting
        // to complicate things I chose an AWT object that
        // would simply take in a String object as a constructor
        // argument and display it within itself.  Label
        // was the perfect candidate.  The only call to the Label
        // object in this method is this.
        //
        labelOutput = new Label( message );

        //
        // These methods are in the Frame object.  add() takes
        // the Label object and attaches it to our Frame.
        //
        add( labelOutput );

        //
        // Please make this window big enough for everyone to
        // see something.
        //
        resize( 200, 50 );

        //
        // This used to be called map().  It is much more
        // OO to call it show() because at an abstract level
        // that is what the behavior is termed.  If you went
        // into a department store and asked if someone could
        // map you a toaster I think they might think twice
        // before helping you...
        //
        show();
    }

    //
    // This method overrides (not overloads) the Frame method
    // of the same name.  As the Frame object and the Label
    // object are distinct they do not follow each other's
    // resize events.  If you want to see what kind of behavior
    // you get without this just comment this out.  The outside
    // Frame will resize, but the inside Label will sit unmoved
    // and unresizeable (is there such a word?).
    //
    public void repaint()                         
    {
         // This says: resize() the Label to the size() of the Frame
         labelOutput.resize( size() );
    }

    //
    // Here we are again.
    //
    public static void main( String args[] )
    {
        String strBuf = "";   // A temporary holding area

        //
        // By rights this should be in an object that is
        // an extention of the String class.  All we want
        // to do is take an array of Strings and turn it
        // into a concatenated String with a single space
        // between each array member.  This will do for now.
        //
        if ( args.length > 0 )
        {
            strBuf = args[0];

            // "Hey, I thought you said class String was read-only?"
            // Well, okay, I lied.  No, I didn't.  class String
            // uses StringBuffer whenever it is used in a fashion
            // that would cause it to break integrity with the
            // string in its possession so it only LOOKS like
            // String is writeable.
            for ( int i = 1; i < args.length; i++ )
                strBuf += " " + args[i];
        }

        //
        // Call the constructor and do whatever it is that
        // happens under the hood.  I will now say this in
        // caps:
        //
        // THERE IS NO WAY FOR THIS APPLICATION TO EXIT UNDER WIN95.
        // PLEASE CNTL-C OUT IN THE WINDOW WHERE IT WAS
        // STARTED (IF YOU ARE NOT RUNNING IT IN THE BACKGROUND).
        // IT WILL NOT GO AWAY WITHOUT A FIGHT.  THE NEXT EXAMPLE
        // WILL TAKE CARE OF THAT.
        //
        EchoWindow app = new EchoWindow( "Display A String", strBuf );
    }
}

