Package research.figure

Source Code of research.figure.EllipseFigure

/*
* @(#)EllipseFigure.java
*
* Project:    JHotdraw - a GUI framework for technical drawings
*        http://www.jhotdraw.org
*        http://jhotdraw.sourceforge.net
* Copyright:  ? by the original author(s) and all contributors
* License:    Lesser GNU Public License (LGPL)
*        http://www.opensource.org/licenses/lgpl-license.html
*/

package research.figure;

import research.*;
import research.connector.BoxConnector;
import research.connector.ChopEllipseConnector;
import research.connector.Connector;
import research.store.StorableOutput;
import research.store.StorableInput;

import java.awt.*;
import java.io.IOException;
import java.util.Vector;

/**
* An ellipse figure.
*
* @version <$CURRENT_VERSION$>
*/
public class EllipseFigure extends AbstractFigure {
    private static final int CENTER = 0;
    private static final int NORTH = 1;
    private static final int SOUTH = 2;
    private static final int WEST = 3;
    private static final int EAST = 4;


    private Rectangle fDisplayBox;

    /*
     * Serialization support.
     */
    private static final long serialVersionUID = -6856203289355118951L;
    private int ellipseFigureSerializedDataVersion = 1;

    public EllipseFigure() {
        this(new Point(0, 0), new Point(0, 0));
        initConnectors();
    }

    public EllipseFigure(Point origin, Point corner) {
        basicDisplayBox(origin, corner);
        initConnectors();
    }

    protected void initConnectors() {
        connector = new Connector[5];
        connector[NORTH] = new BoxConnector(this, BoxConnector.NORTH);
        connector[SOUTH] = new BoxConnector(this, BoxConnector.SOUTH);
        connector[WEST] = new BoxConnector(this, BoxConnector.WEST);
        connector[EAST] = new BoxConnector(this, BoxConnector.EAST);
        connector[CENTER] = new ChopEllipseConnector(this);
    }

    public Vector handles() {
        Vector handles = new Vector();
        BoxHandleKit.addHandles(this, handles);
        return handles;
    }

    public void basicDisplayBox(Point origin, Point corner) {
        fDisplayBox = new Rectangle(origin);
        fDisplayBox.add(corner);
    }

    public Rectangle getDisplayBox() {
        return getDisplayBox(null);
    }

    public Rectangle getDisplayBox(Rectangle r) {
        if (r == null)
            r = new Rectangle();
        r.setBounds(fDisplayBox);
        return r;
    }

    protected void basicMoveBy(int x, int y) {
        fDisplayBox.translate(x, y);
    }

    public void drawBackground(Graphics g) {
        Rectangle r = getDisplayBox();
        g.fillOval(r.x, r.y, r.width, r.height);
    }

    public void drawFrame(Graphics g) {
        Rectangle r = getDisplayBox();
        g.drawOval(r.x, r.y, r.width - 1, r.height - 1);
    }

    public Insets connectionInsets() {
        Rectangle r = fDisplayBox;
        int cx = r.width / 2;
        int cy = r.height / 2;
        return new Insets(cy, cx, cy, cx);
    }

    public Connector connectorAt(int x, int y) {
        Rectangle rect = getDisplayBox();
        if (!rect.contains(x, y)) return null;

        int index = findLocation(x, y);
        if(index < 0) return null;

        Connector[] array = getConnectors();
        return array[index];
    }

    private int findLocation(int x, int y) {
        Rectangle rect = getDisplayBox();

        if (x < rect.x + rect.width / 3) {
            if (y < rect.y + rect.height / 3) {
                return -1;
            } else if (y < rect.y + rect.height - rect.height / 3) {
                return WEST;
            } else {
                return -1;
            }
        } else if (x < rect.x + rect.width - rect.width / 3) {
            if (y < rect.y + rect.height / 3) {
                return NORTH;
            } else if (y < rect.y + rect.height - rect.height / 3) {
                return CENTER;
            } else {
               return SOUTH;
            }
        } else {
            if (y < rect.y + rect.height / 3) {
                return -1;
            } else if (y < rect.y + rect.height - rect.height / 3) {
                return EAST;
            } else {
                return -1;
            }
        }
    }

    public void write(StorableOutput dw) {
        super.write(dw);
        dw.writeInt(fDisplayBox.x);
        dw.writeInt(fDisplayBox.y);
        dw.writeInt(fDisplayBox.width);
        dw.writeInt(fDisplayBox.height);
    }

    public void read(StorableInput dr) throws IOException {
        super.read(dr);
        fDisplayBox = new Rectangle(
                dr.readInt(),
                dr.readInt(),
                dr.readInt(),
                dr.readInt());
    }
}
TOP

Related Classes of research.figure.EllipseFigure

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.