Shape equals - Java 2D Graphics

Java examples for 2D Graphics:Shape

Description

Shape equals

Demo Code

/*/*from   w  w  w.  ja  v a2s. c o m*/
 * VectorGraphics2D: Vector export for Java(R) Graphics2D
 *
 * (C) Copyright 2010-2015 Erich Seifert <dev[at]erichseifert.de>
 *
 * This file is part of VectorGraphics2D.
 *
 * VectorGraphics2D is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VectorGraphics2D is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with VectorGraphics2D.  If not, see <http://www.gnu.org/licenses/>.
 */
//package com.java2s;

import java.awt.Shape;

import java.awt.geom.*;

public class Main {
    public static boolean equals(Shape shapeA, Shape shapeB) {
        PathIterator pathAIterator = shapeA.getPathIterator(null);
        PathIterator pathBIterator = shapeB.getPathIterator(null);

        if (pathAIterator.getWindingRule() != pathBIterator
                .getWindingRule()) {
            return false;
        }
        double[] pathASegment = new double[6];
        double[] pathBSegment = new double[6];
        while (!pathAIterator.isDone()) {
            int pathASegmentType = pathAIterator
                    .currentSegment(pathASegment);
            int pathBSegmentType = pathBIterator
                    .currentSegment(pathBSegment);
            if (pathASegmentType != pathBSegmentType) {
                return false;
            }
            for (int segmentIndex = 0; segmentIndex < pathASegment.length; segmentIndex++) {
                if (pathASegment[segmentIndex] != pathBSegment[segmentIndex]) {
                    return false;
                }
            }

            pathAIterator.next();
            pathBIterator.next();
        }
        // When the iterator of shapeA is done and shapeA equals shapeB, the iterator of shapeB must also be done
        if (!pathBIterator.isDone()) {
            return false;
        }
        return true;
    }
}

Related Tutorials