1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| public class TextUtils {
/**
* Obtient la taille de l'espace pris par le texte
*/
public static Dimension getSize(Graphics g, String string) {
return getSize(g, string, null);
}
/**
* Obtient la taille de l'espace pris par le texte avec des marges
*/
public static Dimension getSize(Graphics g, String string, Insets insets) {
return getSize(g, string, insets==null?new Insets(0,0,0,0):insets);
}
/**
* Obtient la taille de l'espace pris par le texte avec des marges
*/
public static Dimension getSize(Graphics g, String string, int top, int left, int bottom, int right) {
FontMetrics fontMetrics = g.getFontMetrics();
Rectangle2D textBounds = fontMetrics.getStringBounds(string, g);
return new Dimension((int)(textBounds.getWidth()+left+right), (int)(textBounds.getHeight()+top+bottom));
}
/**
* Ecrit un texte aligné sur un point
* @param g
* @param string
* @param x du point
* @param y du point
* @param halign SwingConstants.LEFT, SwingConstants.RIGHT ou SwingConstants.CENTER
* @param valign SwingConstants.TOP, SwingConstants.BOTTOM ou SwingConstants.CENTER
*/
public static void drawString(Graphics g, String string, int x, int y, int halign, int valign) {
FontMetrics fontMetrics = g.getFontMetrics();
Rectangle2D bounds = fontMetrics.getStringBounds(string, g);
double xx=x;
double yy=y;
switch(halign) {
case SwingConstants.CENTER:
xx-=bounds.getWidth()/2;
break;
case SwingConstants.RIGHT:
xx-=bounds.getWidth();
break;
}
switch(valign) {
case SwingConstants.CENTER:
yy -= bounds.getHeight()/2;
break;
case SwingConstants.BOTTOM:
yy -= bounds.getHeight() - fontMetrics.getDescent();
break;
case SwingConstants.TOP:
yy -= bounds.getHeight() - fontMetrics.getAscent();
break;
}
bounds = new Rectangle2D.Double(xx, yy, bounds.getWidth(), bounds.getHeight());
drawString(g, string, bounds, SwingConstants.CENTER, SwingConstants.CENTER);
}
/**
* Ecrit un texte aligné sur un rectangle
* @param g
* @param string
* @param bounds rectangle
* @param halign SwingConstants.LEFT, SwingConstants.RIGHT ou SwingConstants.CENTER
* @param valign SwingConstants.TOP, SwingConstants.BOTTOM ou SwingConstants.CENTER
*/
public static void drawString(Graphics g, String string, Rectangle2D bounds, int halign, int valign) {
FontMetrics fontMetrics = g.getFontMetrics();
Rectangle2D textBounds = fontMetrics.getStringBounds(string, g);
double textWidth = textBounds.getWidth();
double x,y;
switch (halign) {
case SwingConstants.LEFT:
x = bounds.getX();
break;
case SwingConstants.RIGHT:
x = bounds.getX() + bounds.getWidth() - textWidth;
break;
case SwingConstants.CENTER:
default:
x = bounds.getX()
+ (bounds.getWidth() - textWidth) / 2;
break;
}
switch (valign) {
case SwingConstants.TOP:
y = bounds.getY() + fontMetrics.getAscent() - fontMetrics.getDescent();
break;
case SwingConstants.BOTTOM:
y = bounds.getY()
+ bounds.getHeight() - fontMetrics.getDescent() ;
break;
case SwingConstants.CENTER:
default:
y = bounds.getY()
+ bounds.getHeight()/ 2 - ( ( fontMetrics.getAscent() + fontMetrics.getDescent() ) / 2 ) + fontMetrics.getAscent() ;
break;
}
g.drawString(string, (int)x, (int)y);
}
} |