Guest User

Untitled

a guest
Apr 22nd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. public class Pen
  2. {
  3. // constants for randomSquiggle method
  4. private static final int SQIGGLE_SIZE = 40;
  5. private static final int SQIGGLE_COUNT = 30;
  6.  
  7. private int xPosition;
  8. private int yPosition;
  9. private int rotation;
  10. private Color color;
  11. private boolean penDown;
  12.  
  13. private Canvas canvas;
  14. private Random random;
  15.  
  16. /**
  17. * Create a new Pen with its own canvas. The pen will create a new canvas for
  18. * itself to draw on, and start in the default state (centre of canvas, direction
  19. * right, color black, pen down).
  20. */
  21. public Pen()
  22. {
  23. this (280, 220, new Canvas("My Canvas", 560, 440));
  24. }
  25.  
  26. /**
  27. * Create a new Pen for a given canvas. The direction is initially 0 (to the right),
  28. * the color is black, and the pen is down.
  29. *
  30. * @param xPos the initial horizontal coordinate of the pen
  31. * @param yPos the initial vertical coordinate of the pen
  32. * @param drawingCanvas the canvas to draw on
  33. */
  34. public Pen(int xPos, int yPos, Canvas drawingCanvas)
  35. {
  36. xPosition = xPos;
  37. yPosition = yPos;
  38. rotation = 0;
  39. penDown = true;
  40. color = Color.BLACK;
  41. canvas = drawingCanvas;
  42. random = new Random();
  43. }
  44.  
  45. /**
  46. * Move the specified distance in the current direction. If the pen is down,
  47. * leave a line on the canvas.
  48. *
  49. * @param distance The distance to move forward from the current location.
  50. */
  51. public void move(int distance)
  52. {
  53. double angle = Math.toRadians(rotation);
  54. int newX = (int) Math.round(xPosition + Math.cos(angle) * distance);
  55. int newY = (int) Math.round(yPosition + Math.sin(angle) * distance);
  56.  
  57. moveTo(newX, newY);
  58. }
  59.  
  60. /**
  61. * Move to the specified location. If the pen is down, leave a line on the canvas.
  62. *
  63. * @param x The x-coordinate to move to.
  64. * @param y The y-coordinate to move to.
  65. */
  66. public void moveTo(int x, int y)
  67. {
  68. if (penDown) {
  69. canvas.setForegroundColor(color);
  70. canvas.drawLine(xPosition, yPosition, x, y);
  71. }
  72.  
  73. xPosition = x;
  74. yPosition = y;
  75. }
  76.  
  77. /**
  78. * Turn the specified amount (out of a 360 degree circle) clockwise from the current
  79. * rotation.
  80. *
  81. * @param degrees The amount of degrees to turn. (360 is a full circle.)
  82. */
  83. public void turn(int degrees)
  84. {
  85. rotation = rotation + degrees;
  86. }
  87.  
  88. /**
  89. * Turn to the specified direction. 0 is right, 90 is down, 180 is left, 270 is up.
  90. *
  91. * @param angle The angle to turn to.
  92. */
  93. public void turnTo(int angle)
  94. {
  95. rotation = angle;
  96. }
  97.  
  98. /**
  99. * Set the drawing color.
  100. *
  101. * @param newColor The color to use for subsequent drawing operations.
  102. */
  103. public void setColor(Color newColor)
  104. {
  105. color = newColor;
  106. }
  107.  
  108. /**
  109. * Lift the pen up. Moving afterwards will not leave a line on the canvas.
  110. */
  111. public void penUp()
  112. {
  113. penDown = false;
  114. }
  115.  
  116. /**
  117. * Put the pen down. Moving afterwards will leave a line on the canvas.
  118. */
  119. public void penDown()
  120. {
  121. penDown = true;
  122. }
  123.  
  124. /**
  125. * Clear the canvas.
  126. */
  127. public void clearCanvas()
  128. {
  129. canvas.erase();
  130. }
  131.  
  132. /**
  133. * Scribble on the canvas in the current color. The size and complexity of the
  134. * squiggle produced is defined by the constants SQIGGLE_SIZE and SQIGGLE_COUNT.
  135. */
  136. public void randomSquiggle()
  137. {
  138. for (int i=0; i<SQIGGLE_COUNT; i++) {
  139. move(random.nextInt(SQIGGLE_SIZE));
  140. turn(160 + random.nextInt(40));
  141. }
  142.  
  143. }
  144.  
  145. /**
  146. * Return the rotation angle of the pen.
  147. * @return the angle of the pen.
  148. */
  149. public int getRotation()
  150. {
  151. return rotation;
  152. }
  153.  
  154. /**
  155. * Return whether the pen is down or not.
  156. * @return true if the pen is down, false otherwise.
  157. */
  158. public boolean isPenDown()
  159. {
  160. return penDown;
  161. }
  162.  
  163. /**
  164. * Return the x-position of the pen.
  165. * @return The x-position.
  166. */
  167. public int getXPosition()
  168. {
  169. return xPosition;
  170. }
  171.  
  172. /**
  173. * Return the y-position of the pen.
  174. * @return The y-position.
  175. */
  176. public int getYPosition()
  177. {
  178. return yPosition;
  179. }
  180.  
  181. /**
  182. * Return the color of the pen.
  183. * @return The pen's color.
  184. */
  185. public Color getColor()
  186. {
  187. return color;
  188. }
  189. }
Add Comment
Please, Sign In to add comment