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

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