示例#1
0
  /**
   * Draw multi line Hint type text in the specified Graphics context<br>
   * at the specified location.
   */
  public static void drawHint(
      Graphics2D g, String text, int x, int y, Color bgColor, Color textColor) {
    final Graphics2D g2 = (Graphics2D) g.create();

    final Rectangle2D stringRect = getStringBounds(g, text);
    // calculate hint rect
    final RoundRectangle2D backgroundRect =
        new RoundRectangle2D.Double(
            x, y, (int) (stringRect.getWidth() + 10), (int) (stringRect.getHeight() + 8), 8, 8);

    g2.setStroke(new BasicStroke(1.3f));
    // draw translucent background
    g2.setColor(bgColor);
    mixAlpha(g2, AlphaComposite.SRC_OVER, 1f / 2f);
    // g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
    g2.fill(backgroundRect);
    // draw background border
    g2.setColor(ColorUtil.mix(bgColor, Color.black));
    mixAlpha(g2, AlphaComposite.SRC_OVER, 2f / 1f);
    // g2.setComposite(AlphaComposite.Src);
    g2.draw(backgroundRect);
    // draw text
    g2.setColor(textColor);
    drawString(g2, text, x + 5, y + 4, false);

    g2.dispose();
  }
示例#2
0
  /**
   * Draw a horizontal and vertical centered text on specified position. This function handle multi
   * lines string ('\n' character used a line separator).
   */
  public static void drawCenteredString(Graphics g, String text, int x, int y, boolean shadow) {
    if (StringUtil.isEmpty(text)) return;

    final Color color = g.getColor();
    final Color shadowColor = ColorUtil.mix(color, Color.black);
    final Rectangle2D textRect = getStringBounds(g, text);

    final double offX = textRect.getX();
    double curY = y - (textRect.getY() + (textRect.getHeight() / 2));

    for (String s : text.split("\n")) {
      final Rectangle2D r = getStringBounds(g, s);
      final int curX = (int) (x - (offX + (r.getWidth() / 2)));

      if (shadow) {
        g.setColor(shadowColor);
        g.drawString(s, curX + 1, (int) (curY + 1));
        g.setColor(color);
      }
      g.drawString(s, curX, (int) curY);
      curY += r.getHeight();
    }
  }