/**
   * straightenPoints This is a simpler version of the.
   *
   * @see updateIfNotRectilinear that simply ensures that the lines are horizontal or vertical
   *     without any intelligence in terms of shortest distance around a rectangle.
   * @param newLine PointList to check for rectilinear qualities and change if necessary.
   * @param tolerance int tolerance value by which points will be straightened in HiMetrics
   */
  protected static void straightenPoints(final PointList newLine, final int tolerance) {
    for (int i = 0; i < newLine.size() - 1; i++) {
      final Point ptCurrent = newLine.getPoint(i);
      final Point ptNext = newLine.getPoint(i + 1);

      final int xDelta = Math.abs(ptNext.x - ptCurrent.x);
      final int yDelta = Math.abs(ptNext.y - ptCurrent.y);

      if (xDelta < yDelta) {
        if (xDelta > tolerance) {
          return;
        }
        if (i == newLine.size() - 2) {
          // The last point is more important than the other (not
          // change the end of the line)
          ptCurrent.x = ptNext.x;
        } else {
          ptNext.x = ptCurrent.x;
        }
      } else {
        if (yDelta > tolerance) {
          return;
        }
        if (i == newLine.size() - 2) {
          // The last point is more important than the other (not
          // change the end of the line)
          ptCurrent.y = ptNext.y;
        } else {
          ptNext.y = ptCurrent.y;
        }
      }

      newLine.setPoint(ptNext, i + 1);
    }
  }
  @Override
  public void route(Connection conn) {
    if (isDirty) {
      ignoreInvalidate = true;
      processStaleConnections();
      isDirty = false;
      List<?> updated = algorithm.solve();
      Connection current;
      for (int i = 0; i < updated.size(); i++) {
        Path path = (Path) updated.get(i);
        current = (Connection) path.data;
        current.revalidate();

        PointList points = path.getPoints().getCopy();
        Point ref1;
        Point ref2;
        Point start;
        Point end;
        ref1 = new PrecisionPoint(points.getPoint(1));
        ref2 = new PrecisionPoint(points.getPoint(points.size() - 2));
        current.translateToAbsolute(ref1);
        current.translateToAbsolute(ref2);

        start = current.getSourceAnchor().getLocation(ref1).getCopy();
        end = current.getTargetAnchor().getLocation(ref2).getCopy();

        current.translateToRelative(start);
        current.translateToRelative(end);
        points.setPoint(start, 0);
        points.setPoint(end, points.size() - 1);

        current.setPoints(points);
      }
      ignoreInvalidate = false;
    }
  }
