Package research.figure

Source Code of research.figure.RoundRectangleFigure

/*
* @(#)RoundRectangleFigure.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.Connector;
import research.store.StorableOutput;
import research.store.StorableInput;

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

import research.util.Geom;

/**
* A round rectangle figure.
*
* @see RadiusHandle
*
* @version <$CURRENT_VERSION$>
*/
public class RoundRectangleFigure 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;
  private int         fArcWidth;
  private int         fArcHeight;
  private static final int DEFAULT_ARC = 8;

  /*
   * Serialization support.
   */
  private static final long serialVersionUID = 7907900248924036885L;
  private int roundRectangleSerializedDataVersion = 1;

  public RoundRectangleFigure() {
    this(new Point(0,0), new Point(0,0));
    fArcWidth = fArcHeight = DEFAULT_ARC;
        initConnectors();
  }

  public RoundRectangleFigure(Point origin, Point corner) {
    basicDisplayBox(origin,corner);
    fArcWidth = fArcHeight = DEFAULT_ARC;
        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 ShortestDistanceConnector(this);
    }

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

        //check arc
        int rx = Geom.range(0, fDisplayBox.width, fArcWidth);
    int ry = Geom.range(0, fDisplayBox.height, fArcHeight);
    _setArc(rx, ry);
  }

  /**
   * Sets the arc's witdh and height.
   */
  public void setArc(int width, int height) {
    willChange();
    _setArc(width, height);
    changed();
  }

    protected void _setArc(int width, int height) {
    fArcWidth = width;
    fArcHeight = height;
    }

  /**
   * Gets the arc's width and height.
   */
  public Point getArc() {
    return new Point(fArcWidth, fArcHeight);
  }

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

    handles.addElement(new RadiusHandle(this));

    return handles;
  }

  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.fillRoundRect(r.x, r.y, r.width, r.height, fArcWidth, fArcHeight);
  }

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

  public Insets connectionInsets() {
    return new Insets(fArcHeight/2, fArcWidth/2, fArcHeight/2, fArcWidth/2);
  }

  public Connector connectorAt(int x, int y) {
    //return new ShortestDistanceConnector(this); // just for demo purposes

        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);
    dw.writeInt(fArcWidth);
    dw.writeInt(fArcHeight);
  }

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

}
TOP

Related Classes of research.figure.RoundRectangleFigure

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.