draw Quad Curve - Java 2D Graphics

Java examples for 2D Graphics:Curve

Description

draw Quad Curve

Demo Code


//package com.java2s;
import java.awt.*;
import java.util.*;
import java.awt.geom.*;

public class Main {
    public static GeneralPath drawQuadCurve(AffineTransform affine,
            Vector<Point2D.Double> gPathPoints, Graphics g) {
        if (affine == null) {
            affine = new AffineTransform();
            affine.setToIdentity();/*from  w  w w . j a  va2  s  .  c o m*/
        }
        boolean drawPath = true;
        if (g == null)
            drawPath = false;
        int pixelSize = 1;
        Point2D.Double pt;
        if (gPathPoints.size() == 0)
            return null;
        // if (gPathPoints.size() % 2 == 0) return null;
        // System.out.println("Path Points Size: "+gPathPoints.size() + 
        // " Mod2: "+gPathPoints.size() % 2 );
        Point2D.Double[] points = gPathPoints
                .toArray(new Point2D.Double[0]);
        Graphics2D g2 = (Graphics2D) g;
        GeneralPath gPath = new GeneralPath();
        gPath.moveTo(points[0].x, points[0].y);
        if (points.length == 1) {
            if (drawPath)
                drawPoint(affine, g2, points[0], pixelSize, Color.CYAN);
            return null;
        }
        for (int i = 1; i < points.length; i += 2) {
            if (i + 1 == points.length)
                gPath.lineTo(points[i].x, points[i].y);
            else
                gPath.quadTo(points[i].x, points[i].y, points[i + 1].x,
                        points[i + 1].y);
        }
        for (int i = 0; i < points.length; i++) {
            if (drawPath) {
                if ((i & 1) == 1)
                    g2.setColor(Color.CYAN);
                else
                    g2.setColor(Color.BLUE);
                Point2D.Double affinePt = (Point2D.Double) affine
                        .transform(points[i], null);
                g2.fill(new Ellipse2D.Double(affinePt.x - 4 * pixelSize,
                        affinePt.y - 4 * pixelSize, 8 * pixelSize,
                        8 * pixelSize));
            }
        }
        gPath = (GeneralPath) gPath.createTransformedShape(affine);
        if (drawPath) {
            g2.setColor(Color.DARK_GRAY);
            // g2.setStroke( new BasicStroke(1) );
            g2.draw(gPath);
        }
        return gPath;
    }

    public static void drawPoint(Graphics2D g2, Point2D.Double pt,
            int ptSize, Color color) {
        AffineTransform affine = new AffineTransform();
        affine.setToIdentity();
        drawPoint(affine, g2, pt, ptSize, color);
    }

    public static void drawPoint(AffineTransform affine, Graphics2D g2,
            Point2D.Double point, int ptSize, Color color) {
        Point2D.Double pt = (Point2D.Double) affine.transform(point, null);
        Color origColor = g2.getColor();
        g2.setColor(color);
        g2.fill(new Ellipse2D.Double(pt.x - 4 * ptSize, pt.y - 4 * ptSize,
                8 * ptSize, 8 * ptSize));
        g2.setColor(origColor);
    }
}

Related Tutorials