All Downloads are FREE. Search and download functionalities are using the official Maven repository.

devutility.internal.awt.FontHelper Maven / Gradle / Ivy

There is a newer version: 1.3.8.1
Show newest version
package devutility.internal.awt;

import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;

public class FontHelper {
	public static int getFontWidth(Graphics2D graphics2d, Font font, String value) {
		FontMetrics fontMetrics = graphics2d.getFontMetrics(font);
		return fontMetrics.stringWidth(value);
	}

	public static int getFontHeight(Graphics2D graphics2d, Font font) {
		FontMetrics fontMetrics = graphics2d.getFontMetrics(font);
		return fontMetrics.getHeight();
	}

	public static float getXOffset(int backgroundWidth, Graphics2D graphics2d, Font font, String value) {
		int width = FontHelper.getFontWidth(graphics2d, font, value);
		return (backgroundWidth - width) / 2;
	}
	
	public static float getYOffset(int backgroundHeight, Graphics2D graphics2d, Font font) {
		FontMetrics fontMetrics = graphics2d.getFontMetrics(font);
		int height = fontMetrics.getHeight();
		return (backgroundHeight - height) / 2 + fontMetrics.getAscent();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy