01: package org.swingml.system;
02:
03: import java.awt.*;
04: import java.awt.image.*;
05:
06: /**
07: * @author CrossLogic
08: */
09: public class SwingMLTextUtilities {
10:
11: private static Graphics2D graphics = null;
12:
13: private static Graphics2D getGraphics() {
14: if (graphics == null) {
15: BufferedImage img = new BufferedImage(1, 1,
16: BufferedImage.TYPE_INT_RGB);
17: graphics = img.createGraphics();
18: }
19:
20: return graphics;
21: }
22:
23: /**
24: * Calculate the width in pixels of a given string.
25: * @param text
26: * @return
27: */
28: public static int getStringWidth(String text) {
29: int result = 0;
30: if (getGraphics() != null) {
31: result = getGraphics().getFontMetrics().stringWidth(text);
32: }
33: return result;
34: }
35:
36: /**
37: * Calculate the height in pixels of a given string.
38: * @param text
39: * @return
40: */
41: public static int getStringHeight() {
42: int result = 0;
43: if (getGraphics() != null) {
44: result = getGraphics().getFontMetrics().getHeight();
45: }
46: return result;
47: }
48:
49: }
|