Пример #1
0
  /**
   * Draws a double headed arrow with arrow heads of a given width and height.
   *
   * @param aG the canvas to draw on;
   * @param aX1 the starting X position of the arrow;
   * @param aY1 the starting Y position of the arrow;
   * @param aX2 the ending X position of the arrow;
   * @param aY2 the ending Y position of the arrow;
   * @param aArrowWidth the total width of the arrow head;
   * @param aArrowHeight the total height of the arrow head.
   */
  public static final void drawDoubleHeadedArrow(
      final Graphics aG,
      final int aX1,
      final int aY1,
      final int aX2,
      final int aY2,
      final int aArrowWidth,
      final int aArrowHeight) {
    final Graphics2D g2d = (Graphics2D) aG.create();

    final int lineWidth = Math.abs(aX2 - aX1);
    final int threshold = (2 * aArrowWidth) + 2;
    try {
      int x1 = aX1;
      int x2 = aX2;

      if (lineWidth > threshold) {
        drawArrowHead(g2d, aX1, aY1, LEFT_FACING, aArrowWidth, aArrowHeight);
        // why x2 needs to be shifted by one pixel is beyond me...
        drawArrowHead(g2d, aX2 + 1, aY2, RIGHT_FACING, aArrowWidth, aArrowHeight);

        x1 += aArrowWidth - 1;
        x2 -= aArrowWidth + 1;
      }

      g2d.drawLine(x1, aY1, x2, aY2);
    } finally {
      g2d.dispose();
    }
  }
Пример #2
0
  @Override
  public void paintIcon(Component c, Graphics g, int x, int y) {
    Graphics2D g2 = (Graphics2D) g.create();
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.translate(x, y);

    g2.setStroke(new BasicStroke(BORDER_WIDTH));
    g2.setPaint(LINE_COLOR);
    g2.draw(BORDER);

    g2.setStroke(new BasicStroke(SLIT_WIDTH));
    g2.setColor(UIManager.getColor("Panel.background"));

    int n = SLIT_NUM + 1;
    int v = ICON_SIZE / n;
    int m = n * v;
    for (int i = 1; i < n; i++) {
      int a = i * v;
      g2.drawLine(a, 0, a, m);
      g2.drawLine(0, a, m, a);
    }

    // g2.drawLine(1 * v, 0 * v, 1 * v, 4 * v);
    // g2.drawLine(2 * v, 0 * v, 2 * v, 4 * v);
    // g2.drawLine(3 * v, 0 * v, 3 * v, 4 * v);
    // g2.drawLine(0 * v, 1 * v, 4 * v, 1 * v);
    // g2.drawLine(0 * v, 2 * v, 4 * v, 2 * v);
    // g2.drawLine(0 * v, 3 * v, 4 * v, 3 * v);

    g2.setPaint(LINE_COLOR);
    Rectangle2D b = ARROW.getBounds();
    Point2D p = new Point2D.Double(b.getX() + b.getWidth() / 2d, b.getY() + b.getHeight() / 2d);
    AffineTransform toCenterAT =
        AffineTransform.getTranslateInstance(ICON_SIZE / 2d - p.getX(), ICON_SIZE / 2d - p.getY());
    g2.fill(toCenterAT.createTransformedShape(ARROW));
    g2.dispose();
  }