Guest User

Untitled

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