TxtWrite Source Code

//***************************************************************************
// Application:	Villanova Cat Cave Conference Center
// File: 	TxtWrite.java 
// Module:	TxtWrite
//		TxtMsg
// Version:	1.0 
// Written by:  Graham L. Mehl
//              Villanova Master of Science Candidate
//              Independent Study project
// Date:        3/31/96
// 
// Purpose:     The purpose of this file is the creation and functionality
//		a message board that the user can type in a message.  This
//		message then will appear on the easel page.  The message
//		board allows the user to change the font size of the text.
//		The end result is a text object for the easel page. 
// 
//***************************************************************************

import java.awt.*;

public class TxtWrite extends Frame {

        /*
         * default message variable
         */
         textBox message;
         TxtMsg msg;
	 StringBuffer commMessage;
	 zClientDef clientDef;
	 whiteBoard parent_frame;

	/* 
	 * This method is the constructor that accepts the
	 * existing message and prepares a message board
	 * for the user to type in the message that the
	 * user wants to see on the white board.
	 */
        public TxtWrite(zClientDef commHandler, StringBuffer StringMessage, 
		textBox messageObj, whiteBoard wb )  {

        /*
         * Set default message variable
         */

		clientDef = commHandler;

		commMessage = new StringBuffer ();
		commMessage = StringMessage;

		parent_frame = wb;

                message = messageObj;
                msg = new TxtMsg(parent_frame, this, message, clientDef,
					 commMessage);

		/* 
		 * Create the message board
		 */
                setLayout(new BorderLayout());
                add( "Center", msg );
                Panel p = new Panel();
                p.setLayout(new FlowLayout());
                add("South", p);
                Choice FontSize = new Choice();
                FontSize.addItem("10");
                FontSize.addItem("12");
                FontSize.addItem("14");
                FontSize.addItem("16");
                FontSize.addItem("18");
                FontSize.addItem("20");
                FontSize.addItem("22");
                FontSize.addItem("24");
                FontSize.addItem("26");
                FontSize.addItem("28");
                FontSize.addItem("30");
                FontSize.addItem("36");
                p.add(FontSize);
                p.add( new Button( "OK" ) );
        }

	/*
	 * This method is the event handler for the message board.
	 * and key down or mouse clicks while the message board is
	 * active, the action is determined here
	 */
	public boolean action( Event evt, Object arg )
	{
		/*
		 * the code below is called when the push buttons 
		 * are pressed
		 */
                if( "OK".equals( arg ) )
		{
			endMessage();
		}
   
                if (evt.target instanceof Choice) {
                        String choice = (String)arg;

			Integer FirstNumASCII= new Integer 
					(choice.substring (0,2) );
			Integer FirstNumValue = 
				FirstNumASCII.valueOf(choice.substring (0,2) );
			int FirstNumInt = FirstNumValue.intValue();

                        message.setFontSize( FirstNumInt);
                }
		/*
		 * event won't cascade backwards in object hierarchy
		 */
		return( true ); 
	}
	public void endMessage()
	{		
		msg.ReturnMessage();
		parent_frame.repaint(); // so the creator sees the object
                this.dispose();
	}

}

/*
 * This class is the class that controls how the message in
 * the message board appears.
 */
class TxtMsg extends Panel {

    /* 
     * Defines a variable to allow message to be sent across the
     * network to other whiteBoards.
     */
	zClientDef clientDef;

    /* 
     * Defines a variable to for a message to sent it across the
     * network to other whiteBoards.
     */
	StringBuffer networkMessage;

    /*
     * Must define the invoker of this object for disposal purposes
     */
     TxtWrite the_window;

    /* 
     * This is the maximum length of a Message. 
     */
    final int maxWordLen = 61;

    /* 
     * This buffer holds letters that the user has successfully
     * typed in. 
     */
    char word[];

    int wordLen; // Number of correct letters in 'word'. 

    /* 
     * This is the font used to print the letters typed. 
     */
    Font wordFont;

    /* 
     * This is the font used to calculate the size fo the word that the
     * user types
     */
    Font calcWordFont;

    /* 
     * This is used to help determine the heigth of the letters
     * that the users typed. 
     */
    FontMetrics wordFontMetrics;

    /*
     * Determines the color of the word begin placed on the screen
     */
     Color wordColor;

     textBox msg;

     whiteBoard Parent_frame;

    /*
     * Initialize the message Information 
     */
    public TxtMsg(whiteBoard wb,TxtWrite the_windowObj, textBox messageObj, 
		zClientDef commHandler, StringBuffer netMsg) {

	clientDef = commHandler;
	networkMessage = netMsg;
	the_window = the_windowObj;
        msg = messageObj;
        word = new char[maxWordLen];
	newWord();

        wordColor=msg.getColor();
        this.setBackground(Color.white);

        wordFont = new java.awt.Font("Courier", Font.BOLD, 24 );
	wordFontMetrics = getFontMetrics(wordFont);

	resize((maxWordLen+1) * wordFontMetrics.charWidth('M') + maxWordLen * 3,
            wordFontMetrics.getHeight());
        requestFocus();                         // get keyboard events

    }

