Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Graphics2D;
- import java.awt.geom.Point2D;
- import java.awt.geom.Ellipse2D;
- /**
- This class implements a circle and a boolean function to
- test if a user-given point is inside this circle.
- */
- public class Circle
- {
- /**
- Constructs a circle.
- @param x the x-coordinate of the center
- @param y the y-coordinate of the center
- @param r the radius
- @param aColor the color
- */
- public Circle(double x, double y, double r, Color aColor)
- {
- xCenter = x;
- yCenter = y;
- radius = r;
- color = aColor;
- }
- /**
- Draws a circle and a point.
- @param g2 the graphics content
- */
- public void draw(Graphics2D g2)
- {
- g2.setColor(color);
- Ellipse2D.Double circle
- = new Ellipse2D.Double(xCenter - radius, yCenter - radius,
- 2 * radius, 2 * radius);
- g2.draw(circle);
- }
- /**
- Determine if point is inside or outside the circle
- @param p the point to test if it is inside the circle
- @return true if the point is inside the circle
- */
- public boolean isInside(Point2D.Double p)
- {
- . . . <--- CODE needed
- }
- private double xCenter;
- private double yCenter;
- private double radius;
- private Color color;
- }
- import java.awt.Graphics2D;
- import java.awt.geom.Point2D;
- import java.awt.geom.Ellipse2D;
- import javax.swing.JOptionPane;
- /**
- Draws a circle and a point. The point is colored green if it falls
- inside the circle, red otherwise.
- */
- public class CircleComponent extends JComponent
- {
- public CircleComponent(Point2D.Double point)
- {
- circle = new Circle(200, 200, 100, Color.BLACK);
- final double SMALL_RADIUS = 3;
- Color color;
- if(. . .)
- . . .
- else
- . . .
- smallCircle = new Circle(point.getX(), point.getY(), SMALL_RADIUS, color);
- }
- public void paintComponent(Graphics g)
- {
- Graphics2D g2 = (Graphics2D) g;
- circle.draw(g2);
- smallCircle.draw(g2);
- }
- private Circle circle;
- private Circle smallCircle;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement