Lab assignment: The grey square again.

For today's lab you must use the files listed below. Create a folder in your computer and save the files inside the folder.

Canvas.java
Square.java
Lab4.java
Board.java

When you create the Eclipse project for this lab, choose the option Create project from existing source
in the project creation wizard:

You will need to change the file Lab4.java and turn it in at the end of the lab:

Preliminary Information:
When the program starts a canvas is created and displayed on a window. The canvas dimensions are 400x400 pixels. The java
file Canvas.java takes care of creating the canvas and displaying it on the screen in the upper left corner. You do not need to worry about this.

The canvas contains a gray square at the top left corner. The square is created in the constructor of the class Board.java in the line

targetSquare = new Square( 0, 0, 40, "gray", true, "");

The first two numbers (=0) in the line specify the x-value and y-value of the upper-left corner of the square inside the canvas.
The third number (=40) specifies the size of the square. The "gray" specifies the color. The value "true" specifies the visibility,
that is that the square is visible. The last parameter "" assigns a label to the square. Since we do not need the label in this lab,
this parameter is the empty string.

Stage 1:
The square moves after input from the keyboard. There are four possible inputs from the keyboard.

1. "RIGHT" for moving the square from the left margin to the right margin.
2. "LEFT" for moving the square from the right margin to the left.
3. "UP" for moving the square from the bottom of the canvas to the top of the canvas.
4. "DOWN" for moving the square from the top of the canvas to the bottom of the canvas.

The execution of the program for the sequence of inputs "RIGHT", "DOWN", "LEFT", "UP" is shown below:



Note: The x and y coordinates start from the upper left corner of the canvas, that is the upper left corner of the canvas has coordinates (0,0). In order to move the square around you MUST use the methods slowMoveHorizontal(int distance) and slowMoveVertical(int distance) in the class Square. DO NOT use the method setPosition(x,y).

Stage 2:
On console input "AROUND", the square does all the 4 movements shown in the figure in Stage 1. The square completes a full "circle" around the borders of the canvas.

Stage 3 (Advanced):
On a different console input have the square move in clockwise "circles" as before, except on each new circle have the square move inside the canvas 40 pixels, so it essentially spirals inward on each new rotation. The first rotation is the same as the one in Stage 2.

Solution.

For a solution, slightly modified from the solutions of one of the pairs see the file Lab4.java.