Returns a Line2D indexed by pos. - Java 2D Graphics

Java examples for 2D Graphics:Line

Description

Returns a Line2D indexed by pos.

Demo Code


//package com.java2s;
import java.awt.geom.PathIterator;
import java.awt.geom.GeneralPath;

import java.awt.geom.Line2D;

public class Main {
    /** Returns a Line2D indexed by pos. 1st line section is pos =0 */
    public static Line2D getLineSect(int pos, GeneralPath path) {
        double x1 = 0, y1 = 0, x2 = 0, y2 = 0;
        int index = 0;
        double seg[] = new double[6];

        for (PathIterator i = path.getPathIterator(null); !i.isDone(); i
                .next()) {/* w ww  .ja v  a  2 s.  com*/
            int segType = i.currentSegment(seg);

            if (index == 0) {
                x1 = seg[0];
                y1 = seg[1];
            } else if (index == 1) {
                x2 = seg[0];
                y2 = seg[1];
            } else if (index > 1) {
                x1 = x2;
                y1 = y2;
                x2 = seg[0];
                y2 = seg[1];
            }

            if (index > 0) {
                if (pos + 1 == index)
                    return (new Line2D.Double(x1, y1, x2, y2));
            }
            index++;
        }
        System.out.println("Error: (pos out of  bounds) 0<=pos<" + index);
        return null;
    }
}

Related Tutorials