// Use the UW graphics library import uwcse.graphics.*; /** * A class to draw shapes made of squares * * @author your name here */ public class DrawingWithSquares { // Use a Pen to draw private Pen pen = new Pen(); // Do we need a constructor? /** * Draw a square of side length size and rotated by angle *

* The center of the square is the home position of the pen * * @param size * the length of the side of the square (in pixels) * @param angle * the angle of rotation of the square (counted counterclockwise) */ public void drawSquare(int size, int angle) { // Bring the pen to the home position (=center of the square) pen.home(); pen.up(); // Bring the pen on the upper right corner of the square pen.turn(45 + angle); pen.move((int) Math.sqrt(size * size / 2.)); pen.turn(135); // Start drawing pen.down(); // top side pen.move(size); // left side pen.turn(90); pen.move(size); // bottom side pen.turn(90); pen.move(size); // right side pen.turn(90); pen.move(size); } /** * Draw a set of 9 rotated squares. The first square is drawn with a 0 * degree angle, the second with a 10 degree angle, the third with a 30 * degree angle, etc... * * @param size * the length of the side of the squares (in pixels) */ public void drawSquarePattern(int size) { drawSquare(size, 0); drawSquare(size, 10); drawSquare(size, 20); drawSquare(size, 30); drawSquare(size, 40); drawSquare(size, 50); drawSquare(size, 60); drawSquare(size, 70); drawSquare(size, 80); } /** * Draw 8 sets of rotated squares, varying in size from 25 to 200 in * increments of 25 */ public void drawSquarePatterns() { drawSquarePattern(200); drawSquarePattern(175); drawSquarePattern(150); drawSquarePattern(125); drawSquarePattern(100); drawSquarePattern(75); drawSquarePattern(50); drawSquarePattern(25); } /** Clear the screen */ public void clearScreen() { pen.erase(); } /** * Entry point of the program */ public static void main(String[] args) { DrawingWithSquares dws = new DrawingWithSquares(); // type here the actions that you want to test e.g. dws.drawSquarePatterns(); } }