コード例 #1
0
ファイル: GraphicsUtil.java プロジェクト: adufour/Icy-Kernel
  /**
   * Draw a text in the specified Graphics context and at the specified position.<br>
   * This function handles multi lines string ('\n' character used a line separator).
   */
  public static void drawString(Graphics g, String text, int x, int y, boolean shadow) {
    if (StringUtil.isEmpty(text)) return;

    final Color color = g.getColor();
    final Color shadowColor;
    if (ColorUtil.getLuminance(color) > 128) shadowColor = ColorUtil.sub(color, Color.gray);
    else shadowColor = ColorUtil.add(color, Color.gray);
    final Rectangle2D textRect = getStringBounds(g, "M");

    // get height for a single line of text
    final double lineH = textRect.getHeight();
    final int curX = (int) (x - textRect.getX());
    double curY = y - textRect.getY();

    for (String s : text.split("\n")) {
      if (shadow) {
        g.setColor(shadowColor);
        g.drawString(s, curX + 1, (int) (curY + 1));
        g.setColor(color);
      }
      g.drawString(s, curX, (int) curY);
      curY += lineH;
    }
  }