//----------------------------------------------------------------- // // Steven Vormwald // CS1131 // Fall 2002 // October 18, 2002 // // This program allows a user to draw up to 5 rectangles // and up to 5 ovals in a 600x600 pixel drawing area with // user specified color, position, length, width, and fill. // //----------------------------------------------------------------- /* Applet Revision History: Version 1.0- All commands implemented, all work properly in tests. Version 1.1- Changed method of changing background color from 'SETVAR [INDEX] BACKGROUND [COLOR]' to 'BACKGROUND [COLOR]'. Version 1.2- Added a history stack accessible by pressing the and arrow buttons on the keyboard. Version 2.0- Rewrote all command classes to take advantage of the java.util.StringTokenizer class and exception handling. Also added right arrow completion for commmands, though not for arguments to those commands. Also added handling for multiple commmands on a line, and right arrow completion for multiple commands on a line. Version 2.1- Fixed bugs in symbolic color handling code. */ import java.awt.*; import java.awt.event.*; import java.util.StringTokenizer; import java.util.NoSuchElementException; import javax.swing.*; /** * DrawProgram is the class that controls the applet. * * @author Steven Vormwald * @version 2.0 */ public class DrawProgram extends JApplet implements ActionListener, KeyListener { private final String [][] sShort = { { "B", "C", "D", "H", "S", "V" }, // Starting strings of commands whose first letter is unique... { "LI", "LO", "NE", "NU", "RA", "RE" } }; // Starting strings of commands whose first letter + second letter string is unique... private final String [][] sLong = { { "BACKGROUND", "COPY", "DELETE", "HELP", "SETVAR", "VIEW" }, { "LIST", "LOWER", "NEW", "NUMBER", "RAISE", "REDRAW" } }; private MyArray oArray = new MyArray(); private CommandHistory oCmdHistory = new CommandHistory( 15 ); private DrawingPanel oDrawField = new DrawingPanel(oArray); private JTextField oText = new JTextField("Type HELP to see a list of commands and their syntax."); private JTextArea oBaseCMD = new JTextArea( "General Help:\nCOMMANDS:\n" + "HELP\tRAISE\tLOWER\tLIST\n"+ "VIEW\tDELETE\tNUMBER\tSETVAR\n" + "NEW\tREDRAW\tCOPY\tBACKGROUND\n\n" + "VARIABLES:\n" + "X\tY\tWIDTH\tLENGTH\n" + "COLOR\tFILL\tVISIBLE\n\n" + "=> TYPE \'HELP [CMD]\' FOR COMMAND SPECIFIC HELP.\n" + "=> TYPE \'HELP [VARS]\' FOR VARIABLE SPECIFIC HELP.\n" + "=> NOTICE, WHEN \'[CMD]\' APPEARS, IT REFERS TO ANY OF THE\n" + " ABOVE COMMANDS\n" + "=> NOTICE, WHEN \'[VARS]\' APPEARS, IT REFERS TO ANY OF THE\n" + " ABOVE VARIABLES\n" + "=> NOTICE, WHEN \'[INTEGER]\' APPEARS, IT REFERS TO AN INTEGER\n" + " AS DESCRIBED IN THE DESCRIPTION\n" + "=> NOTICE, WHEN \'[VALUE]\' APPEARS, IT REFERS TO A VALUE\n" + " SPECIFIED IN THE DESCRIPTION OF THE PRECEDING VARIABLE\n" + "=> NOTICE, WHEN MULTIPLE WORDS APPEAR IN BRACKETS\n" + " ( \'{\' AND \'}\' ) AND ARE SEPARATED BY A PIPE ( \'|\' ),\n" + " IT REFERS TO CHOOSING ONE OF THE WORDS.\n" + "=> ALL COMMANDS ARE CASE INSENSITIVE.\n" + "=> THERE CAN BE A MAXIMUM OF 5 OVALS AND 5 RECTANGLES\n" + " IN THE STACK AT A TIME, AND A MAXIMUM OF 10 OBJECTS.\n" + "=> NOW YOU CAN HIT THE UP OR DOWN ARROWS TO ACCESS THE\n" + " COMMAND HISTORY.\n" + "=> NOW YOU CAN HIT THE RIGHT ARROW TO COMPLETE A\n" + " COMMAND ON THE COMMAND LINE BY TYPING THE FIRST\n" + " COUPLE LETTERS OF THE COMMAND AND HITTING THE\n" + " RIGHT ARROW KEY.\n" + "=> NOW YOU CAN HAVE MULTIPLE COMMANDS ON A LINE\n" + " BY SEPARATING THE COMMANDS WITH THE SEMICOLON\n" + " ';' CHARACTER." ); /** * Initializes the applet. */ public void init() { Container window = getContentPane(); window.setLayout(new BorderLayout()); oBaseCMD.setEditable(false); oDrawField.setPreferredSize( new Dimension( 600, 600 ) ); oDrawField.setBackground( Color.darkGray ); oText.setPreferredSize( new Dimension( 600, 25 ) ); oText.setCaretColor( Color.white ); oText.setSelectionColor( Color.blue ); oText.setSelectedTextColor( Color.white ); oText.setBackground( Color.black ); oText.setForeground( Color.green ); oBaseCMD.setPreferredSize( new Dimension( 400, 600 ) ); oBaseCMD.setSelectionColor( Color.blue ); oBaseCMD.setSelectedTextColor( Color.white ); oBaseCMD.setBackground( Color.black ); oBaseCMD.setForeground( Color.green ); window.add(oDrawField, BorderLayout.CENTER); window.add( oText, BorderLayout.SOUTH ); window.add(oBaseCMD, BorderLayout.EAST); oText.addActionListener(this); oText.addKeyListener(this); oText.requestFocus(); } /** * Captures the text from the command line, creates a TextCommand object and calls it's * runCommand method. * * @param event The action event that triggered the method. */ public void actionPerformed(ActionEvent event) { StringTokenizer oMultiCmd = new StringTokenizer( oText.getText().trim(), ";" ); TextCommand oCmd; String sTmp; while(oMultiCmd.hasMoreTokens()) { sTmp = oMultiCmd.nextToken().trim(); oCmd = new TextCommand( sTmp, oDrawField ); oCmdHistory.put( sTmp ); oBaseCMD.setText( oCmd.runCommand() ); } oText.setText( "" ); } /** * Determines if the command history needs to be displayed and * displays the next line in the command history if needed. * * @param ke The key event that triggered the method. */ public void keyPressed(KeyEvent ke) { switch(ke.getKeyCode()) { case KeyEvent.VK_UP: oText.setText( oCmdHistory.getPrev() ); break; case KeyEvent.VK_DOWN: oText.setText( oCmdHistory.getNext() ); break; } } /** * Completes the incomplete command on the command line * when the right arrow key is released. * * @param ke The key event that triggered the method. */ public void keyReleased(KeyEvent ke) { String sTmp1, sTmp2; StringTokenizer stTmp; int nNumTokens; boolean isFound; switch(ke.getKeyCode()) { case KeyEvent.VK_RIGHT: { sTmp1 = ""; stTmp = new StringTokenizer( oText.getText(), ";" ); nNumTokens = stTmp.countTokens(); if( nNumTokens <= 0 ) { break; } for(int i=0; i=0;i--) { oCmdArray[i+1] = oCmdArray[i]; } oCmdArray[0] = s; nCurr = -1; } /** * Gets the previous (chronologically) command in the command history. * * @return A string containing the previous command in the command history. */ public String getPrev() { if( nCurr >= nLast ) { if(nCurr == nLast) nCurr++; return ""; } nCurr++; return oCmdArray[nCurr]; } /** * Gets the next (chronologically) command in the command history. * * @return A string containing the next command in the command history. */ public String getNext() { if( nCurr <= 0 ) { if( nCurr == 0 ) nCurr--; return ""; } nCurr--; return oCmdArray[nCurr]; } } /** * DrawingPanel is the class that controls 600x600 pixel drawing field. * * @author Steven Vormwald * @version 2.0 */ class DrawingPanel extends JPanel { private MyArray oArray; /** * The constructor for the DrawingPanel class. * * @param o A MyArray object that is going to hold the objects drawn to the panel. */ public DrawingPanel( MyArray o ) { oArray = o; } /** * Paints the panel. * * @param g The graphics object to use to draw the scene. */ public void paintComponent(Graphics g) { super.paintComponent(g); oArray.drawArray(g); } /** * Gets the array of drawing objects. * * @return A reference to the MyArray object holding the objects drawn to the panel. */ public MyArray getArray() { return oArray; } } /** * MyArray is the class that is a front end for handling the array of drawing objects. * * @author Steven Vormwald * @version 2.0 */ class MyArray { private final int nMaxArraySize = 10; private DrawingObject [] oShapeArray = new DrawingObject[nMaxArraySize]; private int nNumRect = 0; private int nNumOval = 0; private int nTop = -1; /** * Adds a drawing object to the array. * * @param o A reference to the object to add to the array. * @return A string with debugging information. */ public String newObject( DrawingObject o ) { try { if( o.getShape().equals( "Rectangle" ) ) { if( nNumRect < 5 ) { nNumRect++; oShapeArray[nTop + 1] = o; nTop++; return oShapeArray[nTop].getShape().toUpperCase() + " SUCCESSFULLY CREATED"; } else { return "ERROR"; } } else if( o.getShape().equals( "Oval" ) ) { if( nNumOval < 5 ) { nNumOval++; oShapeArray[nTop + 1] = o; nTop++; return oShapeArray[nTop].getShape().toUpperCase() + " SUCCESSFULLY CREATED"; } else { return "ERROR"; } } else { return "ERROR"; } } catch(ArrayIndexOutOfBoundsException e) { return "ERROR"; } } /** * Gets the current size of the array (number of objects). * * @return An integer indicating the number of objects in the array. */ public int length() { return nTop + 1; } /** * Exchanges the object in index n with the one in index n+1. * * @param n The index of the object to raise. * @return A string containing debugging information. */ public String raiseObject( int n ) { if( n > nTop - 1 ) { return "ERROR"; } else if( n < 0 ) { return "ERROR"; } else { DrawingObject tempObj = oShapeArray[n]; oShapeArray[n] = oShapeArray[n+1]; oShapeArray[n+1] = tempObj; return "ARRAY OBJECT " + n + " RAISED TO INDEX " + (n + 1); } } /** * Exchanges the object in index n with the one in index n-1. * * @param n The index of the object to lower. * @return A string containing debugging information */ public String lowerObject( int n ) { if( n > nTop ) { return "ERROR"; } else if( n < 1 ) { return "ERROR"; } else { DrawingObject tempObj = oShapeArray[n]; oShapeArray[n] = oShapeArray[n-1]; oShapeArray[n-1] = tempObj; return "ARRAY OBJECT " + n + " LOWERED TO INDEX " + (n - 1); } } /** * Lists the current contents of the array. * * @return A string containing the list of the current contents of the array. */ public String listObjects() { String objectList = "OBJECT LIST:\n\n0. \n1. \n2. \n3. \n4. \n5. \n6. \n7. \n8. \n9. "; int i; for(i = 0; i <= nTop; i++) { objectList = objectList.substring( 0, objectList.indexOf( i + ". " ) + 3 ) + oShapeArray[i].getShape() + objectList.substring( objectList.indexOf( i + ". " ) + 3 ); } for(;i < 10; i++) { objectList = objectList.substring( 0, objectList.indexOf( i + ". " ) + 3 ) + "NOT DEFINED" + objectList.substring( objectList.indexOf( i + ". " ) + 3 ); } return objectList; } /** * Shows the properties of the object indexed by n. * * @param n The index of the object to view. * @return A string containing the properties of the object. */ public String viewObject( int n ) { try { return "OBJECT " + n + oShapeArray[n].viewObject(); } catch(ArrayIndexOutOfBoundsException e) { return "ERROR"; } } /** * Deletes the object indexed by n. * * @param n The index of the object to delete. * @return A string containing debugging information. */ public String deleteObject( int n ) { try { if( oShapeArray[n].getShape().equals( "Oval" ) ) { nNumOval--; } else { nNumRect--; } for( int i = n; i < nTop; i++ ) { oShapeArray[i] = oShapeArray[i+1]; } nTop--; return "OBJECT " + n + " SUCCESSFULLY DELETED"; } catch(ArrayIndexOutOfBoundsException e) { return "ERROR"; } } /** * Clears the entire array. * * @return A string containing debugging information. */ public String deleteAll() { nTop = -1; nNumRect = 0; nNumOval = 0; return "ALL OBJECTS DELETED"; } /** * Displays the current number of objects in the array of the specified type; * -1 indicating DrawingObjects, 0 indicating Rectangles, 1 indicating Ovals. * * @param n The type of object to perform the number command on. * @return A string displaying the number. */ public String getNumber( int n ) { switch( n ) { case -1: return "CURRENT NUMBER OF OBJECTS:\t" + (nTop + 1) + " / 10"; case 0: return "CURRENT NUMBER OF RECTANGLES:\t" + nNumRect + " / 5"; case 1: return "CURRENT NUMBER OF OVALS:\t" + nNumOval + " / 5"; default: return "ERROR"; } } /** * Copys the object in index n to a new object at the end of the array. * * @param n The index of the object to copy. * @return A string containing debugging information. */ public String copyObject( int n ) { if( nTop < 9 ) { try { if( oShapeArray[n].getShape().equals( "Rectangle" ) ) { if( nNumRect < 5 ) { nNumRect++; oShapeArray[nTop + 1] = new RectangleObject( oShapeArray[n] ); nTop++; return oShapeArray[nTop].getShape().toUpperCase() + " SUCCESSFULLY COPIED TO NEW OBJECT"; } else { return "ERROR"; } } else if( oShapeArray[n].getShape().equals( "Oval" ) ) { if( nNumOval < 5 ) { nNumOval++; oShapeArray[nTop + 1] = new OvalObject( oShapeArray[n] ); nTop++; return oShapeArray[nTop].getShape().toUpperCase() + " SUCCESSFULLY COPIED TO NEW OBJECT"; } else { return "ERROR"; } } else { return "ERROR:\nBAD OBJECT TYPE:\n" + oShapeArray[n].getShape(); } } catch(ArrayIndexOutOfBoundsException e) { return "ERROR"; } } else { return "ERROR"; } } /** * Copies the object in index from to the one in index to. * * @param from The index of the object to copy. * @param to The index of the object to replace. * @return A string containing debugging information. */ public String copyObject( int from, int to ) { if( ((from < 0)||(to < 0)) || ((from > nTop)||(to > nTop)) ) { return "ERROR"; } else { if( from == to ) { return "ERROR"; } else { if( oShapeArray[from].getShape().equals( "Rectangle" ) ) { if( (nNumRect < 5) || oShapeArray[to].getShape().equals( "Rectangle" ) ) { if( ! oShapeArray[to].getShape().equals( "Rectangle" ) ) { nNumRect++; nNumOval--; } oShapeArray[to] = new RectangleObject( oShapeArray[from] ); return oShapeArray[to].getShape().toUpperCase() + " SUCCESSFULLY COPIED TO OBJECT " + to; } else { return "ERROR"; } } else if( oShapeArray[from].getShape().equals( "Oval" ) ) { if( (nNumOval < 5) || oShapeArray[to].getShape().equals( "Oval" ) ) { if( ! oShapeArray[to].getShape().equals( "Oval" ) ) { nNumRect--; nNumOval++; } oShapeArray[to] = new OvalObject( oShapeArray[from] ); return oShapeArray[to].getShape().toUpperCase() + " SUCCESSFULLY COPIED TO OBJECT " + to; } else { return "ERROR"; } } else { return "ERROR:\nBAD OBJECT TYPE:\n" + oShapeArray[from].getShape(); } } } } /** * Draws all the objects in the array. * * @param g The graphics object to use. */ public void drawArray( Graphics g ) { for(int i=0;i<=nTop;i++) { if( oShapeArray[i].getActive() ) { oShapeArray[i].drawShape(g); } } } /** * Sets the x value of the object at index nObNum. * * @param nObNum The index of the object to set the value of x. * @param nX The value x is going to be set to. * @return A string containing debugging information. */ public String setVarX( int nObNum, int nX ) { try { if( nObNum <= nTop ) { return oShapeArray[nObNum].setX( nX ); } else { return "ERROR"; } } catch(ArrayIndexOutOfBoundsException e) { return "ERROR"; } } /** * Sets the y value of the object at index nObNum. * * @param nObNum The index of the object to set the value of y. * @param nY The value y is going to be set to. * @return A string containing debugging information. */ public String setVarY( int nObNum, int nY ) { try { if( nObNum <= nTop ) { return oShapeArray[nObNum].setY( nY ); } else { return "ERROR"; } } catch(ArrayIndexOutOfBoundsException e) { return "ERROR"; } } /** * Sets the width value of the object at index nObNum. * * @param nObNum The index of the object to set the value of width. * @param nW The value width is going to be set to. * @return A string containing debugging information. */ public String setVarW( int nObNum, int nW ) { try { if( nObNum <= nTop ) { return oShapeArray[nObNum].setW( nW ); } else { return "ERROR"; } } catch(ArrayIndexOutOfBoundsException e) { return "ERROR"; } } /** * Sets the length value of the object at index nObNum. * * @param nObNum The index of the object to set the value of length. * @param nL The value length is going to be set to. * @return A string containing debugging information. */ public String setVarL( int nObNum, int nL ) { try { if( nObNum <= nTop ) { return oShapeArray[nObNum].setL( nL ); } else { return "ERROR"; } } catch(ArrayIndexOutOfBoundsException e) { return "ERROR"; } } /** * Sets the color value of the object at index nObNum. * * @param nObNum The index of the object to set the color of. * @param c The color the object is going to be. * @return A string containing debugging information. */ public String setVarColor( int nObNum, Color c ) { try { if( nObNum <= nTop ) { return oShapeArray[nObNum].setColor( c ); } else { return "ERROR"; } } catch(ArrayIndexOutOfBoundsException e) { return "ERROR"; } } /** * Sets the fill value of the object at index nObNum. * * @param nObNum The index of the object to set the value of fill. * @param b The value fill is going to be set to. * @return A string containing debugging information. */ public String setVarFill( int nObNum, boolean b ) { try { if( nObNum <= nTop ) { return oShapeArray[nObNum].setFill( b ); } else { return "ERROR"; } } catch(ArrayIndexOutOfBoundsException e) { return "ERROR"; } } /** * Sets the visible value of the object at index nObNum. * * @param nObNum The index of the object to set the value of vis. * @param b The value vis is going to be set to. * @return A string containing debugging information. */ public String setVarVis( int nObNum, boolean b ) { try { if( nObNum <= nTop ) { return oShapeArray[nObNum].setVis( b ); } else { return "ERROR"; } } catch(ArrayIndexOutOfBoundsException e) { return "ERROR"; } } } /** * DrawingObject is the superclass of all the shapes drawn to the drawing field. * It defines all the common methods and variables for the drawing objects. * * @author Steven Vormwald * @version 2.0 */ class DrawingObject { private boolean bDraw = false; protected final String oShape; protected int nXCoord = 0; protected int nYCoord = 0; protected int nWidth = 0; protected int nLength = 0; protected boolean bFill = false; protected Color oColor = Color.black; /** * The constructor for the DrawingObject class. * * @param s The string representation of the shape to be drawn. */ public DrawingObject( String s ) { oShape = s; } /** * The copy constructor for the DrawingObject class. * * @param o The DrawingObject to make a copy of. */ public DrawingObject( DrawingObject o ) { this.oShape = o.oShape; nXCoord = o.nXCoord; nYCoord = o.nYCoord; nWidth = o.nWidth; nLength = o.nLength; bFill = o.bFill; oColor = new Color( o.oColor.getRed(), o.oColor.getGreen(), o.oColor.getBlue() ); if( o.getActive() ) { this.activate(); } else { this.deactivate(); } } /** * Makes the object visible. */ protected void activate() { bDraw = true; } /** * Makes the object invisible. */ protected void deactivate() { bDraw = false; } /** * Gets the visibility status of the object. */ public boolean getActive() { return bDraw; } /** * Sets the x value of the object. * * @param n The value x is going to be set to. * @return A string containing debugging information. */ public String setX( int n ) { if( (n<0) || (n>600) ) { return "ERROR"; } else { if( (n+nWidth) > 600 ) { return "ERROR"; } else { nXCoord = n; return "X COORDINATE SET TO " + nXCoord; } } } /** * Sets the y value of the object. * * @param n The value y is going to be set to. * @return A string containing debugging information. */ public String setY( int n ) { if( (n<0) || (n>600) ) { return "ERROR"; } else { if( (n+nLength) > 600 ) { return "ERROR"; } else { nYCoord = n; return "Y COORDINATE SET TO " + nYCoord; } } } /** * Sets the width value of the object. * * @param n The value width is going to be set to. * @return A string containing debugging information. */ public String setW( int n ) { if( (n<0) || (n>600) ) { return "ERROR"; } else { if( (n+nXCoord) > 600 ) { return "ERROR"; } else { nWidth = n; return "WIDTH SET TO " + nWidth; } } } /** * Sets the length value of the object. * * @param n The value length is going to be set to. * @return A string containing debugging information. */ public String setL( int n ) { if( (n<0) || (n>600) ) { return "ERROR"; } else { if( (n+nYCoord) > 600 ) { return "ERROR"; } else { nLength = n; return "LENGTH SET TO " + nLength; } } } /** * Sets the color of the object. * * @param n The color the object is going to be. * @return A string containing debugging information. */ public String setColor( Color c ) { oColor = c; return "COLOR SET TO " + oColor.getRed() + "R " + oColor.getGreen() + "G " + oColor.getBlue() + "B"; } /** * Sets the fill value of the object. * * @param n The value fill is going to be set to. * @return A string containing debugging information. */ public String setFill( boolean b ) { bFill = b; return "FILL SET TO " + bFill; } /** * Sets the visible value of the object. * * @param n The value visible is going to be set to. * @return A string containing debugging information. */ public String setVis( boolean b ) { if( b ) activate(); else deactivate(); return "VISIBLE SET TO " + getActive(); } /** * Draws the object. * * @param g The graphics object to use. */ public void drawShape( Graphics g ) { g.setColor( oColor ); } /** * Returns the string value of the shape of the object. * * @return A string containing the shape of the object. */ public String getShape() { return oShape; } /** * Gets a string containing a description of the object's variables. * * @return A string containing the values of the objects variables. */ public String viewObject() { String view = "\nShape:\t" + oShape + "\nX:\t" + nXCoord + "\nY:\t" + nYCoord + "\nWidth:\t" + nWidth + "\nLength:\t" + nLength + "\nFill:\t" + bFill + "\nVisible:\t" + getActive(); if( oColor.equals( Color.black ) ) view = view + "\nColor:\t" + oColor.getRed() + "R " + oColor.getGreen() + "G " + oColor.getBlue() + "B ( Black )"; else if( oColor.equals( Color.blue ) ) view = view + "\nColor:\t" + oColor.getRed() + "R " + oColor.getGreen() + "G " + oColor.getBlue() + "B ( Blue )"; else if( oColor.equals( Color.cyan ) ) view = view + "\nColor:\t" + oColor.getRed() + "R " + oColor.getGreen() + "G " + oColor.getBlue() + "B ( Cyan )"; else if( oColor.equals( Color.darkGray ) ) view = view + "\nColor:\t" + oColor.getRed() + "R " + oColor.getGreen() + "G " + oColor.getBlue() + "B ( DarkGray )"; else if( oColor.equals( Color.gray ) ) view = view + "\nColor:\t" + oColor.getRed() + "R " + oColor.getGreen() + "G " + oColor.getBlue() + "B ( Gray )"; else if( oColor.equals( Color.green ) ) view = view + "\nColor:\t" + oColor.getRed() + "R " + oColor.getGreen() + "G " + oColor.getBlue() + "B ( Green )"; else if( oColor.equals( Color.lightGray ) ) view = view + "\nColor:\t" + oColor.getRed() + "R " + oColor.getGreen() + "G " + oColor.getBlue() + "B ( LightGray )"; else if( oColor.equals( Color.magenta ) ) view = view + "\nColor:\t" + oColor.getRed() + "R " + oColor.getGreen() + "G " + oColor.getBlue() + "B ( Magenta )"; else if( oColor.equals( Color.orange ) ) view = view + "\nColor:\t" + oColor.getRed() + "R " + oColor.getGreen() + "G " + oColor.getBlue() + "B ( Orange )"; else if( oColor.equals( Color.pink ) ) view = view + "\nColor:\t" + oColor.getRed() + "R " + oColor.getGreen() + "G " + oColor.getBlue() + "B ( Pink )"; else if( oColor.equals( Color.red ) ) view = view + "\nColor:\t" + oColor.getRed() + "R " + oColor.getGreen() + "G " + oColor.getBlue() + "B ( Red )"; else if( oColor.equals( Color.white ) ) view = view + "\nColor:\t" + oColor.getRed() + "R " + oColor.getGreen() + "G " + oColor.getBlue() + "B ( White )"; else if( oColor.equals( Color.yellow ) ) view = view + "\nColor:\t" + oColor.getRed() + "R " + oColor.getGreen() + "G " + oColor.getBlue() + "B ( Yellow )"; else view = view + "\nColor:\t" + oColor.getRed() + "R " + oColor.getGreen() + "G " + oColor.getBlue() + "B"; return view; } } /** * RectangleObject is the class that holds the variables and methods * specific to drawing a rectangle in the drawing field. * * @author Steven Vormwald * @version 2.0 */ class RectangleObject extends DrawingObject { /** * The constructor for the RectangleObject class. */ public RectangleObject() { super( "Rectangle" ); } /** * The copy constructor for the RectangleObject class. * * @param o The rectangle object to copy. */ public RectangleObject( DrawingObject o ) { super( o ); } /** * Draws the rectangle. * * @param g The graphics object to use. */ public void drawShape( Graphics g ) { super.drawShape( g ); if( this.getActive() ) { if( bFill ) g.fillRect( nXCoord, nYCoord, nWidth, nLength ); else g.drawRect( nXCoord, nYCoord, nWidth, nLength ); } } } /** * OvalObject is the class that holds the variables and methods * specific to drawing a oval in the drawing field. * * @author Steven Vormwald * @version 2.0 */ class OvalObject extends DrawingObject { /** * The constructor for the OvalObject class. */ public OvalObject() { super( "Oval" ); } /** * The copy constructor for the OvalObject class. * * @param o The oval object to copy. */ public OvalObject( DrawingObject o ) { super( o ); } /** * Draws the oval. * * @param g The graphics object to use. */ public void drawShape( Graphics g ) { super.drawShape( g ); if( this.getActive() ) { if( bFill ) g.fillOval( nXCoord, nYCoord, nWidth, nLength ); else g.drawOval( nXCoord, nYCoord, nWidth, nLength ); } } } /** * Command is the superclass of all the command classes. It defines common methods and variables. * * @author Steven Vormwald * @version 2.0 */ class Command { protected DrawingPanel oDrawField; protected MyArray oShapeArray; protected String oCmdString; protected StringTokenizer oCmdTokens; /** * The no-argument constructor for the Command class. */ public Command() { } /** * The single-argument constructor for the Command class. * * @param s The unparsed command string. */ public Command( String s ) { oCmdString = s; oCmdTokens = new StringTokenizer( s ); } /** * Performs the command. All sub-classes over-ride this. * * @return The output of the command. */ public String runCommand() { return "ERROR:\nSHOULD NEVER SEE THIS"; } } /** * TextCommand is the work-horse command class that decides which command the user * wished to call, then instantiates an object of that command and runs it. * * @author Steven Vormwald * @version 2.0 */ class TextCommand extends Command { private Command oCmd; private boolean bIsCommand = false; private final String [][] oList = { { "HELP", "LIST", "NUMBER", "REDRAW" }, // 0-Argument commands { "HELP", "RAISE", "LOWER", "VIEW", "DELETE", "NUMBER", "NEW", "COPY" }, // 1-Argument commands { "BACKGROUND" }, // 2-Argument commands { "SETVAR", "COPY" }, // 3-Argument commands { "BACKGROUND" }, // 4-Argument commands { "SETVAR" } }; // 5-Argument commands /** * The constructor for the TextCommand class. * * @param s The unparsed command string. * @param o The applet's drawing panel (where the shapes appear). */ public TextCommand( String s, DrawingPanel o ) { super( s ); oDrawField = o; } /** * Performs the command. * * @return The output of the command. */ public String runCommand() { int nNumTokens = oCmdTokens.countTokens(); String sFirstToken = oCmdTokens.nextToken().trim(); int i; for(i=0;i 4 ) { return "ERROR:\nHAVEN\'T IMPLEMENTED ALL COMMANDS IN TextCommand.oList[0] YET."; } } break; } case 2: // HELP, RAISE, LOWER, VIEW, DELETE, NUMBER, NEW, COPY { for(i=0;i 8 ) { return "ERROR:\nHAVEN\'T IMPLEMENTED ALL COMMANDS IN TextCommand.oList[1] YET."; } } break; } case 3: // BACKGROUND { for(i=0;i 1 ) { return "ERROR:\nHAVEN\'T IMPLEMENTED ALL COMMANDS IN TextCommand.oList[2] YET."; } } break; } case 4: // SETVAR, COPY { for(i=0;i 2 ) { return "ERROR:\nHAVEN\'T IMPLEMENTED ALL COMMANDS IN TextCommand.oList[3] YET."; } } break; } case 5: // BACKGROUND { for(i=0;i 1 ) { return "ERROR:\nHAVEN\'T IMPLEMENTED ALL COMMANDS IN TextCommand.oList[4] YET."; } } break; } case 6: // SETVAR { for(i=0;i 1 ) { return "ERROR:\nHAVEN\'T IMPLEMENTED ALL COMMANDS IN TextCommand.oList[5] YET."; } } break; } } if(bIsCommand) return "SYNTAX ERROR:\nTYPE \'HELP " + sFirstToken.toUpperCase() + "\' FOR PROPER SYNTAX"; else return "ERROR:\nUNKNOWN COMMAND:\n" + oCmdString + ":\nTYPE HELP FOR A LIST OF COMMANDS"; } } /** * SetvarCommand is the command class that takes care of setting variable values of objects in the array. * * @author Steven Vormwald * @version 2.1 */ class SetvarCommand extends Command { private final String [] oCList = { "black", "blue", "cyan", "darkGray", "gray", "green", "lightGray", "magenta", "orange", "pink", "red", "white", "yellow" }; private final String [] oList = { "X", "Y", "WIDTH", "LENGTH", "COLOR", "FILL", "VISIBLE" }; private int nObNum; /** * The constructor for the SetvarCommand class. * * @param s The StringTokenizer created in TextCommand containing all the arguments. * @param o The applet's drawing panel (where the shapes appear). */ public SetvarCommand( StringTokenizer s, DrawingPanel o ) { oShapeArray = o.getArray(); oCmdTokens = s; try { nObNum = Integer.parseInt( oCmdTokens.nextToken().trim() ); } catch( NoSuchElementException e ) { nObNum = -1; } catch( NumberFormatException e ) { nObNum = -1; } } /** * Performs the command. * * @return The output of the command. */ public String runCommand() { try { int nNumTokens = oCmdTokens.countTokens(); String sFirstToken = oCmdTokens.nextToken().trim(); int i; for(i=0;i 255) ) { return "SYNTAX ERROR:\nRGB VALUES MUST BE BETWEEN 0 AND 255 INCLUSIVE"; } } return "ATTEMPTING TO SET BACKGROUND TO NEW RGB VALUE\n" + oShapeArray.setVarColor( nObNum, new Color( nRGB[0], nRGB[1], nRGB[2] ) ); } default: { return "SYNTAX ERROR:\nTYPE \'HELP COLOR\' FOR PROPER SYNTAX"; } } } case 5: // Setting FILL variable { sFirstToken = oCmdTokens.nextToken().trim(); if( sFirstToken.equalsIgnoreCase( "TRUE" ) ) { return oShapeArray.setVarFill( nObNum, true ); } else if( sFirstToken.equalsIgnoreCase( "FALSE" ) ) { return oShapeArray.setVarFill( nObNum, true ); } else { return "SYNTAX ERROR:\nTYPE \'HELP FILL\' FOR PROPER SYNTAX"; } } case 6: // Setting VISIBLE variable { sFirstToken = oCmdTokens.nextToken().trim(); if( sFirstToken.equalsIgnoreCase( "TRUE" ) ) { return oShapeArray.setVarVis( nObNum, true ); } else if( sFirstToken.equalsIgnoreCase( "FALSE" ) ) { return oShapeArray.setVarVis( nObNum, true ); } else { return "SYNTAX ERROR:\nTYPE \'HELP VISIBLE\' FOR PROPER SYNTAX"; } } default: // Unknown variable { return "SYNTAX ERROR:\nTYPE \'HELP SETVAR\' FOR PROPER SYNTAX"; } } } catch( NoSuchElementException e ) { return "SYNTAX ERROR:\nTYPE \'HELP SETVAR\' FOR PROPER SYNTAX"; } catch( NumberFormatException e ) { return "SYNTAX ERROR:\nTYPE \'HELP SETVAR\' FOR PROPER SYNTAX"; } } } /** * HelpCommand displays all the help documentation. * * @author Steven Vormwald * @version 2.0 */ class HelpCommand extends Command { private final String [] oList = { "HELP", "RAISE", "LOWER", "LIST", "VIEW", "DELETE", "NUMBER", "SETVAR", "NEW", "REDRAW", "COPY", "allyourbase", "bomb", "X", "Y", "WIDTH", "LENGTH", "COLOR", "FILL", "VISIBLE", "BACKGROUND" }; private String sFirstToken; /** * The no-argument constructor for the HelpCommand class. */ public HelpCommand() { oCmdString = ""; } /** * The single-argument constructor for the HelpCommand class. * * @param s The StringTokenizer created in TextCommand containing all the arguments. */ public HelpCommand( StringTokenizer s ) { oCmdTokens = s; oCmdString = "oCmdString"; } /** * Performs the command. * * @return The output of the command. */ public String runCommand() { int i; if( oCmdString.equals("") ) { i = -1; } else { sFirstToken = oCmdTokens.nextToken().trim(); for(i=0;i TYPE \'HELP [CMD]\' FOR COMMAND SPECIFIC HELP.\n" + "=> TYPE \'HELP [VARS]\' FOR VARIABLE SPECIFIC HELP.\n" + "=> NOTICE, WHEN \'[CMD]\' APPEARS, IT REFERS TO ANY OF THE\n" + " ABOVE COMMANDS\n" + "=> NOTICE, WHEN \'[VARS]\' APPEARS, IT REFERS TO ANY OF THE\n" + " ABOVE VARIABLES\n" + "=> NOTICE, WHEN \'[INTEGER]\' APPEARS, IT REFERS TO AN INTEGER\n" + " AS DESCRIBED IN THE DESCRIPTION\n" + "=> NOTICE, WHEN \'[VALUE]\' APPEARS, IT REFERS TO A VALUE\n" + " SPECIFIED IN THE DESCRIPTION OF THE PRECEDING VARIABLE\n" + "=> NOTICE, WHEN MULTIPLE WORDS APPEAR IN BRACKETS\n" + " ( \'{\' AND \'}\' ) AND ARE SEPARATED BY A PIPE ( \'|\' ),\n" + " IT REFERS TO CHOOSING ONE OF THE WORDS.\n" + "=> ALL COMMANDS ARE CASE INSENSITIVE.\n" + "=> THERE CAN BE A MAXIMUM OF 5 OVALS AND 5 RECTANGLES\n" + " IN THE STACK AT A TIME, AND A MAXIMUM OF 10 OBJECTS.\n" + "=> NOW YOU CAN HIT THE UP OR DOWN ARROWS TO ACCESS THE\n" + " COMMAND HISTORY.\n" + "=> NOW YOU CAN HIT THE RIGHT ARROW TO COMPLETE A\n" + " COMMAND ON THE COMMAND LINE BY TYPING THE FIRST\n" + " COUPLE LETTERS OF THE COMMAND AND HITTING THE\n" + " RIGHT ARROW KEY.\n" + "=> NOW YOU CAN HAVE MULTIPLE COMMANDS ON A LINE\n" + " BY SEPARATING THE COMMANDS WITH THE SEMICOLON\n" + " ';' CHARACTER."; case 0: // Help with the HELP command. return "HELP:\nSYNTAX:\n\tHELP\n\tHELP { [CMD] | [VARS] }\n" + "DESCRIPTION:\n\tHELP LISTS THE POSSIBLE COMMANDS, \n\t" + "THEIR ARGUMENTS, THEIR USAGE, \n\t"+ "AND OTHER PERTANENT INFORMATION THEREOF.\n" + "EXAMPLES:\n\tHELP\n\tHELP RAISE\n\tHELP COLOR\n" + "SEE ALSO:"; case 1: // Help with the RAISE command. return "RAISE:\nSYNTAX:\n\tRAISE [INTEGER]\n" + "DESCRIPTION:\n\tRAISE ELEVATES THE OBJECT INDEXED\n\t" + "BY THE NUMBER INDICATED BY THE FIRST \n\t"+ "ARGUMENT. THIS NUMBER MUST BE AN \n\t" + "INTEGER BETWEEN 0 AND 8 INCLUSIVE.\n" + "EXAMPLES:\n\tRAISE 0\n\tRAISE 8\n\tRAISE 4\n" + "SEE ALSO:\n\tHELP LIST\n\tLIST\n\tHELP LOWER"; case 2: // Help with the LOWER command. return "LOWER\nSYNTAX:\n\tLOWER [INTEGER]\n" + "DESCRIPTION:\n\tLOWER LOWERS THE OBJECT INDEXED\n\t" + "BY THE NUMBER INDICATED BY THE FIRST \n\t"+ "ARGUMENT. THIS NUMBER MUST BE AN \n\t" + "INTEGER BETWEEN 1 AND 9 INCLUSIVE.\n" + "EXAMPLES:\n\tLOWER 1\n\tLOWER 9\n\tLOWER 5\n" + "SEE ALSO:\n\tHELP LIST\n\tLIST\n\tHELP RAISE"; case 3: // Help with the LIST command. return "LIST\nSYNTAX:\n\tLIST\n" + "DESCRIPTION:\n\tLIST DISPLAYS THE CONTENTS OF THE\n\t" + "STACK AS IT CURRENTLY IS STORED BY INDEX.\n"+ "EXAMPLE:\n\tLIST\n" + "SEE ALSO:\n\tHELP VIEW\n\tVIEW\n\tHELP NUMBER\n\tNUMBER"; case 4: // Help with the VIEW command. return "VIEW\nSYNTAX:\n\tVIEW [INTEGER]\n"+ "DESCRIPTION:\n\tVIEW DISPLAYS THE CURRENT VALUES OF\n\t" + "THE VARIABLES OF THE OBJECT STORED\n\t" + "AT THE INDEX PASSED TO VIEW.\n" + "EXAMPLES:\n\tVIEW 0\n\tVIEW 9\n\tVIEW 4\n" + "SEE ALSO:\n\tHELP LIST\n\tLIST\n\tHELP NUMBER\n\tNUMBER"; case 5: // Help with the DELETE command. return "DELETE\nSYNTAX:\n\tDELETE { [INTEGER] | ALL }\n"+ "DESCRIPTION:\n\tDELETE REMOVES THE OBJECT STORED AT\n\t" + "THE INDEX PASSED TO DELETE FROM\n\t" + "THE STACK. THE INDEX MUST BE BETWEEN\n\t" + "0 AND 9 INCLUSIVE. IF ALL IS PASSED TO\n\t" + "DELETE, THE STACK IS CLEARED.\n" + "EXAMPLES:\n\tDELETE 0\n\tDELETE 9\n\tDELETE 4\n\tDELETE ALL\n" + "SEE ALSO:\n\tHELP NEW\n\tNEW\n\tHELP LIST\n\tLIST\n\tHELP VIEW\n\tVIEW"; case 6: // Help with the NUMBER command. return "NUMBER\nSYNTAX:\n\tNUMBER\n\tNUMBER { OVAL | RECTANGLE }\n" + "DESCRIPTION:\n\tNUMBER DISPLAYS THE NUMBER OF OBJECTS\n\t" + "CURRENTLY IN THE STACK, OR, IF PASSED\n\t" + "A TYPE ( OVAL OR RECTANGLE ), THE\n\t" + "NUMBER OF OBJECTS OF THAT TYPE CURRENTLY\n\t" + "IN THE STACK AND THE MAXIMUN NUMBER OF\n\t" + "OBJECTS (OR OF THE TYPE)\n" + "EXAMPLES:\n\tNUMBER\n\tNUMBER OVAL\n\tNUMBER RECTANGLE\n" + "SEE ALSO:\n\tHELP LIST\n\tLIST"; case 7: // Help with the SETVAR command. return "SETVAR\nSYNTAX:\n\tSETVAR [INTEGER] [VARS] [VALUE]\n\tSETVAR [INTEGER] [VARS] [VALUE] [VALUE] [VALUE]\n" + "DESCRIPTION:\n\tSETVAR SETS A VALUE OF THE VARIABLE\n\t" + "IN THE OBJECT AT THE INDEX PASSED TO SETVAR.\n\t" + "THESE VALUES CONTROL WHAT IS DISPLAYED,\n\t" + "AND MUST BE EDITED BEFORE ANYTHING WILL\n\t" + "BE DISPLAYED. PLEASE REFER TO THE HELP\n\t" + "ON INDIVIDUAL VARIABLES TO FIND THE\n\t" + "SYNTAX SPECIFIC TO THAT VARIABLE.\n" + "EXAMPLES:\n\tSETVAR 0 COLOR BLUE\n\tSETVAR 9 COLOR 255 245 235\n\tSETVAR 4 VISIBLE TRUE\n" + "SEE ALSO:\n\tHELP X\n\tHELP Y\n\tHELP WIDTH\n\tHELP LENGTH\n\tHELP COLOR\n\tHELP FILL\n\tHELP VISIBLE\n\tHELP BACKGROUND"; case 8: // Help with the NEW command. return "NEW\nSYNTAX:\n\tNEW { OVAL | RECTANGLE }\n" + "DESCRIPTION:\n\tNEW CREATES A NEW OBJECT OF THE TYPE\n\t" + "SPECIFIED AS AN ARGUMENT. THIS MUST\n\t" + "BE DONE BEFORE ANY OBJECTS CAN BE EDITED.\n" + "EXAMPLES:\n\tNEW OVAL\n\tNEW RECTANGLE\n" + "SEE ALSO:\n\tHELP DELETE\n\tDELETE\n\tHELP COPY\n\tCOPY\n\tHELP"; case 9: // Help with the REDRAW command. return "REDRAW\nSYNTAX:\n\tREDRAW\n" + "DESCRIPTION:\n\tREDRAW CAUSES THE DRAW WINDOW TO BE\n\t" + "REPAINTED, SHOWING ANY CHANGES MADE\n\t" + "SINCE THE LAST REDRAW CALL. NOTE REDRAW\n\t" + "MUST BE CALLED TO DISPLAY ALL CHANGES TO \n\t" + "THE DRAWING FIELD EXCEPT BACKGROUND \n\t" + "CHANGES.\n" + "EXAMPLE:\n\tREDRAW\n" + "SEE ALSO:"; case 10: // Help with the COPY command. return "COPY\nSYNTAX:\n\tCOPY [INTEGER]\n\tCOPY [INTEGER] TO [INTEGER]\n" + "DESCRIPTION:\n\tCOPY DOES 2 THINGS: IT COPIES THE\n\t" + "OBJECT INDEXED BY THE INTEGER PASSED TO\n\t" + "IT TO A NEW OBJECT THAT IS THEN PLACED\n\t" + "ON THE TOP OF THE STACK, OR IT COPIES\n\t" + "THE OBJECT INDEXED BY THE FIRST INTEGER\n\t" + "PASSED TO IT TO THE SECOND OBJECT INDEXED\n\t" + "BY THE SECOND INTEGER PASSED TO IT, OVER-\n\t" + "WRITING WHATEVER WAS THERE.\n" + "EXAMPLES:\n\tCOPY 0\n\tCOPY 4 TO 2\n" + "SEE ALSO:\n\tHELP NEW\n\tNEW"; case 11: // All Your Base easter egg. return "ERROR:\nAll Your Base Are Belong To Us."; case 12: // Bomb easter egg. return "ERROR:\nSomeone Set Up Us The Bomb."; case 13: // Help with the X variable return "X VARIABLE\nSYNTAX IN SETVAR:\n\tSETVAR [INTEGER] X [VALUE]\n" + "DESCRIPTION:\n\tX IS THE X-COORDINATE FOR THE UPPER-\n\t" + "LEFT CORNER OF THE OBJECT INDEXED BY THE\n\t" + "INTEGER PASSED TO SETVAR. IT\'S VALUE CAN BE\n\t" + "ANY INTEGER BETWEEN 0 AND 600 INCLUSIVE, BUT\n\t" + "THE SUM OF X AND WIDTH MUST BE LESS THAN OR\n\t" + "EQUAL TO 600.\n" + "EXAMPLE OF SETVAR COMMAND WITH X:\n\tSETVAR 0 X 35\n" + "SEE ALSO:\n\tHELP SETVAR\n\tSETVAR\n\tHELP Y"; case 14: // Help with the Y variable return "Y VARIABLE\nSYNTAX IN SETVAR:\n\tSETVAR [INTEGER] Y [VALUE]\n" + "DESCRIPTION:\n\tY IS THE Y-COORDINATE FOR THE UPPER-\n\t" + "LEFT CORNER OF THE OBJECT INDEXED BY THE\n\t" + "INTEGER PASSED TO SETVAR. IT\'S VALUE CAN BE\n\t" + "ANY INTEGER BETWEEN 0 AND 600 INCLUSIVE, BUT\n\t" + "THE SUM OF Y AND LENGTH MUST BE LESS THAN OR\n\t" + "EQUAL TO 600.\n" + "EXAMPLE OF SETVAR COMMAND WITH Y:\n\tSETVAR 0 Y 35\n" + "SEE ALSO:\n\tHELP SETVAR\n\tSETVAR\n\tHELP X"; case 15: // Help with the WIDTH variable return "WIDTH VARIABLE\nSYNTAX IN SETVAR:\n\tSETVAR [INTEGER] WIDTH [VALUE]\n" + "DESCRIPTION:\n\tWIDTH IS THE HORIZONTAL LENGTH OF THE OBJECT\n\t" + "INDEXED BY THE INTEGER PASSED TO SETVAR.\n\t" + "IT\'S VALUE CAN BE ANY INTEGER BETWEEN 0 \n\t" + "AND 600 INCLUSIVE, BUT THE SUM OF X AND \n\t" + "WIDTH MUST BE LESS THAN OR EQUAL TO 600.\n" + "EXAMPLE OF SETVAR COMMAND WITH WIDTH:\n\tSETVAR 0 WIDTH 35\n" + "SEE ALSO:\n\tHELP SETVAR\n\tSETVAR\n\tHELP LENGTH"; case 16: // Help with the LENGTH variable return "LENGTH VARIABLE\nSYNTAX IN SETVAR:\n\tSETVAR [INTEGER] LENGTH [VALUE]\n" + "DESCRIPTION:\n\tLENGTH IS THE VERTICAL LENGTH OF THE OBJECT\n\t" + "INDEXED BY THE INTEGER PASSED TO SETVAR.\n\t" + "IT\'S VALUE CAN BE ANY INTEGER BETWEEN 0 \n\t" + "AND 600 INCLUSIVE, BUT THE SUM OF Y AND \n\t" + "LENGTH MUST BE LESS THAN OR EQUAL TO 600.\n" + "EXAMPLE OF SETVAR COMMAND WITH LENGTH:\n\tSETVAR 0 LENGTH 35\n" + "SEE ALSO:\n\tHELP SETVAR\n\tSETVAR\n\tHELP WIDTH"; case 17: // Help with the COLOR variable return "COLOR VARIABLE\nSYNTAX IN [CMD]:\n\t[CMD] COLOR [VALUE]\n\tSETVAR [INTEGER] COLOR [VALUE] [VALUE] [VALUE]\n" + "DESCRIPTION:\n\tCOLOR IS THE COLOR OF THE OBJECT INDICATED\n\t" + "BY THE COMMAND. IT\'S VALUE IF USING\n\t" + "THE SINGLE VALUE FORM CAN BE ANY OF\n\t" + "THE FOLLOWING:\n\t\t" + "BLACK\n\t\t" + "BLUE\n\t\t" + "CYAN\n\t\t" + "DARKGRAY\n\t\t" + "GRAY\n\t\t" + "GREEN\n\t\t" + "LIGHTGRAY\n\t\t" + "MAGENTA\n\t\t" + "ORANGE\n\t\t" + "PINK\n\t\t" + "RED\n\t\t" + "WHITE\n\t\t" + "YELLOW\n\t" + "OR IT\'S THREE VALUES IF USING THE THREE\n\t" + "VALUE FORM ARE INTEGERS BETWEEN 0 AND 255\n\t" + "INCLUSIVE THAT REPRESENT THE RED, GREEN,\n\t" + "AND BLUE VALUES RESPECTIVELY OF THE DESIRED\n\t" + "COLOR.\n" + "EXAMPLES OF COLOR IN COMMANDS:\n\tSETVAR 0 COLOR BLUE\n\tSETVAR 9 COLOR 255 245 235\n\tBACKGROUND COLOR BLUE\n\tBACKGROUND COLOR 255 245 235\n" + "SEE ALSO:\n\tHELP SETVAR\n\tSETVAR\n\tHELP BACKGROUND"; case 18: // Help with the FILL variable return "FILL VARIABLE\nSYNTAX IN SETVAR:\n\tSETVAR [INTEGER] FILL { TRUE | FALSE }\n" + "DESCRIPTION:\n\tFILL DETERMINES WHETHER OR NOT THE OBJECT\n\t" + "INDEXED BY THE INTEGER PASSED TO SETVAR IS\n\t" + "FILLED IN OR NOT. IF TRUE, IT IS FILLED IN\n\t" + "AND IF FALSE, IT IS NOT.\n" + "EXAMPLES OF SETVAR COMMAND WITH FILL:\n\tSETVAR 0 FILL TRUE\n\tSETVAR 9 FILL FALSE\n" + "SEE ALSO:\n\tHELP SETVAR\n\tSETVAR"; case 19: // Help with the VISIBLE variable return "VISIBLE VARIABLE\nSYNTAX IN SETVAR:\n\tSETVAR [INTEGER] VISIBLE { TRUE | FALSE }\n" + "DESCRIPTION:\n\tVISIBLE DETERMINES WHETHER OR NOT THE\n\t" + "OBJECT INDEXED BY THE INTEGER PASSED TO \n\t" + "SETVAR IS DRAWN IN OR NOT. IF TRUE, IT \n\t" + "IS DRAWN IN AND IF FALSE, IT IS NOT.\n" + "EXAMPLES OF SETVAR COMMAND WITH VISIBLE:\n\tSETVAR 0 VISIBLE TRUE\n\tSETVAR 9 VISIBLE FALSE\n" + "SEE ALSO:\n\tHELP SETVAR\n\tSETVAR"; case 20: // Help with the BACKGROUND command return "BACKGROUND\nSYNTAX:\n\tBACKGROUND COLOR [VALUE]\n\tBACKGROUND COLOR [VALUE] [VALUE] [VALUE]\n" + "DESCRIPTION:\n\tBACKGROUND SETS THE COLOR OF THE DRAWING\n\t" + "FIELD\'S BACKGROUND.\n" + "EXAMPLES:\n\tBACKGROUND COLOR BLUE\n\tBACKGROUND COLOR 255 245 235\n" + "SEE ALSO:\n\tHELP SETVAR\n\tSETVAR\n\tHELP COLOR"; default: // DEBUGGING: check for new commands in oList and display error if not implemented yet... if( oList.length > 21 ) { return "ERROR:\nHAVEN\'T IMPLEMENTED ALL COMMANDS IN Help.oList YET."; } return "SYNTAX ERROR:\nTYPE \'HELP HELP\' FOR PROPER SYNTAX"; } } } /** * RaiseCommand raises an object in the array, effectively raising it's z-value. * * @author Steven Vormwald * @version 2.0 */ class RaiseCommand extends Command { private boolean bLegal; private boolean bIsInt; private int nIndex; private String sIndex; /** * The constructor for the RaiseCommand class. * * @param s The StringTokenizer created in TextCommand containing all the arguments. * @param o The applet's drawing panel (where the shapes appear). */ public RaiseCommand( StringTokenizer s, DrawingPanel o ) { oShapeArray = o.getArray(); oCmdTokens = s; sIndex = oCmdTokens.nextToken().trim(); try { int nIndex = Integer.parseInt( sIndex ); bIsInt = true; if( (nIndex >= 0) && (nIndex < (oShapeArray.length() - 1)) ) bLegal = true; } catch( NumberFormatException e ) { bLegal = false; } } /** * Performs the command. * * @return The output of the command. */ public String runCommand() { if(bIsInt) { if( bLegal ) { return oShapeArray.raiseObject( nIndex ); } else { return "SYNTAX ERROR:\nTYPE \'HELP RAISE\' FOR PROPER SYNTAX"; } } else { return "SYNTAX ERROR:\nTYPE \'HELP RAISE\' FOR PROPER SYNTAX"; } } } /** * LowerCommand does the opposite of RaiseCommand, lowering the effective z-value of the drawing object. * * @author Steven Vormwald * @version 2.0 */ class LowerCommand extends Command { private boolean bLegal; private int nIndex; private String sIndex; /** * The constructor for the LowerCommand class. * * @param s The StringTokenizer created in TextCommand containing all the arguments. * @param o The applet's drawing panel (where the shapes appear). */ public LowerCommand( StringTokenizer s, DrawingPanel o ) { oShapeArray = o.getArray(); oCmdTokens = s; sIndex = oCmdTokens.nextToken().trim(); try { nIndex = Integer.parseInt( sIndex ); if( (nIndex > 0) && (nIndex < oShapeArray.length()) ) bLegal = true; } catch( NumberFormatException e ) { bLegal = false; } } /** * Performs the command. * * @return The output of the command. */ public String runCommand() { if(bLegal) { return oShapeArray.lowerObject( nIndex ); } else { return "SYNTAX ERROR:\nTYPE \'HELP LOWER\' FOR PROPER SYNTAX"; } } } /** * ListCommand lists the contents of the array. * * @author Steven Vormwald * @version 2.0 */ class ListCommand extends Command { /** * The constructor for the ListCommand class. * * @param o The applet's drawing panel (where the shapes appear). */ public ListCommand( DrawingPanel o ) { oShapeArray = o.getArray(); } /** * Performs the command. * * @return The output of the command. */ public String runCommand() { return oShapeArray.listObjects(); } } /** * ViewCommand prints out the properties of the desired object. * * @author Steven Vormwald * @version 2.0 */ class ViewCommand extends Command { private boolean bLegal; private int nIndex; private String sIndex; /** * The constructor for the ViewCommand class. * * @param s The StringTokenizer created in TextCommand containing all the arguments. * @param o The applet's drawing panel (where the shapes appear). */ public ViewCommand( StringTokenizer s, DrawingPanel o ) { oShapeArray = o.getArray(); oCmdTokens = s; sIndex = oCmdTokens.nextToken().trim(); try { nIndex = Integer.parseInt( sIndex ); if( (nIndex >= 0) && (nIndex < oShapeArray.length()) ) bLegal = true; } catch( NumberFormatException e ) { bLegal = false; } } /** * Performs the command. * * @return The output of the command. */ public String runCommand() { if(bLegal) { return oShapeArray.viewObject( nIndex ); } else { return "SYNTAX ERROR:\nTYPE \'HELP VIEW\' FOR PROPER SYNTAX"; } } } /** * DeleteCommand deletes an object from the array. * * @author Steven Vormwald * @version 2.0 */ class DeleteCommand extends Command { private boolean bLegal = false; private boolean bAll = false; private int nIndex; private String sIndex; /** * The constructor for the SetvarCommand class. * * @param s The StringTokenizer created in TextCommand containing all the arguments. * @param o The applet's drawing panel (where the shapes appear). */ public DeleteCommand( StringTokenizer s, DrawingPanel o ) { oShapeArray = o.getArray(); oCmdTokens = s; sIndex = oCmdTokens.nextToken().trim(); try { nIndex = Integer.parseInt( sIndex ); if( (nIndex >= 0) && (nIndex < oShapeArray.length()) ) bLegal = true; } catch( NumberFormatException e ) { if( sIndex.equalsIgnoreCase( "ALL" ) ) { bLegal = false; bAll = true; } else { bLegal = false; } } } /** * Performs the command. * * @return The output of the command. */ public String runCommand() { if(bLegal) { return oShapeArray.deleteObject( nIndex ); } else { if(bAll) { return oShapeArray.deleteAll(); } else { return "SYNTAX ERROR:\nTYPE \'HELP DELETE\' FOR PROPER SYNTAX"; } } } } /** * NumberCommand prints out the current number of objects * (Oval, Rectangle, or Drawing) versus the maximum number. * * @author Steven Vormwald * @version 2.0 */ class NumberCommand extends Command { private final String [] oList = { "RECTANGLE", "OVAL" }; private String sFirstToken; /** * The constructor for the NumberCommand class. * * @param o The applet's drawing panel (where the shapes appear). */ public NumberCommand( DrawingPanel o ) { oCmdString = ""; oShapeArray = o.getArray(); } /** * The constructor for the NumberCommand class. * * @param s The StringTokenizer created in TextCommand containing all the arguments. * @param o The applet's drawing panel (where the shapes appear). */ public NumberCommand( StringTokenizer s, DrawingPanel o ) { oCmdTokens = s; oCmdString = "oCmdString"; oShapeArray = o.getArray(); } /** * Performs the command. * * @return The output of the command. */ public String runCommand() { int i; if( oCmdString.equals("") ) { i = -1; } else { sFirstToken = oCmdTokens.nextToken().trim(); for(i=0;i= 0) && (nIndex[0] < oShapeArray.length()) ) b1Legal = true; } catch( NumberFormatException e ) { b1Legal = false; } if( sIndex[1].equalsIgnoreCase( "TO" ) ) { b2Legal = true; } try { nIndex[1] = Integer.parseInt( sIndex[2] ); if( (nIndex[1] >= 0) && (nIndex[1] < oShapeArray.length()) ) b3Legal = true; } catch( NumberFormatException e ) { b2Legal = false; } } /** * The constructor for the CopyCommand class. * * @param n The number arguments passed to copy. * @param s The StringTokenizer created in TextCommand containing all the arguments. * @param o The applet's drawing panel (where the shapes appear). */ public CopyCommand( int n, StringTokenizer s, DrawingPanel o ) { if( n == 1 ) { oShapeArray = o.getArray(); oCmdTokens = s; sIndex[0] = oCmdTokens.nextToken().trim(); try { nIndex[0] = Integer.parseInt( sIndex[0] ); if( (nIndex[0] >= 0) && (nIndex[0] < oShapeArray.length()) ) b1Legal = true; } catch( NumberFormatException e ) { b1Legal = false; } } } /** * Performs the command. * * @return The output of the command. */ public String runCommand() { if(b1Legal) { if(b2) { if(b2Legal) { if(b3Legal) { return oShapeArray.copyObject( nIndex[0], nIndex[1] ); } else { return "SYNTAX ERROR:\nTYPE \'HELP COPY\' FOR PROPER SYNTAX"; } } else { return "SYNTAX ERROR:\nTYPE \'HELP COPY\' FOR PROPER SYNTAX"; } } else { return oShapeArray.copyObject( nIndex[0] ); } } else { return "SYNTAX ERROR:\nTYPE \'HELP COPY\' FOR PROPER SYNTAX"; } } } /** * BackgroundCommand sets the background color of the drawing field. * * @author Steven Vormwald * @version 2.1 */ class BackgroundCommand extends Command { private final String [] oCList = { "black", "blue", "cyan", "darkGray", "gray", "green", "lightGray", "magenta", "orange", "pink", "red", "white", "yellow" }; private String [] sArgList = new String[4]; private int nRGB[] = new int[3]; private int nCIndex; private boolean bRGB = false; private boolean bLegal[] = new boolean[4]; /** * The constructor for the BackgroundCommand class. * * @param n The number arguments passed to copy. * @param s The StringTokenizer created in TextCommand containing all the arguments. * @param o The applet's drawing panel (where the shapes appear). */ public BackgroundCommand( int n, StringTokenizer s, DrawingPanel o ) { if( n == 2 ) { bRGB = false; oDrawField = o; oCmdTokens = s; for(int i=0; i= 0) && (nRGB[i-1] < 256) ) bLegal[i] = true; } catch( NumberFormatException e ) { bLegal[i] = false; } } } /** * Performs the command. * * @return The output of the command. */ public String runCommand() { if( bRGB ) { if( bLegal[0] ) { if( bLegal[1] ) { if( bLegal[2] ) { if( bLegal[3] ) { oDrawField.setBackground( new Color( nRGB[0], nRGB[1], nRGB[2] ) ); return "ATTEMPTING TO SET BACKGROUND TO NEW RGB VALUE\n"; } else { return "SYNTAX ERROR:\nTYPE \'HELP COLOR\' FOR PROPER SYNTAX"; } } else { return "SYNTAX ERROR:\nTYPE \'HELP COLOR\' FOR PROPER SYNTAX"; } } else { return "SYNTAX ERROR:\nTYPE \'HELP COLOR\' FOR PROPER SYNTAX"; } } else { return "SYNTAX ERROR:\nTYPE \'HELP BACKGROUND\' FOR PROPER SYNTAX"; } } else { if( bLegal[0] ) { if( bLegal[1] ) { switch( nCIndex ) { case 0: // black oDrawField.setBackground( Color.black ); return "ATTEMPTING TO SET BACKGROUND TO BLACK\n"; case 1: // blue oDrawField.setBackground( Color.blue ); return "ATTEMPTING TO SET BACKGROUND TO BLUE\n"; case 2: // cyan oDrawField.setBackground( Color.cyan ); return "ATTEMPTING TO SET BACKGROUND TO CYAN\n"; case 3: // darkGray oDrawField.setBackground( Color.darkGray ); return "ATTEMPTING TO SET BACKGROUND TO DARK GRAY\n"; case 4: // gray oDrawField.setBackground( Color.gray ); return "ATTEMPTING TO SET BACKGROUND TO GRAY\n"; case 5: // green oDrawField.setBackground( Color.green ); return "ATTEMPTING TO SET BACKGROUND TO GREEN\n"; case 6: // lightGray oDrawField.setBackground( Color.lightGray ); return "ATTEMPTING TO SET BACKGROUND TO LIGHT GRAY\n"; case 7: // magenta oDrawField.setBackground( Color.magenta ); return "ATTEMPTING TO SET BACKGROUND TO MAGENTA\n"; case 8: // orange oDrawField.setBackground( Color.orange ); return "ATTEMPTING TO SET BACKGROUND TO ORANGE\n"; case 9: // pink oDrawField.setBackground( Color.pink ); return "ATTEMPTING TO SET BACKGROUND TO PINK\n"; case 10: // red oDrawField.setBackground( Color.red ); return "ATTEMPTING TO SET BACKGROUND TO RED\n"; case 11: // white oDrawField.setBackground( Color.white ); return "ATTEMPTING TO SET BACKGROUND TO WHITE\n"; case 12: // yellow oDrawField.setBackground( Color.yellow ); return "ATTEMPTING TO SET BACKGROUND TO YELLOW\n"; } return "ERROR:\nSHOULD NEVER SEE THIS"; } else { return "SYNTAX ERROR:\nTYPE \'HELP COLOR\' FOR PROPER SYNTAX"; } } else { return "SYNTAX ERROR:\nTYPE \'HELP BACKGROUND\' FOR PROPER SYNTAX"; } } } }