Java Draw String drawStringInBox(Graphics g, String string, int x, int y)

Here you can find the source of drawStringInBox(Graphics g, String string, int x, int y)

Description

draw String In Box

License

Open Source License

Declaration

public static void drawStringInBox(Graphics g, String string, int x, int y) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;

public class Main {
    public static void drawStringInBox(Graphics g, String string, int x, int y) {
        Color color = g.getColor();
        FontMetrics metrics = g.getFontMetrics();
        y -= metrics.getAscent();/*from   w  w  w. java2s.c  o  m*/
        int width = metrics.stringWidth(string);
        int height = metrics.getHeight();
        g.setColor(Color.WHITE);
        g.fillRect(x, y, width + 6, height + 6);
        g.setColor(color);
        g.drawRect(x, y, width + 6, height + 6);
        g.drawString(string, x + 3, y + 3 + metrics.getAscent());
    }
}

Related

  1. drawString(String txt, int x, int y, Graphics g)
  2. drawStringAlignCenter(Graphics2D g2d, String s, int x, int y)
  3. drawStringAt(Graphics g, String t, int x, int y, int where)
  4. drawStringAtPoint(Graphics g, String s, Point p)
  5. drawStringEx(Graphics g1, String s, Font font, Rectangle rect, int align, int valign)
  6. drawStringLeft(Graphics2D g, String text, Rectangle rect, int fontHeight, Color c)
  7. drawStringOnImage(Image image, String string, int x, int y)
  8. drawStringOnScreen(String s, Color color, long milseconds)
  9. drawStringPair(Graphics2D g, String str1, String str2, int left, int right, int y, int size, Color color, boolean bold)