Select a Curve To Display Its Points and Flatness Index : Curve « 2D Graphics « Java Tutorial






Select a Curve To Display Its Points and Flatness Index
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.QuadCurve2D;
import java.awt.geom.Rectangle2D;
import java.util.Vector;

import javax.swing.JFrame;

public class QuadCurveControlPoint extends JFrame {
  DrawingCanvas canvas = new DrawingCanvas();

  public QuadCurveControlPoint() {
    getContentPane().add(canvas);
    setSize(500, 500);
    setVisible(true);
  }

  public static void main(String arg[]) {
    new QuadCurveControlPoint();
  }

  class DrawingCanvas extends Canvas {
    Vector quadCurves = new Vector();

    QuadCurve2D selectedCurve = null;

    Rectangle2D boundingRec = null;

    public DrawingCanvas() {
      setSize(400, 200);
      quadCurves.addElement(new QuadCurve2D.Float(20, 20, 80, 160, 120, 20));
      quadCurves.addElement(new QuadCurve2D.Float(120, 100, 160, 40, 200, 180));
      for (int i = 0; i < quadCurves.size(); i++) {
        QuadCurve2D curve = (QuadCurve2D) quadCurves.elementAt(i);

        selectedCurve = curve;
        boundingRec = curve.getBounds2D();

        System.out.println(selectedCurve.getX1());
        System.out.println(selectedCurve.getY1());
        System.out.println(selectedCurve.getCtrlX());
        System.out.println(selectedCurve.getCtrlY());
        System.out.println(selectedCurve.getX2());
        System.out.println(selectedCurve.getY2());

        double flatness = selectedCurve.getFlatness();
        System.out.println(flatness);

      }

    }

    public void paint(Graphics g) {
      Graphics2D g2D = (Graphics2D) g;

      for (int i = 0; i < quadCurves.size(); i++) {
        g2D.draw((QuadCurve2D) quadCurves.elementAt(i));
      }
      if (boundingRec != null) {
        drawHighlightSquares(g2D, boundingRec);
      }
    }

    public void drawHighlightSquares(Graphics2D g2D, Rectangle2D r) {
      double x = r.getX();
      double y = r.getY();
      double w = r.getWidth();
      double h = r.getHeight();
      g2D.setColor(Color.black);

      g2D.fill(new Rectangle.Double(x - 3.0, y - 3.0, 6.0, 6.0));
      g2D.fill(new Rectangle.Double(x + w * 0.5 - 3.0, y - 3.0, 6.0, 6.0));

    }
  }
}








16.15.Curve
16.15.1.QuadCurve2D.FloatQuadCurve2D.Float
16.15.2.CubicCurve2D.Float
16.15.3.Select a Curve To Display Its Points and Flatness IndexSelect a Curve To Display Its Points and Flatness Index