Package simtools.shapes

Source Code of simtools.shapes.BasicShape

/* ==============================================
* Simtools : The tools library used in JSynoptic
* ==============================================
*
* Project Info:  http://jsynoptic.sourceforge.net/index.html
*
* This library 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 2.1 of the License, or (at your option) any later version.
*
* This library 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 this
* library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* (C) Copyright 1999-2003, by :
*     Corporate:
*         Astrium SAS
*         EADS CRC
*     Individual:
*         Claude Cazenave
*     Nicolas Brodu
*
*
* $Id: BasicShape.java,v 1.3 2007/09/04 14:57:35 ogor Exp $
*
* Changes
* -------
* 25-Sep-2003 : Initial public release (NB);
*
*/
package simtools.shapes;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.AffineTransform;
import java.awt.geom.Arc2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.PathIterator;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RectangularShape;
import java.awt.geom.RoundRectangle2D;

/**
* A class to draw basic shapes such as rectangle, ellipse, arc and round
* rectangle The drawing includes the definition of the color for both the
* bounds and the filling
*
* @author Claude Cazenave
*
* @version 1.0 2001
*/
public class BasicShape extends AbstractShape implements java.io.Serializable, Cloneable, simtools.diagram.Resizable {
    static final long serialVersionUID = -1727924798190005009L;

    public static final int RECTANGLE = 1;

    public static final int ELLIPSE = 2;

    public static final int ARC = 3;

    public static final int ROUND_RECTANGLE = 4;

    protected RectangularShape _shape;

    protected Color _bColor;

    protected Color _fColor;

    /**
     * Creates a new shape
     */
    public BasicShape(int type, int ox, int oy, int width, int height, double param1, double param2) {
        super(ox, oy);
        _w = width;
        _h = height;
        _x = -_w / 2;
        _y = _h / 2;
        switch (type) {
        case RECTANGLE:
            _shape = new Rectangle2D.Double(_ox + _x, _oy + _y - _h, _w, _h);
            break;
        case ELLIPSE:
            _shape = new Ellipse2D.Double(_ox + _x, _oy + _y - _h, _w, _h);
            break;
        case ARC:
            Arc2D a = new Arc2D.Double();
            a.setAngleStart(param1);
            a.setAngleExtent(param2);
            a.setFrame(_ox + _x, _oy + _y - _h, _w, _h);
            _shape = a;
            break;
        case ROUND_RECTANGLE:
            _shape = new RoundRectangle2D.Double(_ox + _x, _oy + _y - _h, _w, _h, param1, param2);
            break;
        default:
            throw new IllegalArgumentException();
        }
        _bColor = null;
        _fColor = null;
    }

    /**
     * Performs a copy of the shape
     *
     * @return a copy of the shape
     */
    protected AbstractShape cloneShape() {
        BasicShape o = null;
        try {
            o = (BasicShape) super.clone();
        } catch (CloneNotSupportedException cnse) {
            return null;
        }
        // deep copies
        o._shape = (RectangularShape) o._shape.clone();
        return o;
    }

    /**
     * Set border color
     *
     * @param the
     *            color
     */
    public void setBorderColor(Color c) {
        _bColor = c;
    }

    /**
     * Get border color
     *
     * @return the color
     */
    public Color setBorderColor() {
        return _bColor;
    }

    /**
     * Set filler color
     *
     * @param the
     *            color
     */
    public void setFillerColor(Color c) {
        _fColor = c;
    }

    /**
     * Get filler color
     *
     * @return the color
     */
    public Color setFillerColor() {
        return _fColor;
    }

    /**
     * Resizes the shape
     */
    public void resize(int dx, int dy) {
        _w += dx;
        _h += dy;
        if (_w < 3) {
            _w = 3;
        }
        if (_h < 3) {
            _h = 3;
        }
        _shape.setFrame(_ox + _x, _oy + _y - _h, _w, _h);
    }

    /**
     * Sets shape anchor
     *
     * @param p
     *            the origin
     */
    public void setAnchor(Point p) {
        int dx = p.x - _ox;
        int dy = p.y - _oy;
        translate(dx, dy);
    }

    /**
     * Translates the shape
     */
    public void translate(int dx, int dy) {
        super.translate(dx, dy);
        _shape.setFrame(_ox + _x, _oy + _y - _h, _w, _h);
    }

    /**
     * Draws the shape
     *
     * @param g
     *            the graphics context
     */
    public void draw(Graphics2D g) {
        // save the current color
        Color orgCol = g.getColor();
        if (_fColor != null) {
            g.setColor(_fColor);
            g.fill(_shape);
        }
        if (_bColor != null) {
            g.setColor(_bColor);
            g.draw(_shape);
        }
        g.setColor(orgCol);
    }

    public PathIterator getPathIterator(AffineTransform at) {
        return _shape.getPathIterator(at);
    }

    public PathIterator getPathIterator(AffineTransform at, double flatness) {
        return _shape.getPathIterator(at, flatness);
    }
}
TOP

Related Classes of simtools.shapes.BasicShape

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.