The very first BlueJ lab. Draw the house of your dreams.

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