Applet程序。
1 import javax.swing.*; 2 import java.awt.*; 3 import java.awt.geom.*; 4 5 @SuppressWarnings("serial") 6 public class CurveApplet extends JApplet { 7 // Initialize the applet 8 @Override 9 public void init() { 10 pane = new CurvePane(); // Create pane containing curves 11 Container content = getContentPane(); // Get the content pane 12 13 // Add the pane displaying the curves to the content pane for the applet 14 content.add(pane); // BorderLayout.CENTER is default position 15 } 16 17 // Class defining a pane on which to draw 18 class CurvePane extends JComponent { 19 // Constructor 20 public CurvePane() { 21 quadCurve = new QuadCurve2D.Double( // Create quadratic curve 22 startQ.x, startQ.y, // Segment start point 23 control.x, control.y, // Control point 24 endQ.x, endQ.y); // Segment end point 25 26 cubicCurve = new CubicCurve2D.Double( // Create cubic curve 27 startC.x, startC.y, // Segment start point 28 controlStart.x, controlStart.y, // Control pt for start 29 controlEnd.x, controlEnd.y, // Control point for end 30 endC.x, endC.y); // Segment end point 31 } 32 33 @Override 34 public void paint(Graphics g) { 35 Graphics2D g2D = (Graphics2D)g; // Get a 2D device context 36 37 // Draw the curves 38 g2D.setPaint(Color.BLUE); 39 g2D.draw(quadCurve); 40 g2D.draw(cubicCurve); 41 42 // Create and draw the markers showing the control points 43 g2D.setPaint(Color.red); // Set the color 44 ctrlQuad.draw(g2D); 45 ctrlCubic1.draw(g2D); 46 ctrlCubic2.draw(g2D); 47 // Draw tangents from the curve end points to the control marker centers 48 Line2D.Double tangent = new Line2D.Double(startQ, ctrlQuad.getCenter()); 49 g2D.draw(tangent); 50 tangent = new Line2D.Double(endQ, ctrlQuad.getCenter()); 51 g2D.draw(tangent); 52 53 tangent = new Line2D.Double(startC, ctrlCubic1.getCenter()); 54 g2D.draw(tangent); 55 tangent = new Line2D.Double(endC, ctrlCubic2.getCenter()); 56 g2D.draw(tangent); 57 } 58 } 59 60 // Inner class defining a control point marker 61 private class Marker { 62 public Marker(Point2D.Double control) { 63 center = control; // Save control point as circle center 64 65 // Create circle around control point 66 circle = new Ellipse2D.Double(control.x-radius, control.y-radius, 67 2.0*radius, 2.0*radius); 68 } 69 70 // Draw the marker 71 public void draw(Graphics2D g2D) { 72 g2D.draw(circle); 73 } 74 75 // Get center of marker - the control point position 76 Point2D.Double getCenter() { 77 return center; 78 } 79 80 Ellipse2D.Double circle; // Circle around control point 81 Point2D.Double center; // Circle center - the control point 82 static final double radius = 3; // Radius of circle 83 } 84 85 // Points for quadratic curve 86 private Point2D.Double startQ = new Point2D.Double(50, 75); // Start point 87 private Point2D.Double endQ = new Point2D.Double(150, 75); // End point 88 private Point2D.Double control = new Point2D.Double(80, 25); // Control point 89 90 // Points for cubic curve 91 private Point2D.Double startC = new Point2D.Double(50, 150); // Start point 92 private Point2D.Double endC = new Point2D.Double(150, 150); // End point 93 private Point2D.Double controlStart = new Point2D.Double(80, 100); // 1st cntrl point 94 private Point2D.Double controlEnd = new Point2D.Double(160, 100); // 2nd cntrl point 95 private QuadCurve2D.Double quadCurve; // Quadratic curve 96 private CubicCurve2D.Double cubicCurve; // Cubic curve 97 private CurvePane pane = new CurvePane(); // Pane to contain curves 98 99 // Markers for control points 100 private Marker ctrlQuad = new Marker(control); 101 private Marker ctrlCubic1 = new Marker(controlStart); 102 private Marker ctrlCubic2 = new Marker(controlEnd); 103 104 }
HTML文件与上一例同。