1 |
import java.applet.Applet; |
2 |
import java.awt.Graphics; |
3 |
import java.awt.Graphics2D; |
4 |
import java.awt.geom.Ellipse2D; |
5 |
import java.awt.geom.Line2D; |
6 |
import javax.swing.JOptionPane; |
7 |
|
8 |
/** |
9 |
An applet that computes and draws the intersection points |
10 |
of a circle and a line. |
11 |
*/ |
12 |
public class IntersectionApplet extends Applet |
13 |
{ |
14 |
public IntersectionApplet() |
15 |
{ |
16 |
String input |
17 |
= JOptionPane.showInputDialog("x:"); |
18 |
x = Integer.parseInt(input); |
19 |
} |
20 |
|
21 |
public void paint(Graphics g) |
22 |
{ |
23 |
Graphics2D g2 = (Graphics2D)g; |
24 |
|
25 |
double r = 100; // the radius of the circle |
26 |
|
27 |
// draw the circle |
28 |
|
29 |
Ellipse2D.Double circle |
30 |
= new Ellipse2D.Double(0, 0, 2 * RADIUS, 2 * RADIUS); |
31 |
g2.draw(circle); |
32 |
|
33 |
// draw the vertical line |
34 |
|
35 |
Line2D.Double line |
36 |
= new Line2D.Double(x, 0, x, 2 * RADIUS); |
37 |
g2.draw(line); |
38 |
|
39 |
// compute the intersection points |
40 |
|
41 |
double a = RADIUS; |
42 |
double b = RADIUS; |
43 |
|
44 |
double root = Math.sqrt(RADIUS * RADIUS - (x - a) * (x - a)); |
45 |
double y1 = b + root; |
46 |
double y2 = b - root; |
47 |
|
48 |
// draw the intersection points |
49 |
|
50 |
LabeledPoint p1 = new LabeledPoint(x, y1); |
51 |
LabeledPoint p2 = new LabeledPoint(x, y2); |
52 |
|
53 |
p1.draw(g2); |
54 |
p2.draw(g2); |
55 |
} |
56 |
|
57 |
private static final double RADIUS = 100; |
58 |
private double x; |
59 |
} |