Here you can find the source of drawStringInBox(Graphics g, String string, int x, int y)
public static void drawStringInBox(Graphics g, String string, int x, int y)
//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()); } }