Get control point for QuadCurve2D in Java

Description

The following code shows how to get control point for QuadCurve2D.

Example


// w  w w  .j  av a  2s .c o m

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 Main extends JFrame {
  DrawingCanvas canvas = new DrawingCanvas();

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

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

  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));

    }
  }
}

The code above generates the following result.

Get control point for QuadCurve2D in Java




















Home »
  Java Tutorial »
    Graphics »




Animation
BufferedImage
Color
Font
Gradient
Graphics Settings
Image
Mouse Draw
Print
Shape
Text
Transform