// This program defines a new kind of robot that can draw filled-in // squares. The messages this robot provides to clients to do this are: // // o Parameterless and 4-parameter constructors, corresponding to // the constructors for "Robot." // // o paintLine, a message that takes an integer, n, and a color, c // as its parameters, and causes a robot to paint a line n tiles // long in color c. This message has no return value. // Preconditions: The robot is standing over the first tile of the // future line, facing in the direction the line should grow. // There are no obstructions on the n tiles in front of the robot. // Postconditions: The robot is standing on the tile after the line, // facing in its origina direction. // // o paintSquare, a message that takes an integer, n, and a color, c, // as parameters, and causes a robot to fill a square n tiles wide // by n tiles high with color c. This message has no return value. // Preconditions: The robot is standing on what will be the back left // (relative to the robot's initial heading) corner of the square. // There are no obstructions on any tile within the region extending // from 1 tile behind the robot to n tiles in front, and n tiles // to the robot's right. // // History: // // January 2005 -- Created by Doug Baldwin. import geneseo.cs.sc.*; class SquareRobot extends Robot { // The constructors. Both just pass their parameters on to the // corresponding constructor for the "Robot" class. public SquareRobot() { super(); } public SquareRobot( int col, int row, int heading, RobotRoom room ) { super( col, row, heading, room ); } // The "paintLine" method. This just paints and moves n times. public void paintLine( int n, java.awt.Color c ) { for ( int i = 0; i < n; i++ ) { this.paint( c ); this.move(); } } // The "paintSquare" method. This fills the square by painting n lines, // each n tiles long. The robot paints in a zigzag pattern, first up the // square, then down, then up, and so forth. In case n is odd, the "down" // pass can be skipped if all lines have already been drawn. public void paintSquare( int n, java.awt.Color c ) { for ( int i = n; i > 0; i = i - 2 ) { // "Up" pass: this.paintLine( n, c ); this.turnRight(); this.move(); this.turnRight(); this.move(); // "Down" pass: if ( i > 1 ) { this.paintLine( n, c ); this.turnLeft(); this.move(); this.turnLeft(); this.move(); } } } // The main method. This creates a SquareRobot in a room where it // has space to paint nice big squares, and asks it to paint one. public static void main(String args[]) { for ( int n = 6; n < 13; n = n + 2 ) { RobotRoom room = new RobotRoom( "17 17" ); SquareRobot painter = new SquareRobot( 2, 14, Robot.NORTH, room ); long start = System.currentTimeMillis(); painter.paintSquare( n, java.awt.Color.blue ); long end = System.currentTimeMillis(); long elapsed = end - start; double theory = 4 * n + 2 * n * n; System.out.println( "Square took " + elapsed + " mS to draw." ); System.out.println( "Ratio to 4n + 4n^2 = " + elapsed/theory ); } } }