clone Shape - Java 2D Graphics

Java examples for 2D Graphics:Shape

Description

clone Shape

Demo Code

/*// w w  w.  jav a  2  s  .  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.Polygon;
import java.awt.Rectangle;
import java.awt.Shape;

import java.awt.geom.*;

public class Main {
    public static Shape clone(Shape shape) {
        if (shape == null) {
            return null;
        }
        Shape clone;
        if (shape instanceof Line2D) {
            clone = (shape instanceof Line2D.Float) ? new Line2D.Float()
                    : new Line2D.Double();
            ((Line2D) clone).setLine((Line2D) shape);
        } else if (shape instanceof Rectangle) {
            clone = new Rectangle((Rectangle) shape);
        } else if (shape instanceof Rectangle2D) {
            clone = (shape instanceof Rectangle2D.Float) ? new Rectangle2D.Float()
                    : new Rectangle2D.Double();
            ((Rectangle2D) clone).setRect((Rectangle2D) shape);
        } else if (shape instanceof RoundRectangle2D) {
            clone = (shape instanceof RoundRectangle2D.Float) ? new RoundRectangle2D.Float()
                    : new RoundRectangle2D.Double();
            ((RoundRectangle2D) clone)
                    .setRoundRect((RoundRectangle2D) shape);
        } else if (shape instanceof Ellipse2D) {
            clone = (shape instanceof Ellipse2D.Float) ? new Ellipse2D.Float()
                    : new Ellipse2D.Double();
            ((Ellipse2D) clone).setFrame(((Ellipse2D) shape).getFrame());
        } else if (shape instanceof Arc2D) {
            clone = (shape instanceof Arc2D.Float) ? new Arc2D.Float()
                    : new Arc2D.Double();
            ((Arc2D) clone).setArc((Arc2D) shape);
        } else if (shape instanceof Polygon) {
            Polygon p = (Polygon) shape;
            clone = new Polygon(p.xpoints, p.ypoints, p.npoints);
        } else if (shape instanceof CubicCurve2D) {
            clone = (shape instanceof CubicCurve2D.Float) ? new CubicCurve2D.Float()
                    : new CubicCurve2D.Double();
            ((CubicCurve2D) clone).setCurve((CubicCurve2D) shape);
        } else if (shape instanceof QuadCurve2D) {
            clone = (shape instanceof QuadCurve2D.Float) ? new QuadCurve2D.Float()
                    : new QuadCurve2D.Double();
            ((QuadCurve2D) clone).setCurve((QuadCurve2D) shape);
        } else if (shape instanceof Path2D.Float) {
            clone = new Path2D.Float(shape);
        } else {
            clone = new Path2D.Double(shape);
        }
        return clone;
    }
}

Related Tutorials