Square.java (3366B)
1 import java.awt.*; 2 3 /** 4 * A square that can be manipulated and that draws itself on a canvas. 5 * 6 * @author Michael Kolling and David J. Barnes 7 * @version 1.0 (15 July 2000) 8 */ 9 10 public class Square 11 { 12 private int size; 13 private int xPosition; 14 private int yPosition; 15 private String color; 16 private boolean isVisible; 17 18 @Override 19 public String toString(){ 20 return "size: " + size + 21 ", xPosition: " + xPosition + 22 ", yPosition: " + yPosition + 23 ", color: " + color + 24 ", isVisible: " + isVisible; 25 } 26 27 /** 28 * Create a new square at default position with default color. 29 */ 30 public Square() 31 { 32 size = 30; 33 xPosition = 60; 34 yPosition = 50; 35 color = "red"; 36 isVisible = false; 37 } 38 39 /** 40 * Make this square visible. If it was already visible, do nothing. 41 */ 42 public void makeVisible() 43 { 44 isVisible = true; 45 draw(); 46 } 47 48 /** 49 * Make this square invisible. If it was already invisible, do nothing. 50 */ 51 public void makeInvisible() 52 { 53 erase(); 54 isVisible = false; 55 } 56 57 /** 58 * Move the square a few pixels to the right. 59 */ 60 public void moveRight() 61 { 62 moveHorizontal(20); 63 } 64 65 /** 66 * Move the square a few pixels to the left. 67 */ 68 public void moveLeft() 69 { 70 moveHorizontal(-20); 71 } 72 73 /** 74 * Move the square a few pixels up. 75 */ 76 public void moveUp() 77 { 78 moveVertical(-20); 79 } 80 81 /** 82 * Move the square a few pixels down. 83 */ 84 public void moveDown() 85 { 86 moveVertical(20); 87 } 88 89 /** 90 * Move the square horizontally by 'distance' pixels. 91 */ 92 public void moveHorizontal(int distance) 93 { 94 erase(); 95 xPosition += distance; 96 draw(); 97 } 98 99 /** 100 * Move the square vertically by 'distance' pixels. 101 */ 102 public void moveVertical(int distance) 103 { 104 erase(); 105 yPosition += distance; 106 draw(); 107 } 108 109 /** 110 * Slowly move the square horizontally by 'distance' pixels. 111 */ 112 public void slowMoveHorizontal(int distance) 113 { 114 int delta; 115 116 if(distance < 0) 117 { 118 delta = -1; 119 distance = -distance; 120 } 121 else 122 { 123 delta = 1; 124 } 125 126 for(int i = 0; i < distance; i++) 127 { 128 xPosition += delta; 129 draw(); 130 } 131 } 132 133 /** 134 * Slowly move the square vertically by 'distance' pixels. 135 */ 136 public void slowMoveVertical(int distance) 137 { 138 int delta; 139 140 if(distance < 0) 141 { 142 delta = -1; 143 distance = -distance; 144 } 145 else 146 { 147 delta = 1; 148 } 149 150 for(int i = 0; i < distance; i++) 151 { 152 yPosition += delta; 153 draw(); 154 } 155 } 156 157 /** 158 * Change the size to the new size (in pixels). Size must be >= 0. 159 */ 160 public void changeSize(int newSize) 161 { 162 erase(); 163 size = newSize; 164 draw(); 165 } 166 167 /** 168 * Change the color. Valid colors are "red", "yellow", "blue", "green", 169 * "magenta" and "black". 170 */ 171 public void changeColor(String newColor) 172 { 173 color = newColor; 174 draw(); 175 } 176 177 /* 178 * Draw the square with current specifications on screen. 179 */ 180 private void draw() 181 { 182 if(isVisible) { 183 Canvas canvas = Canvas.getCanvas(); 184 canvas.draw(this, color, 185 new Rectangle(xPosition, yPosition, size, size)); 186 canvas.wait(10); 187 } 188 } 189 190 /* 191 * Erase the square on screen. 192 */ 193 private void erase() 194 { 195 if(isVisible) { 196 Canvas canvas = Canvas.getCanvas(); 197 canvas.erase(this); 198 } 199 } 200 }