zoukankan      html  css  js  c++  java
  • Java基础之在窗口中绘图——移动曲线的控制点(CurveApplet 3 moving the control points)

    Applet程序。

      1 import javax.swing.*;
      2 import java.awt.*;
      3 import java.awt.geom.*;
      4 import javax.swing.event.MouseInputAdapter;
      5 import java.awt.event.MouseEvent;
      6 
      7 @SuppressWarnings("serial")
      8 public class CurveApplet extends JApplet {
      9   // Initialize the applet
     10   @Override
     11   public void init() {
     12     pane = new CurvePane();                                            // Create pane containing curves
     13     Container content = getContentPane();                              // Get the content pane
     14 
     15     // Add the pane displaying the curves to the content pane for the applet
     16     content.add(pane);                                                 // BorderLayout.CENTER is default position
     17   MouseHandler handler = new MouseHandler();                           // Create the listener
     18   pane.addMouseListener(handler);                                      // Monitor mouse button presses
     19   pane.addMouseMotionListener(handler);                                // as well as movement
     20 
     21   }
     22 
     23   // Class defining a pane on which to draw
     24   class CurvePane extends JComponent {
     25     // Constructor
     26     public CurvePane() {
     27       quadCurve = new QuadCurve2D.Double(                              // Create quadratic curve
     28                       startQ.x, startQ.y,                              // Segment start point
     29                       control.x, control.y,                            // Control point
     30                       endQ.x, endQ.y);                                 // Segment end point
     31 
     32       cubicCurve = new CubicCurve2D.Double(                            // Create cubic curve
     33                       startC.x, startC.y,                              // Segment start point
     34                       controlStart.x, controlStart.y,                  // Control pt for start
     35                       controlEnd.x, controlEnd.y,                      // Control point for end
     36                       endC.x, endC.y);                                 // Segment end point
     37     }
     38 
     39     @Override
     40     public void paint(Graphics g) {
     41       Graphics2D g2D = (Graphics2D)g;                                  // Get a 2D device context
     42 
     43       // Update the curves with the current control point positions
     44       quadCurve.ctrlx = ctrlQuad.getCenter().x;
     45       quadCurve.ctrly = ctrlQuad.getCenter().y;
     46       cubicCurve.ctrlx1 = ctrlCubic1.getCenter().x;
     47       cubicCurve.ctrly1 = ctrlCubic1.getCenter().y;
     48       cubicCurve.ctrlx2 = ctrlCubic2.getCenter().x;
     49       cubicCurve.ctrly2 = ctrlCubic2.getCenter().y;
     50 
     51       // Draw the curves
     52       g2D.setPaint(Color.BLUE);
     53       g2D.draw(quadCurve);
     54       g2D.draw(cubicCurve);
     55 
     56       // Create and draw the markers showing the control points
     57       g2D.setPaint(Color.red);                                         // Set the color
     58       ctrlQuad.draw(g2D);
     59       ctrlCubic1.draw(g2D);
     60       ctrlCubic2.draw(g2D);
     61       // Draw tangents from the curve end points to the control marker centers
     62       Line2D.Double tangent = new Line2D.Double(startQ, ctrlQuad.getCenter());
     63       g2D.draw(tangent);
     64       tangent = new Line2D.Double(endQ, ctrlQuad.getCenter());
     65       g2D.draw(tangent);
     66 
     67       tangent = new Line2D.Double(startC, ctrlCubic1.getCenter());
     68       g2D.draw(tangent);
     69       tangent = new Line2D.Double(endC, ctrlCubic2.getCenter());
     70       g2D.draw(tangent);
     71     }
     72   }
     73 
     74   // Inner class defining a control point marker
     75   private class Marker {
     76     public Marker(Point2D.Double control)  {
     77       center = control;                                                // Save control point as circle center
     78 
     79       // Create circle around control point
     80       circle = new Ellipse2D.Double(control.x-radius, control.y-radius,
     81                                     2.0*radius, 2.0*radius);
     82     }
     83 
     84       // Draw the marker
     85       public void draw(Graphics2D g2D) {
     86         g2D.draw(circle);
     87       }
     88 
     89      // Get center of marker - the control point position
     90       Point2D.Double getCenter() {
     91         return center;
     92     }
     93 
     94     // Test if a point x,y is inside the marker
     95     public boolean contains(double x, double y) {
     96       return circle.contains(x,y);
     97     }
     98 
     99     // Sets a new control point location
    100     public void setLocation(double x, double y) {
    101       center.x = x;                      // Update control point
    102       center.y = y;                      // coordinates
    103       circle.x = x-radius;               // Change circle position
    104       circle.y = y-radius;               // correspondingly
    105     }
    106 
    107     Ellipse2D.Double circle;                                           // Circle around control point
    108     Point2D.Double center;                                             // Circle center - the control point
    109     static final double radius = 3;                                    // Radius of circle
    110   }
    111 
    112   private class MouseHandler extends MouseInputAdapter {
    113     @Override
    114     public void mousePressed(MouseEvent e) {
    115       // Check if the cursor is inside any marker
    116       if(ctrlQuad.contains(e.getX(), e.getY()))
    117         selected = ctrlQuad;
    118       else if(ctrlCubic1.contains(e.getX(), e.getY()))
    119         selected = ctrlCubic1;
    120       else if(ctrlCubic2.contains(e.getX(), e.getY()))
    121         selected = ctrlCubic2;
    122     }
    123 
    124     @Override
    125     public void mouseReleased(MouseEvent e) {
    126       selected = null;                                                 // Deselect any selected marker
    127     }
    128 
    129     @Override
    130     public void mouseDragged(MouseEvent e) {
    131       if(selected != null) {                                           // If a marker is selected
    132         // Set the marker to current cursor position
    133         selected.setLocation(e.getX(), e.getY());
    134         pane.repaint();                                                // Redraw pane contents
    135       }
    136     }
    137 
    138     private Marker selected;                                           // Stores reference to selected marker
    139   }
    140 
    141   // Points for quadratic curve
    142   private Point2D.Double startQ = new Point2D.Double(50, 75);          // Start point
    143   private Point2D.Double endQ = new Point2D.Double(150, 75);           // End point
    144   private Point2D.Double control = new Point2D.Double(80, 25);         // Control point
    145 
    146   // Points for cubic curve
    147   private Point2D.Double startC = new Point2D.Double(50, 150);         // Start point
    148   private Point2D.Double endC = new Point2D.Double(150, 150);          // End point
    149   private Point2D.Double controlStart = new Point2D.Double(80, 100);   // 1st cntrl point
    150   private Point2D.Double controlEnd = new Point2D.Double(160, 100);    // 2nd cntrl point
    151   private QuadCurve2D.Double quadCurve;                                // Quadratic curve
    152   private CubicCurve2D.Double cubicCurve;                              // Cubic curve
    153   private CurvePane pane = new CurvePane();                            // Pane to contain curves
    154 
    155    // Markers for control points
    156    private Marker ctrlQuad = new Marker(control);
    157    private Marker ctrlCubic1 = new Marker(controlStart);
    158    private Marker ctrlCubic2 = new Marker(controlEnd);
    159 
    160 }

    HTML文件与上一例同。

  • 相关阅读:
    并行执行计划
    mongodb数据查询-小结
    mongodb的基本操作-小结
    架构
    bat、dos控制多个后台程序启动
    python技术实践清单
    Linux升级安装python2.7.5到python2.7.9
    数据分析-GDP统计
    技术能力清单-小结
    动态链接库*.so
  • 原文地址:https://www.cnblogs.com/mannixiang/p/3488386.html
Copyright © 2011-2022 走看看