Applet程序。
定义自由曲线的类有两个,其中一个定义二次曲线,另一个定义三次曲线。这些自由曲线是用一系列线段定义的参数化曲线。二次曲线段用方程定义,方程包含独立变量x的平方。三次曲线也用方程定义,方程包含独立变量x的立方。
QuadCurve2D:二次曲线的抽象基类,曲线用两个端点和一个用来定义两端切线的控制点来定义。切线是从端点到控制点的直线。
CubicCurve2D:三次曲线的抽象基类,曲线用两个端点和两个用来定义两端切线的控制点来定义。切线是从端点到对应控制点的直线。
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 } 43 44 // Points for quadratic curve 45 private Point2D.Double startQ = new Point2D.Double(50, 75); // Start point 46 private Point2D.Double endQ = new Point2D.Double(150, 75); // End point 47 private Point2D.Double control = new Point2D.Double(80, 25); // Control point 48 49 // Points for cubic curve 50 private Point2D.Double startC = new Point2D.Double(50, 150); // Start point 51 private Point2D.Double endC = new Point2D.Double(150, 150); // End point 52 private Point2D.Double controlStart = new Point2D.Double(80, 100); // 1st cntrl point 53 private Point2D.Double controlEnd = new Point2D.Double(160, 100); // 2nd cntrl point 54 private QuadCurve2D.Double quadCurve; // Quadratic curve 55 private CubicCurve2D.Double cubicCurve; // Cubic curve 56 private CurvePane pane = new CurvePane(); // Pane to contain curves 57 }
1 <html> 2 <head> 3 </head> 4 <body bgcolor="000000"> 5 <center> 6 <applet 7 code = "CurveApplet.class" 8 width = "300" 9 height = "300" 10 > 11 </applet> 12 </center> 13 </body> 14 </html>