01: import java.awt.Graphics;
02: import java.awt.Graphics2D;
03: import java.awt.geom.Ellipse2D;
04: import java.awt.geom.Line2D;
05: import javax.swing.JComponent;
06: 
07: /**
08:    A component that computes and draws the intersection points
09:    of a circle and a line.
10: */
11: public class IntersectionComponent extends JComponent
12: {  
13:    /**
14:       Constructs the component from a given x-value for the line 
15:       @param anX  the x-value for the line (between 0 and 200)
16:    */
17:    public IntersectionComponent(double anX)
18:    {  
19:       x = anX;
20:    }
21:    
22:    public void paintComponent(Graphics g)
23:    {  
24:       Graphics2D g2 = (Graphics2D) g;
25: 
26:       // Draw the circle
27: 
28:       final double RADIUS = 100;
29: 
30:       Ellipse2D.Double circle 
31:             = new Ellipse2D.Double(0, 0, 2 * RADIUS, 2 * RADIUS);
32:       g2.draw(circle);
33:       
34:       // Draw the vertical line
35: 
36:       Line2D.Double line
37:             = new Line2D.Double(x, 0, x, 2 * RADIUS);
38:       g2.draw(line);
39:       
40:       // Compute the intersection points
41:       
42:       double a = RADIUS;
43:       double b = RADIUS;
44: 
45:       double root = Math.sqrt(RADIUS * RADIUS - (x - a) * (x - a));
46:       double y1 = b + root;
47:       double y2 = b - root;
48:       
49:       // Draw the intersection points
50: 
51:       LabeledPoint p1 = new LabeledPoint(x, y1);
52:       LabeledPoint p2 = new LabeledPoint(x, y2);
53:       
54:       p1.draw(g2);
55:       p2.draw(g2);
56:    }
57: 
58:    private double x;
59: }