Program 6 - Command Line

Goal Description Hints Passoff

Due Date - 6 Apr

Goal

 
To learn to parse commands read from user input. To learn more string processing methods. To work with arrays more.
CS 142
Description
 

You should create a program that opens a new Win2D. It then starts reading lines from System.in and performs commands on those lines. Each command starts with the name of the command followed by argument values that tell how each command should be performed.

Step 1 - create a place to store DrawObj objects by name.

You should create a class for holding DrawObj objects and finding them by name. First create a class NamedObj that contains the fields name and object. These are both public.

Then create a class ObjTable. This clas should contain a private field that is an array of NamedObj. There should also be a nObjects field telling you how many objects have been placed in the array. The clas ObjTable should have the following methods:

  • void add(String name, DrawObj obj) - This should create a new NamedObj object and put the name and object in it. The new object should be added to the private array and the number of objects in the array should be incremented. The new object can be added to the array at the index specified by nObjects.
  • DrawObj find(String name) - this should loop through all of the objects that you have added to your array (zero through nObjects-1) looking for an object whose name matches the name parameter. Use equalsIgnoreCase() for your matching.

Test your ObjTable before going on so that you are sure that it is working correctly.

Step 2 - process command lines.

Create a loop in your main program that will continuously read command lines from System.in and do what they say to do. For each command you should output a question mark "?" to tell the user that you are ready for a new command.

Each command has a name and then some parameters. The command names and the parameters are all separated by spaces.

The commands that you must implement are as follows. The names of the commands are in bold and the parameters are in italics.

  • axes- This will create a new Axesobject and add it to the window.
  • oval name x y width height - This will create a new Oval(x,y,width,height) and will add it to the window. It will also add the Oval and its name to your ObjTable so that it can be found again. Use setTranslate() rather than setCenter() to control the position of the oval. This will work better with scaling and rotating.
  • rect name x y width height - This will create a new Rect(x,y,width,height) and will add it to the window. It will also add the Rect and its name to your ObjTable so that it can be found again. Use setTranslate() rather than setCenter() to control the position of the oval. This will work better with scaling and rotating.
  • line name x1 y1 x2 y2 - This will create a new Line(x1,y1,x2,y2) and will add it to the window. It will also add the Line and its name to your ObjTable so that it can be found again.
  • move name x y - This will use the find() method on our ObjTable to locate the object by this name. If there is no such object, then this will write out an error. Having found the object it will use setTranslate(x,y) method to move the object to a new location.
  • scale name sx sy - This will use the find() method on our ObjTable to locate the object by this name. If there is no such object, then this will write out an error. Having found the object it will use setScale(sx, sy) method to resize the object.
  • rot name angle - This will use the find() method on our ObjTable to locate the object by this name. If there is no such object, then this will write out an error. Having found the object it will use setRot(angle) method to rotate the object .
  • fill name red green blue - This will use the find() method on our ObjTable to locate the object by this name. If there is no such object, then this will write out an error. Having found the object it will use setFillColor(red,green,blue) to change the color of the object.
  • linecolor name red green blue - This will use the find() method on our ObjTable to locate the object by this name. If there is no such object, then this will write out an error. Having found the object it will use setLineColor(red,green,blue) to change the color of the object.
  • linewidth name width - This will use the find() method to locate this object and then use the setLineWidth(width) to change its line width.
  • quit - this will stop the loop from reading new commands and will end the program.

Each of your commands should be implemented as a method that accepts a Scanner as a parameter and will read the rest of the command from the Scanner and do the command.

You should read the first token from a command using the Scanner's next() method. You can then decide which of your command methods to call and pass the scanner to that method.

Example

? axes
? oval face 0 0 10 10
? color face 1.0 0.6 0.6
? scale face 0.5 0.6 
? move face 3 3 
? rect seat 0 -5 4 2
? scale seat 0.5 0.5
? move seat 3 0 
? color seat 0 0 1
? quit

This would interactively do the same thing as the following code.

Win2D win = new Win2D("interactive");
ObjTable table=new ObjTable();
DrawObj tmp;
win.add(new Axes());
tmp=new Oval(0,0,10,10)
win.add(tmp);
table.add("face",tmp);

tmp=table.find("face");
tmp.setColor(1.0,0.6,0.6);

tmp=table.find("face");
tmp.setScale(0.5,0.6);

tmp=table.find("face");
tmp.setTranslate(3,3);

tmp=new Rect(0,-5,4,2)
win.add(tmp);
table.add("seat",tmp);

tmp=table.find("seat");
tmp.setScale(0.5,0.5);

tmp=table.find("seat"); 
tmp.setTranslate(3,0); 

tmp=table.find("seat"); 
tmp.setColor(0,0,1);
Hints
 
  • Don't forget to create your array in the constructor of ObjTable and to initialize nObjects to zero. If you create your array with at least 1000 items in it, you should never run out.
  • Your main program should set up the window and then enter a loop like
    		boolean quit=false;
    		while(!quite)
    		{ 
    			// read a command and call its method using a lot of if statements
    			// when you read a "quit" command, don't call a method, just set quit=true;
    		} 
    		
  • Test each command to be certain it works before doing the next one.
Passoff
 

__ 3) NamedObj and ObjTable are implemented correctly as their own classes

__ 1) axes works

__ 1) oval works

__ 1) rect works

__ 1) line works

__ 1) move works

__ 1) scale works

__ 1) rotate works

__ 1) quit works

__ 1) error messages appear correctly when a wrong name is used.