Guest User

Untitled

a guest
Sep 16th, 2018
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.geom.*;
  3. /**
  4. * Create Circle
  5. *
  6. * @author (Hendra Ramadani 05111740000055)
  7. *
  8. * @version (0.0001)
  9. */
  10. public class Circle
  11. {
  12. private int diameter;
  13. private int xPosition;
  14. private int yPosition;
  15. private String color;
  16. private boolean isVisible;
  17. /**
  18. * Create a new circle at default position with default color.
  19. */
  20. public Circle()
  21. {
  22. diameter = 30;
  23. xPosition = 20;
  24. yPosition = 60;
  25. color = "blue";
  26. isVisible = false;
  27. }
  28. /**
  29. * Make this circle visible. If it was already visible, do nothing.
  30. */
  31. public void makeVisible()
  32. {
  33. isVisible = true;
  34. draw();
  35. }
  36. /**
  37. * Make this circle invisible. If it was already invisible, do nothing.
  38. */
  39. public void makeInvisible()
  40. {
  41. erase();
  42. isVisible = false;
  43. }
  44. /**
  45. * Move the circle a few pixels to the right.
  46. */
  47. public void moveRight()
  48. {
  49. moveHorizontal(20);
  50. }
  51. /**
  52. * Move the circle a few pixels to the left.
  53. */
  54. public void moveLeft()
  55. {
  56. moveHorizontal(-20);
  57. }
  58. /**
  59. * Move the circle a few pixels up.
  60. */
  61. public void moveUp()
  62. {
  63. moveVertical(-20);
  64. }
  65. /**
  66. * Move the circle a few pixels down.
  67. */
  68. public void moveDown()
  69. {
  70. moveVertical(20);
  71. }
  72. /**
  73. * Move the circle horizontally by 'distance' pixels.
  74. */
  75. public void moveHorizontal(int distance)
  76. {
  77. erase();
  78. xPosition += distance;
  79. draw();
  80. }
  81. /**
  82. * Move the circle 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 circle 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 circle 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 newDiameter)
  136. {
  137. erase();
  138. diameter = newDiameter;
  139. draw();
  140. }
  141. /**
  142. * Change the color. Valid colors are "red", "yellow", "blue", "green",
  143. * "magenta" and "black".
  144. */
  145. public void changeColor(String newColor)
  146. {
  147. color = newColor;
  148. draw();
  149. }
  150. /*
  151. * Draw the circle with current specifications on screen.
  152. */
  153. private void draw()
  154. {
  155. if(isVisible) {
  156. Canvas canvas = Canvas.getCanvas();
  157. canvas.draw(this, color, new Ellipse2D.Double(xPosition, yPosition, 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. }
Advertisement
Add Comment
Please, Sign In to add comment