Package research.figure

Source Code of research.figure.TriangleFigure

package research.figure;

import research.AbstractFigure;
import research.BoxHandleKit;
import research.connector.Connector;
import research.connector.BoxConnector;
import research.connector.ChopBoxConnector;

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

/**
* Created by IntelliJ IDEA.
* User: zhangwei
* Date: 2004-7-3
* Time: 17:57:53
* To change this template use File | Settings | File Templates.
*/
public class TriangleFigure extends AbstractFigure {

    private static final int NORTH = 0;
    private static final int SOUTH = 1;

    public static final String DISPLAY_BOX = "displayBox";

    protected transient Polygon polygon = null;

    public static Polygon generateTriangle(Polygon polygon, Rectangle rect) {
        if (polygon == null)
            polygon = new Polygon();
        else
            polygon.reset();
        polygon.addPoint(rect.x + rect.width / 2, rect.y);
        polygon.addPoint(rect.x, rect.y + rect.height);
        polygon.addPoint(rect.x + rect.width, rect.y + rect.height);
        return polygon;
    }

    public TriangleFigure() {
        this(new Point(0, 0), new Point(4, 4));
    }

    public TriangleFigure(Point origin, Point corner) {
        Rectangle displayBox = new Rectangle(origin);
        displayBox.add(corner);
        this.setAttribute(DISPLAY_BOX, displayBox);
        polygon = generateTriangle(polygon, displayBox);
        initConnectors();
    }

    protected void initConnectors() {
        connector = new Connector[2];
        connector[NORTH] = new BoxConnector(this, BoxConnector.NORTH);
        connector[SOUTH] = new BoxConnector(this, BoxConnector.SOUTH);
    }

    public void basicDisplayBox(Point origin, Point corner) {
        Rectangle displayBox = (Rectangle) this.getAttribute(DISPLAY_BOX);
        displayBox.setSize(0, 0);
        displayBox.setLocation(origin);
        displayBox.add(corner);
        polygon = generateTriangle(polygon, displayBox);
        this._setAttribute(DISPLAY_BOX, displayBox);
    }

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

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

    public Rectangle getDisplayBox(Rectangle r) {
        if (r == null)
            r = new Rectangle();
        Rectangle displayBox = (Rectangle) this.getAttribute(DISPLAY_BOX);
        r.setBounds(displayBox);
        return r;
    }

    protected void basicMoveBy(int x, int y) {
        Rectangle displayBox = (Rectangle) this.getAttribute(DISPLAY_BOX);
        displayBox.translate(x, y);
        polygon = generateTriangle(polygon, displayBox);
        this._setAttribute(DISPLAY_BOX, displayBox);
    }

    public void drawBackground(Graphics g) {
        Rectangle displayBox = (Rectangle) this.getAttribute(DISPLAY_BOX);
        polygon = generateTriangle(polygon, displayBox);
        g.fillPolygon(polygon);
    }

    public void drawFrame(Graphics g) {
        Rectangle displayBox = (Rectangle) this.getAttribute(DISPLAY_BOX);
        polygon = generateTriangle(polygon, displayBox);
        g.drawPolygon(polygon);
    }

    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 (y < rect.y + rect.height / 2) {
            return NORTH;
        } else {
            return SOUTH;
        }

    }
}
TOP

Related Classes of research.figure.TriangleFigure

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.