Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Graphics2D;
  3. import java.awt.geom.Point2D;
  4. import java.awt.geom.Ellipse2D;
  5.  
  6. /**
  7. This class implements a circle and a boolean function to
  8. test if a user-given point is inside this circle.
  9. */
  10. public class Circle
  11. {
  12. /**
  13. Constructs a circle.
  14. @param x the x-coordinate of the center
  15. @param y the y-coordinate of the center
  16. @param r the radius
  17. @param aColor the color
  18. */
  19. public Circle(double x, double y, double r, Color aColor)
  20. {
  21. xCenter = x;
  22. yCenter = y;
  23. radius = r;
  24. color = aColor;
  25. }
  26.  
  27. /**
  28. Draws a circle and a point.
  29. @param g2 the graphics content
  30. */
  31. public void draw(Graphics2D g2)
  32. {
  33. g2.setColor(color);
  34. Ellipse2D.Double circle
  35. = new Ellipse2D.Double(xCenter - radius, yCenter - radius,
  36. 2 * radius, 2 * radius);
  37. g2.draw(circle);
  38. }
  39.  
  40. /**
  41. Determine if point is inside or outside the circle
  42. @param p the point to test if it is inside the circle
  43. @return true if the point is inside the circle
  44. */
  45. public boolean isInside(Point2D.Double p)
  46. {
  47. . . . <--- CODE needed
  48. }
  49.  
  50. private double xCenter;
  51. private double yCenter;
  52. private double radius;
  53. private Color color;
  54. }
  55.  
  56. import java.awt.Graphics2D;
  57. import java.awt.geom.Point2D;
  58. import java.awt.geom.Ellipse2D;
  59. import javax.swing.JOptionPane;
  60.  
  61. /**
  62. Draws a circle and a point. The point is colored green if it falls
  63. inside the circle, red otherwise.
  64. */
  65. public class CircleComponent extends JComponent
  66. {
  67. public CircleComponent(Point2D.Double point)
  68. {
  69. circle = new Circle(200, 200, 100, Color.BLACK);
  70. final double SMALL_RADIUS = 3;
  71. Color color;
  72. if(. . .)
  73. . . .
  74. else
  75. . . .
  76. smallCircle = new Circle(point.getX(), point.getY(), SMALL_RADIUS, color);
  77. }
  78.  
  79. public void paintComponent(Graphics g)
  80. {
  81. Graphics2D g2 = (Graphics2D) g;
  82. circle.draw(g2);
  83. smallCircle.draw(g2);
  84. }
  85.  
  86. private Circle circle;
  87. private Circle smallCircle;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement