draw Path - Java 2D Graphics

Java examples for 2D Graphics:Path

Description

draw Path

Demo Code


import java.awt.*;
import java.util.*;
import java.awt.geom.*;

public class Main{
    public static GeneralPath drawPath(String pathSelId,
            AffineTransform affine, Vector<Point2D.Double> gPathPoints,
            Graphics g) {/*  w w  w  .  j a  v  a 2  s.  c o m*/
        // Drawing the GPath to get the 
        // The functions in Shape Util draw the shape using the Graphics and Path Points
        if (pathSelId == RGPTActionListener.ImageActions.SHAPE_LINE_PATH_MENU
                .toString())
            return drawLine(affine, gPathPoints, g);
        else if (pathSelId == RGPTActionListener.ImageActions.SHAPE_QUAD_PATH_MENU
                .toString())
            return drawQuadCurve(affine, gPathPoints, g);
        else if (pathSelId == RGPTActionListener.ImageActions.SHAPE_CUBIC_PATH_MENU
                .toString())
            return drawBezierCurve(affine, gPathPoints, null);
        return null;
    }
    public static GeneralPath drawLine(AffineTransform affine,
            Vector<Point2D.Double> gPathPoints, Graphics g) {
        return drawLine(g, affine, Color.blue, Color.black, gPathPoints);
    }
    public static GeneralPath drawLine(Graphics g, AffineTransform affine,
            Color ptColor, Color lineColor,
            Vector<Point2D.Double> gPathPoints) {
        if (affine == null) {
            affine = new AffineTransform();
            affine.setToIdentity();
        }
        boolean drawPath = true;
        Color origColor = null;
        if (g == null)
            drawPath = false;
        else
            origColor = g.getColor();
        int ptSize = 4;
        Point2D.Double pt;
        if (gPathPoints.size() == 0)
            return null;
        // RGPTLogger.logToFile("Path Points: "+gPathPoints);
        Graphics2D g2d = (Graphics2D) g;
        GeneralPath gPath = new GeneralPath();
        if (gPathPoints.size() == 1) {
            if (drawPath)
                drawPoint(affine, g2d, gPathPoints.elementAt(0), 1,
                        Color.BLACK);
            return null;
        }
        for (int i = 0; i < gPathPoints.size(); i++) {
            pt = gPathPoints.elementAt(i);
            Point2D.Double affinePt = (Point2D.Double) affine.transform(pt,
                    null);

            if (drawPath) {
                if (ptColor != null) {
                    g.setColor(ptColor);
                    g.fillOval((int) affinePt.x - ptSize, (int) affinePt.y
                            - ptSize, 2 * ptSize, 2 * ptSize);
                }
                g2d.setColor(lineColor);
            }
            if (i == 0) {
                gPath.moveTo(pt.x, pt.y);
                continue;
            }
            gPath.lineTo(pt.x, pt.y);
        }
        gPath = (GeneralPath) gPath.createTransformedShape(affine);
        if (drawPath) {
            g2d.draw(gPath);
            g.setColor(origColor);
        }
        return gPath;
    }
    public static GeneralPath drawQuadCurve(AffineTransform affine,
            Vector<Point2D.Double> gPathPoints, Graphics g) {
        if (affine == null) {
            affine = new AffineTransform();
            affine.setToIdentity();
        }
        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 GeneralPath drawBezierCurve(AffineTransform affine,
            Vector<Point2D.Double> gPathPoints, Graphics g) {
        if (affine == null) {
            affine = new AffineTransform();
            affine.setToIdentity();
        }
        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 = null;
        AffineTransform origAffine = null;
        if (drawPath) {
            g2 = (Graphics2D) g;
            origAffine = g2.getTransform();
        }
        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.BLACK);
            return null;
        }
        for (int i = 1; i < points.length; i += 3) {
            if (i + 1 == points.length)
                gPath.lineTo(points[i].x, points[i].y);
            else if (i + 2 == points.length)
                gPath.quadTo(points[i].x, points[i].y, points[i + 1].x,
                        points[i + 1].y);
            else
                gPath.curveTo(points[i].x, points[i].y, points[i + 1].x,
                        points[i + 1].y, points[i + 2].x, points[i + 2].y);
        }
        Color ptColor;
        if (drawPath) {
            drawPoint(affine, g2, points[0], pixelSize, Color.BLUE);
            for (int i = 1; i < points.length; i += 3) {
                if (i + 1 == points.length) {
                    drawPoint(affine, g2, points[i], pixelSize, Color.CYAN);
                } else if (i + 2 == points.length) {
                    drawPoint(affine, g2, points[i], pixelSize, Color.CYAN);
                    drawPoint(affine, g2, points[i + 1], pixelSize,
                            Color.CYAN);
                } else {
                    for (int cntr = 0; cntr < 3; cntr++) {
                        int ptCntr = i + cntr;
                        if (cntr == 2)
                            g2.setColor(Color.blue);
                        else
                            g2.setColor(Color.cyan);
                        Point2D.Double affinePt = (Point2D.Double) affine
                                .transform(points[ptCntr], null);
                        g2.fill(new Ellipse2D.Double(affinePt.x - 4
                                * pixelSize, affinePt.y - 4 * pixelSize,
                                8 * pixelSize, 8 * pixelSize));
                        // System.out.println("Drawing Point at PtCntr: "+ptCntr);
                    }
                }
            } // for (int i = 1; i < points.length;  i += 3)
        }
        gPath = (GeneralPath) gPath.createTransformedShape(affine);
        if (drawPath) {
            g2.setColor(Color.DARK_GRAY);
            Stroke origStroke = g2.getStroke();
            g2.setStroke(new BasicStroke(0.5f));
            g2.draw(gPath);
            g2.setStroke(origStroke);
            g2.transform(origAffine);
        }
        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