    /*
     * Paint the message on the message board.
     */
    public void paint(Graphics g) {
        Font font;
	FontMetrics fontMetrics;
        int i, x, y;

        if (wordLen > 0) {
	    int Mwidth = wordFontMetrics.charWidth('M');
	    int Mheight = wordFontMetrics.getHeight();

	    g.setFont(wordFont);

            /*
	     * draw known characters in word
	     */
	    x = 0;
	    y = size().height - 3;
            g.setColor(msg.getColor() );
            for (i=0; i<wordLen; i++) {
		if (word[i] != 0) {
		    g.drawChars(word, i, 1, x, y);
		}
		x += Mwidth + 3;
	    }
	     /*
	      * This if statement handles if a character is
	      * deleted with by pressing the backspace key
	      */
	     if  (wordLen < maxWordLen) {
                if (word[wordLen] != 0) {
                    g.setColor(Color.white);
                    g.drawChars(word, wordLen, 1, x, y);
                    g.setColor(msg.getColor() );
                    word[wordLen] = 0;
		}
	    }
        }
    }

	/*
	 * This method converts a color in to a literal allowing
	 * it to become part of a string to be transmitted across
	 * the network to another white board
	 */
	char getColor( Color color )
	{
		if( color == Color.blue )
		{
			return( '0' );
		}

		if( color == Color.red )
		{
			return( '1' );
		}

		if( color == Color.black )
		{
			return( '2' );
		}

		if( color == Color.yellow )
		{
			return( '3' );
		}

		if( color == Color.cyan )
		{
			return( '4' );
		}

		if( color == Color.magenta )
		{
			return( '5' );
		}

		if( color == Color.green )
		{
			return( '6' );
		}

		if( color == Color.gray )
		{
			return( '7' );
		}

		if( color == Color.orange )
		{
			return( '8' );
		}

		if( color == Color.pink )
		{
			return( '9' );
		}

		return( '0' );
	}
/*
 * This method handles all key presses whilte the user
 * is inside the message board
 */
    public boolean keyDown(java.awt.Event evt, int key) {
        int i;
        boolean found = false;

        if ((key == 10) || (wordLen >= maxWordLen)) { 
	   /* 
	    * Only return the message if a word exists already
	    * and the length of the message has reached its 
	    * maximum length or the return key was pressed
	    */
	    the_window.endMessage();
            // ReturnMessage();
            return true;
        }

        /*
	 * check if valid character
	 */
        if (((key >= '!' && key <= '~') || (key == ' ')) && 
		(wordLen < maxWordLen)) {
                word[wordLen++] = (char)key;
                msg.setMessage(word, wordLen);
                found = true;
        } else          // if not a valid letter then what key is it
          
                if (key == 8) {   // The Delete Key
                    wordLen--;
                } else

                if (key == 10) { // The Return Key
            		ReturnMessage();  // ADD RETURN MESSAGE HERE
                } else

                     {            // Not a valid character
                      return true;
                     }    
                 
        repaint();
	return true;
    }

    /*
     * Grab the focus of the key down events when the message board is
     * started and returns the message when
     * the message is complete.
     */
    public boolean mouseDown(java.awt.Event evt, int x, int y) {
        int i;

        requestFocus();     // grab focus to get keyDown events

	
        if ((wordLen > 0 ) && (maxWordLen == wordLen)) {
	    
	   /* 
	    * Only return the message if a word exists already
	    * and the length of the message has reached its 
	    * maximum length
	    */

            ReturnMessage(); 	

        } else {

           /*
	    * Do Nothing Only if the word does not exist or if 
	    * the length of the message has NOT reached its 
	    * maximum length
	    */

        }
	return true;
    }

    /*
     * Starts a new word and clear the buffer
     */
    public void newWord() {
        int i;

        for (i=0; i<maxWordLen; i++) {	 // clear word buffer
            word[i] = 0;
        }
        wordLen = 0;

        repaint();
    }

    /*
     * Gets the message the user typed in
     */
     public char[] GetMessage() {

        return word;

     }

    /*
     * Returns the message the user typed in
     */
     public void ReturnMessage() {

     FontMetrics calcWordFontMetrics;

        calcWordFont = new java.awt.Font("Courier", Font.BOLD, 
				msg.getFontSize() );
        calcWordFontMetrics = getFontMetrics(calcWordFont);
	int CMwidth = calcWordFontMetrics.charWidth('M');
	int CMheight = wordFontMetrics.getHeight();
	msg.setX2( ( CMwidth  * wordLen) + msg.getX1() );
	msg.setY2( msg.getY1() + (( msg.getFontSize() / 2) * 2 ) /*+ CMheight*/ );

	/* 
	 * Because the string of the message for whiteboard is
	 * being passed, then the message is being changed 
	 * directly. The whiteboad just has to refresh itself
	 * send it to all the other white boards that are in
	 * the conference.
	 */
	   // Parent_frame.repaint();	// So the object creator sees the new object

         /* 
	  * This sets of lines creates the message
	  * to broadcast to the other white boards
	  * to display the complete message that 
	  * user types in.
	  */
	    networkMessage.append( "TS" );
	    networkMessage.append( msg.getX1() );
	    networkMessage.append( ',' );
	    networkMessage.append( msg.getY1() );
	    networkMessage.append( ',' );
	    networkMessage.append( msg.getFontSize() );
	    networkMessage.append( ',' );
	    networkMessage.append( msg.getMessage() );
	    networkMessage.append( ',' );
	    networkMessage.append( msg.getX2() );
	    networkMessage.append( ',' );
	    networkMessage.append( msg.getY2() );
	    networkMessage.append( ',' );
	    networkMessage.append( getColor(wordColor ) );

	    clientDef.zCommDispatch( "_PM_", networkMessage.length(),
			networkMessage.toString() );
	/*
	 * Dispose of the frame that was use to create the 
	 * message board
	 */ 
	
	the_window.dispose();

     }
}

Villanova Univ. Seal Top of Paper | Previous Page | Next Page | Bottom of Paper

Written By Graham L. Mehl
Last Modified on April 23, 1996
© Villanova University
</