Advertisement
Guest User

Untitled

a guest
Feb 17th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. package dot;
  2.  
  3. public class Dot {
  4.  
  5. private double x;
  6. private double y;
  7. private double radius;
  8. private int r;
  9. private int g;
  10. private int b;
  11.  
  12. /**
  13. * Constructs a black dot at (0.0, 0.0) with radius 1.0
  14. */
  15. public Dot() {
  16. this(0.0, 0.0, 0.0, 1, 0, 0);
  17. }
  18.  
  19. /**
  20. * Constructs a black dot with radius 1.0 at the given location
  21. */
  22. public Dot(double x, double y) {
  23. this(x, y, 1.0, 0, 0, 0);
  24. }
  25.  
  26. /**
  27. * Constructs a black dot at the given location and radius
  28. */
  29. public Dot(double x, double y, double radius) {
  30. this(x, y, radius, 1, 0, 0);
  31. }
  32.  
  33. /**
  34. * Constructs a dot at the given location and radius, and a color specified by
  35. * r, g, b values.
  36. */
  37. public Dot(double x, double y, double radius, int r, int g, int b) {
  38. if (r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255 || radius <= 0) {
  39. throw new IllegalArgumentException();
  40. }
  41. this.x = x;
  42. this.y = y;
  43. this.radius = radius;
  44. this.r = r;
  45. this.g = g;
  46. this.b = b;
  47. }
  48.  
  49. /**
  50. * Returns the x-coordinate
  51. */
  52. public double getX() {
  53. return this.x;
  54. }
  55.  
  56. /**
  57. * Sets the x-coordinate
  58. */
  59. public void setX(double x) {
  60. this.x = x;
  61. }
  62.  
  63. /**
  64. * Doubles the radius of this dot.
  65. */
  66. public void grow() {
  67. this.radius *= 2;
  68. }
  69.  
  70. /**
  71. * Translates this dot by increasing the x- and y-coordinates by dx and dy,
  72. * respectively.
  73. */
  74. public void translate​(double dx, double dy) {
  75. this.x += dx;
  76. this.y += dy;
  77.  
  78. }
  79.  
  80. /**
  81. * Halves the radius of this dot.
  82. */
  83. public void shrink() {
  84. this.radius /= 2;
  85. }
  86.  
  87. /**
  88. * Gets the y-coordinate
  89. */
  90. public double getY() {
  91. return this.y;
  92. }
  93.  
  94. /**
  95. * Sets the y-coordinate
  96. */
  97. public void setY(double y) {
  98. this.y = y;
  99. }
  100.  
  101. /**
  102. * Gets the radius
  103. */
  104. public double getRadius() {
  105. return this.radius;
  106. }
  107.  
  108. /**
  109. * Sets the radius to a specified value
  110. */
  111. public void setRadius(double newRadius) {
  112. this.radius = newRadius;
  113. }
  114.  
  115. /**
  116. * Gets the red component of this dot's color
  117. */
  118. public int getRed() {
  119. return this.r;
  120. }
  121.  
  122. /**
  123. * Gets the green component of this dot's color
  124. */
  125. public int getGreen() {
  126. return this.g;
  127. }
  128.  
  129. /**
  130. * Returns a new dot that is halfway between this dot and the specified dot.
  131. */
  132. public Dot getMidDot​(Dot that) {
  133.  
  134. return new Dot((this.x + that.x) / 2, (this.y + that.y) / 2, (this.radius + that.radius) / 2,
  135. (this.r + that.r) / 2, (this.g + that.g) / 2, (this.b + that.b) / 2);
  136. }
  137.  
  138. /**
  139. * Gets the blue component of this dot's color
  140. */
  141. public int getBlue() {
  142. return this.b;
  143. }
  144.  
  145. /**
  146. * Returns the brightness of a dot, defined by the average of the red, green,
  147. * and blue components of the dot
  148. */
  149. public int getBrightness() {
  150. return (this.b + this.g + this.r) / 3;
  151. }
  152.  
  153. /**
  154. * Returns the distance between this dot's center and a point specified by (x,
  155. * y)
  156. */
  157. public double getDistanceTo​(double x, double y) {
  158. return Math.sqrt(Math.pow((this.x - x), 2) + Math.pow((this.y - y), 2));
  159. }
  160.  
  161. /**
  162. * Returns the distance between two dots' centers
  163. */
  164. public double getDistanceTo​(Dot that) {
  165. return Math.sqrt(Math.pow((this.x - that.x), 2) + Math.pow((this.y - that.y), 2));
  166. }
  167.  
  168. /**
  169. * Returns the distance between this dot's center and the point (0, 0)
  170. */
  171. public double getDistanceToOrigin() {
  172. return Math.sqrt(Math.pow((this.x - 0), 2) + Math.pow((this.y - 0), 2));
  173. }
  174.  
  175. /**
  176. * Sets the color of this point.
  177. */
  178. public void setColor​(int r, int g, int b) {
  179. if (r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255) {
  180. throw new IllegalArgumentException();
  181. }
  182.  
  183. this.r = r;
  184. this.g = g;
  185. this.b = b;
  186. }
  187.  
  188. /**
  189. * Brightens this dot's color by doubling its red, green, and blue components.
  190. */
  191. public void brighten() {
  192. this.r *= 2;
  193. this.g *= 2;
  194. this.b *= 2;
  195. }
  196.  
  197. /**
  198. * Darken this dot's color by cutting its red, green, and blue components in
  199. * half.
  200. */
  201. public void darken() {
  202. this.r /= 2;
  203. this.g /= 2;
  204. this.b /= 2;
  205. }
  206.  
  207. /*
  208. *
  209. * Returns a String representation of this Dot in the form: (47.375, 18.500),
  210. * radius=72.139, color=(255, 0, 0) Note that all double values are rounded to
  211. * three places.
  212. */
  213. @Override
  214. public String toString() {
  215. return "(" + x + ", " + y + "), radius=" + radius + ", color=(" + r + "," + g + "," + b + ")";
  216. }
  217.  
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement