private boolean textFitsInFont(Graphics g, Font font, int width, int height) {

		// padding so the text does not touch the border
		int padding = 2;
		FontMetrics fontMetrics = g.getFontMetrics(font);
		int textWidth = fontMetrics.stringWidth(number) + padding;
		if (textWidth > width) {
			return false;
		}

		int textHeight = fontMetrics.getHeight();
		return textHeight < height;
	}
An icon that paints the given number
Ghidra/Framework/Docking/src/main/java/docking/resources/icons/NumberIcon.java