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

Circle.java 3.6KB

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