Java基础之在窗口中绘图——绘制曲线(CurveApplet 1)

Applet程序。

定义*曲线的类有两个,其中一个定义二次曲线,另一个定义三次曲线。这些*曲线是用一系列线段定义的参数化曲线。二次曲线段用方程定义,方程包含独立变量x的平方。三次曲线也用方程定义,方程包含独立变量x的立方。

QuadCurve2D:二次曲线的抽象基类,曲线用两个端点和一个用来定义两端切线的控制点来定义。切线是从端点到控制点的直线。

CubicCurve2D:三次曲线的抽象基类,曲线用两个端点和两个用来定义两端切线的控制点来定义。切线是从端点到对应控制点的直线。

 import javax.swing.*;
import java.awt.*;
import java.awt.geom.*; @SuppressWarnings("serial")
public class CurveApplet extends JApplet {
// Initialize the applet
@Override
public void init() {
pane = new CurvePane(); // Create pane containing curves
Container content = getContentPane(); // Get the content pane // Add the pane displaying the curves to the content pane for the applet
content.add(pane); // BorderLayout.CENTER is default position
} // Class defining a pane on which to draw
class CurvePane extends JComponent {
// Constructor
public CurvePane() {
quadCurve = new QuadCurve2D.Double( // Create quadratic curve
startQ.x, startQ.y, // Segment start point
control.x, control.y, // Control point
endQ.x, endQ.y); // Segment end point cubicCurve = new CubicCurve2D.Double( // Create cubic curve
startC.x, startC.y, // Segment start point
controlStart.x, controlStart.y, // Control pt for start
controlEnd.x, controlEnd.y, // Control point for end
endC.x, endC.y); // Segment end point
} @Override
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D)g; // Get a 2D device context // Draw the curves
g2D.setPaint(Color.BLUE);
g2D.draw(quadCurve);
g2D.draw(cubicCurve);
}
} // Points for quadratic curve
private Point2D.Double startQ = new Point2D.Double(50, 75); // Start point
private Point2D.Double endQ = new Point2D.Double(150, 75); // End point
private Point2D.Double control = new Point2D.Double(80, 25); // Control point // Points for cubic curve
private Point2D.Double startC = new Point2D.Double(50, 150); // Start point
private Point2D.Double endC = new Point2D.Double(150, 150); // End point
private Point2D.Double controlStart = new Point2D.Double(80, 100); // 1st cntrl point
private Point2D.Double controlEnd = new Point2D.Double(160, 100); // 2nd cntrl point
private QuadCurve2D.Double quadCurve; // Quadratic curve
private CubicCurve2D.Double cubicCurve; // Cubic curve
private CurvePane pane = new CurvePane(); // Pane to contain curves
}
 <html>
<head>
</head>
<body bgcolor="000000">
<center>
<applet
code = "CurveApplet.class"
width = "300"
height = "300"
>
</applet>
</center>
</body>
</html>
上一篇:android sdk manager下载慢可以使用代理信息


下一篇:Java基础之在窗口中绘图——绘制圆弧和椭圆(Sketcher 3 drawing arcs and ellipses)