Tuesday, May 26, 2009

Bordered text

How about adding a fancy bordered text to your customized UI? Simple, draw the text with the border color four times around the anchor point with just one pixel difference. Then draw the text with the desired color at the anchor point. Did not get it? How about the source code below?

public void drawBorderedText(Graphics g, String text,
  int x, int y, int anchor, int textColor, int borderColor)
{
  // draw border
  g.setColor(borderColor);
  g.drawString(text, x - 1, y - 1, anchor);
  g.drawString(text, x - 1, y + 1, anchor);
  g.drawString(text, x + 1, y - 1, anchor);
  g.drawString(text, x + 1, y + 1, anchor);
  // draw text
  g.setColor(textColor);
  g.drawString(text, x, y, anchor);
}

No comments: