Java基础之在窗口中绘图——显示曲线的控制点(CurveApplet 2 displaying control points)
2013-12-24 00:18
337 查看
Applet程序。
HTML文件与上一例同。
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); // Create and draw the markers showing the control points g2D.setPaint(Color.red); // Set the color ctrlQuad.draw(g2D); ctrlCubic1.draw(g2D); ctrlCubic2.draw(g2D); // Draw tangents from the curve end points to the control marker centers Line2D.Double tangent = new Line2D.Double(startQ, ctrlQuad.getCenter()); g2D.draw(tangent); tangent = new Line2D.Double(endQ, ctrlQuad.getCenter()); g2D.draw(tangent); tangent = new Line2D.Double(startC, ctrlCubic1.getCenter()); g2D.draw(tangent); tangent = new Line2D.Double(endC, ctrlCubic2.getCenter()); g2D.draw(tangent); } } // Inner class defining a control point marker private class Marker { public Marker(Point2D.Double control) { center = control; // Save control point as circle center // Create circle around control point circle = new Ellipse2D.Double(control.x-radius, control.y-radius, 2.0*radius, 2.0*radius); } // Draw the marker public void draw(Graphics2D g2D) { g2D.draw(circle); } // Get center of marker - the control point position Point2D.Double getCenter() { return center; } Ellipse2D.Double circle; // Circle around control point Point2D.Double center; // Circle center - the control point static final double radius = 3; // Radius of circle } // 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 // Markers for control points private Marker ctrlQuad = new Marker(control); private Marker ctrlCubic1 = new Marker(controlStart); private Marker ctrlCubic2 = new Marker(controlEnd); }
HTML文件与上一例同。
相关文章推荐
- Java基础之在窗口中绘图——移动曲线的控制点(CurveApplet 3 moving the control points)
- Java基础之在窗口中绘图——绘制曲线(CurveApplet 1)
- Java_基础—GUI(创建一个窗口并显示/布局管理器)
- Java基础之在窗口中绘图——绘制圆弧和椭圆(Sketcher 3 drawing arcs and ellipses)
- Java基础之在窗口中绘图——利用多态性使用鼠标自由绘图(Sketcher 7 with a crosshair cursor)
- Java基础之扩展GUI——显示About对话框(Sketcher 2 displaying an About dialog)
- Java基础之在窗口中绘图——使用模型/视图体系结构在视图中绘图(Sketcher 1 drawing a 3D rectangle)
- Java基础之在窗口中绘图——绘制星星(StarApplet 1)
- Java基础之在窗口中绘图——绘制直线和矩形(Sketcher 2 drawing lines and rectangles)
- Java基础之在窗口中绘图——填充星型(StarApplet 2 filled stars)
- Java基础之在窗口中绘图——渐变填充(GradientApplet 1)
- TankWar 单机(JAVA版) 版本0.1&&版本0.2 显示一个窗口
- Java基础之时间的友好显示
- Java知多少(81)框架窗口基础
- Java绘图技术基础(实例讲解)
- 用Java图形窗口显示网页内容
- java入门教程-11.1Java绘图基础
- JAVA进阶8.3——绘图基础(3)绘制文字
- Java基础——数据显示处理 DecimalFormat+BigDecimal
- JAVA学习笔记:(三)基本绘图窗口