Beispiel #3
0
    public void layout(IFigure container) {
      Rectangle area = container.getClientArea();
      // calculate a virtual area

      if (scale != null && scale.isDirty())
        M =
            Math.max(
                    FigureUtilities.getTextWidth(
                        scale.format(scale.getRange().getLower()), scale.getFont()),
                    FigureUtilities.getTextWidth(
                        scale.format(scale.getRange().getUpper()), scale.getFont()))
                / 2;

      int h = area.height;
      int w = area.width;
      if (h > HW_RATIO * (w - 2 * M)) h = (int) (HW_RATIO * (w - 2 * M));
      // else if (w > h/HW_RATIO + 2*M)
      //	w = (int) (h/HW_RATIO + 2*M);
      double r = h / (1 - Math.sin(ALPHA) / 2);
      int x = (int) (area.x - r * (1.0 - Math.cos(ALPHA)) + M);
      int y = area.y;

      area = new Rectangle(x, y, (int) (2 * r), (int) (2 * r));
      Point center = area.getCenter();

      if (scale != null) {
        scale.setBounds(area);
      }

      if (ramp != null && ramp.isVisible()) {
        Rectangle rampBounds = area.getCopy();
        ramp.setBounds(
            rampBounds.shrink(
                area.width / 4 - ramp.getRampWidth(), area.height / 4 - ramp.getRampWidth()));
      }

      if (valueLabel != null) {
        Dimension labelSize = valueLabel.getPreferredSize();
        valueLabel.setBounds(
            new Rectangle(
                area.x + area.width / 2 - labelSize.width / 2,
                area.y
                    + area.height / 2
                    - area.height / 4
                    - (scale.getInnerRadius() - area.height / 4) / 2
                    - labelSize.height / 2,
                labelSize.width,
                labelSize.height));
      }

      if (needle != null && scale != null) {
        needlePoints.setPoint(
            new Point(center.x + area.width / 4, center.y - NEEDLE_WIDTH / 2 + 3), 0);
        scale.getScaleTickMarks();
        needlePoints.setPoint(
            new Point(center.x + scale.getInnerRadius() - GAP_BTW_NEEDLE_SCALE, center.y), 1);
        needlePoints.setPoint(
            new Point(center.x + area.width / 4, center.y + NEEDLE_WIDTH / 2 - 3), 2);

        double valuePosition = 360 - scale.getValuePosition(getCoercedValue(), false);
        if (maximum > minimum) {
          if (value > maximum) valuePosition += 8;
          else if (value < minimum) valuePosition -= 8;
        } else {
          if (value > minimum) valuePosition -= 8;
          else if (value < maximum) valuePosition += 8;
        }
        needlePoints.setPoint(
            PointsUtil.rotate(needlePoints.getPoint(0), valuePosition, center), 0);
        needlePoints.setPoint(
            PointsUtil.rotate(needlePoints.getPoint(1), valuePosition, center), 1);
        needlePoints.setPoint(
            PointsUtil.rotate(needlePoints.getPoint(2), valuePosition, center), 2);
        needle.setPoints(needlePoints);
      }

      //			if(needleCenter != null){
      //				needleCenter.setBounds(new Rectangle(center.x - area.width/4,
      //						center.y - area.height/4,
      //						area.width/2, area.height/2));
      //			}

    }
    public void layout(IFigure container) {
      Rectangle area = container.getClientArea();

      area.width = Math.min(area.width, area.height);
      area.height = area.width;
      area.shrink(BORDER_WIDTH, BORDER_WIDTH);

      Point center = area.getCenter();

      if (scale != null) {
        scale.setBounds(area);
      }

      if (ramp != null && ramp.isVisible()) {
        Rectangle rampBounds = area.getCopy();
        ramp.setBounds(rampBounds.shrink(area.width / 4, area.height / 4));
      }

      if (valueLabel != null) {
        Dimension labelSize = valueLabel.getPreferredSize();
        valueLabel.setBounds(
            new Rectangle(
                area.x + area.width / 2 - labelSize.width / 2,
                area.y + area.height * 7 / 8 - labelSize.height / 2,
                labelSize.width,
                labelSize.height));
      }

      if (needle != null && scale != null) {
        needlePoints.setPoint(new Point(center.x, center.y - NeedleCenter.DIAMETER / 2 + 3), 0);
        scale.getScaleTickMarks();
        needlePoints.setPoint(
            new Point(
                center.x
                    + area.width / 2
                    - RoundScaleTickMarks.MAJOR_TICK_LENGTH
                    - GAP_BTW_NEEDLE_SCALE,
                center.y),
            1);
        needlePoints.setPoint(new Point(center.x, center.y + NeedleCenter.DIAMETER / 2 - 3), 2);

        double valuePosition = 360 - scale.getValuePosition(getCoercedValue(), false);
        if (maximum > minimum) {
          if (value > maximum) valuePosition += 10;
          else if (value < minimum) valuePosition -= 10;
        } else {
          if (value > minimum) valuePosition -= 10;
          else if (value < maximum) valuePosition += 10;
        }
        needlePoints.setPoint(
            PointsUtil.rotate(needlePoints.getPoint(0), valuePosition, center), 0);
        needlePoints.setPoint(
            PointsUtil.rotate(needlePoints.getPoint(1), valuePosition, center), 1);
        needlePoints.setPoint(
            PointsUtil.rotate(needlePoints.getPoint(2), valuePosition, center), 2);
        needle.setPoints(needlePoints);
      }

      if (needleCenter != null) {
        needleCenter.setBounds(
            new Rectangle(
                center.x - NeedleCenter.DIAMETER / 2,
                center.y - NeedleCenter.DIAMETER / 2,
                NeedleCenter.DIAMETER,
                NeedleCenter.DIAMETER));
      }
    }
  /** Applies a hexagon-based layout to the container. */
  @Override
  public void layout(IFigure container) {

    // Get the maximum bounding box we can use for layout out sub-figures.
    Rectangle limit = container.getClientArea();

    // Math.cos() may vary across platforms, so pre-compute it.
    double cosPiDivSix = Math.sqrt(3) / 2.0;

    if (!rotated) {

      // Get the hexagon side lengths supported by the current
      // width/height of
      // the client area.
      double lx =
          2.0
              * (double) (limit.width - horizontalSpacing * (columns - 1))
              / (double) (3 * columns + 1);
      double ly =
          (double) (limit.height - verticalSpacing * (rows - 1))
              / (cosPiDivSix * (double) (rows * 2 + 1));

      // Determine which length to use and flag the direction that needs
      // to be
      // padded (x or y).
      double l;
      int paddingX = 0, paddingY = 0;
      if (ly >= lx) { // Size restricted by width.
        l = lx;
      } else { // Size restricted by height.
        l = ly;
        paddingX++;
      }

      // Compute the half-height and half-width of each hexagon.
      lx = l / 2; // shortcut: sin(PI/6) = 1/2!
      ly = l * cosPiDivSix;

      // Compute the necessary padding for whichever dimension needs it.
      if (paddingX != 0) { // Size restricted by height.
        paddingX =
            (int)
                (((double) limit.width
                        - horizontalSpacing * (rows - 1)
                        - (lx * (double) (3 * columns + 1)))
                    / 2.0);
      } else { // Size restricted by width.
        paddingY =
            (int)
                (((double) limit.height
                        - verticalSpacing * (rows - 1)
                        - (ly * (double) (2 * rows + 1)))
                    / 2.0);
      }

      // Update the PointList used by each hexagon.
      // Note: The hexagons store references to the PointList, so we do
      // not need to call hexagon.setPoints(), which, in fact,
      // refreshes/repaints the Shape, in the quadratic loop below.
      points.setPoint(new Point((int) (lx), 0), 0);
      points.setPoint(new Point((int) (lx + l), 0), 1);
      points.setPoint(new Point((int) (l + l), (int) ly), 2);
      points.setPoint(new Point((int) (lx + l), (int) (ly + ly)), 3);
      points.setPoint(new Point((int) (lx), (int) (ly + ly)), 4);
      points.setPoint(new Point(0, (int) ly), 5);

      // We want to limit math ops, so compute the factors used in the
      // loop.
      double xFactor = l + lx + (double) horizontalSpacing;
      double yFactor = 2 * ly + (double) verticalSpacing;

      // Compute the width and height of each hexagon.
      int width = (int) Math.ceil(2 * l);
      int height = (int) Math.ceil(2 * ly);

      // Variables used throughout the below loop.
      int i, row, column, x, y, w, h;

      // Loop over the IFigures in the container with this layout.
      for (Object childObject : container.getChildren()) {
        IFigure child = (IFigure) childObject;

        // Get the constraints (and the x, y, w, h offsets from it).
        GridData constraint = getConstraint(child);
        Rectangle offsets = constraint.getOffsets();

        // Get the index and compute the row and column for the index.
        i = constraint.getIndex();
        row = i / columns;
        column = i % columns;

        // Compute the bounds of the cell in the row, column position.
        x = paddingX + (int) (column * xFactor) + offsets.x;
        y = paddingY + (int) (row * yFactor + ly * (double) (column % 2)) + offsets.y;
        w = width + offsets.width;
        h = height + offsets.height;

        // Set the bounds for the child IFigure.
        child.setBounds(new Rectangle(x, y, w, h));
      }
    } else { // All hexagons are rotated 90 degrees.

      // Get the hexagon side lengths supported by the current
      // width/height of
      // the client area.
      double lx =
          (double) (limit.width - horizontalSpacing * (columns - 1))
              / (cosPiDivSix * (double) (columns * 2 + 1));
      double ly =
          2.0 * (double) (limit.height - verticalSpacing * (rows - 1)) / (double) (3 * rows + 1);

      // Determine which length to use and flag the direction that needs
      // to be
      // padded (x or y).
      double l;
      int paddingX = 0, paddingY = 0;
      if (ly >= lx) { // Size restricted by width.
        l = lx;
      } else { // Size restricted by height.
        l = ly;
        paddingX++;
      }

      // Compute the half-height and half-width of each hexagon.
      lx = l * cosPiDivSix;
      ly = l / 2; // shortcut: sin(PI/6) = 1/2!

      // Compute the necessary padding for whichever dimension needs it.
      if (paddingX != 0) { // Size restricted by height.
        paddingX =
            (int)
                (((double) limit.width
                        - horizontalSpacing * (columns - 1)
                        - (lx * (double) (2 * columns + 1)))
                    / 2.0);
      } else { // Size restricted by width.
        paddingY =
            (int)
                (((double) limit.height
                        - verticalSpacing * (rows - 1)
                        - (ly * (double) (3 * rows + 1)))
                    / 2.0);
      }

      // Update the PointList used by each hexagon.
      // Note: The hexagons store references to the PointList, so we do
      // not need to call hexagon.setPoints(), which, in fact,
      // refreshes/repaints the Shape, in the quadratic loop below.
      points.setPoint(new Point((int) lx, 0), 0);
      points.setPoint(new Point((int) (lx + lx), (int) ly), 1);
      points.setPoint(new Point((int) (lx + lx), (int) (ly + l)), 2);
      points.setPoint(new Point((int) lx, (int) (l + l)), 3);
      points.setPoint(new Point(0, (int) (ly + l)), 4);
      points.setPoint(new Point(0, (int) ly), 5);

      // We want to limit math ops, so compute the factors used in the
      // loop.
      double xFactor = lx + lx + (double) horizontalSpacing;
      double yFactor = l + ly + (double) verticalSpacing;

      // Compute the width and height of each hexagon.
      int width = (int) Math.ceil(lx + lx);
      int height = (int) Math.ceil(l + l);

      // Variables used throughout the below loop.
      int i, row, column, x, y, w, h;

      // Loop over the IFigures in the container with this layout.
      for (Object childObject : container.getChildren()) {
        IFigure child = (IFigure) childObject;

        // Get the constraints (and the x, y, w, h offsets from it).
        GridData constraint = getConstraint(child);
        Rectangle offsets = constraint.getOffsets();

        // Get the index and compute the row and column for the index.
        i = constraint.getIndex();
        row = i / columns;
        column = i % columns;

        // Compute the bounds of the cell in the row, column position.
        x = paddingX + (int) (column * xFactor + lx * (double) (row % 2)) + offsets.x;
        y = paddingY + (int) (row * yFactor) + offsets.y;
        w = width + offsets.width;
        h = height + offsets.height;

        // Set the bounds for the child IFigure.
        child.setBounds(new Rectangle(x, y, w, h));
      }
    }

    return;
  }