Java Utililty Methods Draw String

List of utility methods to do Draw String

Description

The list of methods to do Draw String are organized into topic(s).

Method

voiddrawString(Graphics2D g, String string, double x, double y, Color color)
draw String
g.setColor(color);
g.setFont(myFont);
g.drawString(string, (int) x, (int) y);
voiddrawString(Graphics2D g, String string, int x, int y)
draw String
AffineTransform identity = new AffineTransform();
g.setTransform(identity);
g.setColor(Color.BLACK);
Font font = Font.decode("Arial-BOLD-18");
GlyphVector vector = font.createGlyphVector(g.getFontRenderContext(), string);
Shape shape = vector.getOutline();
g.setStroke(new BasicStroke(3));
g.setFont(font);
...
voiddrawString(Graphics2D g, String string, int x, int y, boolean includeOutline)
Draw a string in Verdana size 14.
Font font = (new Font("Verdana", 0, 14));
if (includeOutline) {
    Shape textShape = font.createGlyphVector(g.getFontRenderContext(), string).getOutline(x, y);
    g.setComposite(AlphaComposite.SrcOver);
    if (includeOutline) {
        g.setStroke(new BasicStroke(3, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
        g.setColor(Color.white);
        g.draw(textShape);
...
intdrawString(Graphics2D g2, String str, int x, int y, int halign, int valign)
draw String
FontMetrics fm = g2.getFontMetrics();
int w = fm.stringWidth(str);
int h = fm.getAscent() - fm.getDescent();
Point a = new Point(w * halign / 2, h * valign / 2);
g2.drawString(str, x - a.x, y + h - a.y);
return y + h;
voiddrawString(Graphics2D gfx, String text, int x, int y)
draw String
Color color = gfx.getColor();
if (gfx.getColor().getRed() >= 33 || gfx.getColor().getBlue() >= 33 || gfx.getColor().getGreen() >= 33) {
    gfx.setColor(Color.BLACK); 
    gfx.drawString(text, x + 1, y + 1);
gfx.setColor(color);
gfx.drawString(text, x, y);
Rectangle2DdrawString(Graphics2D graphics, String text, double x, double y, boolean centerHorizontal, boolean centerVertical, Color backgroundColor)
draw String
return drawString(graphics, text, x, y, centerHorizontal, centerVertical, backgroundColor, Color.BLACK);
ImagedrawString(Image img, String text, Color color)
Dibuja un texto en el centro de la imagen, con el color indicado.
BufferedImage result = new BufferedImage(img.getWidth(null), img.getHeight(null),
        BufferedImage.TYPE_INT_ARGB);
Graphics2D g = (Graphics2D) result.getGraphics();
g.drawImage(img, 0, 0, null);
Font font = new Font(Font.SANS_SERIF, Font.BOLD, 12);
Rectangle2D r = font.getStringBounds(text, g.getFontRenderContext());
g.setFont(font);
g.setColor(color);
...
RectangledrawString(String pString, Graphics2D pG, int pX, int pY, int pWidth, int pHeight, boolean pYOverflow, boolean pShrinkWords, String pJustification, boolean pDraw)
Draw a string onto a graphics2d object, wrapping words at pWidth pixels wide.
FontMetrics lFontMetrics = pG.getFontMetrics();
Font lDrawFont = pG.getFont();
char[] lCharacters = pString.toCharArray();
int lFontHeight = lFontMetrics.getHeight();
int lLeading = 0;
int lY = pY;
boolean lFirstLine = true;
boolean lSuppressLastLine = false;
...
voiddrawString(String txt, int x, int y, Graphics g)
draw String
int x2 = 0;
for (int v = 0; v < txt.length(); v++) {
    char c = txt.charAt(v);
    drawChar(c, x2 + x, y, g);
    x2 += 8;
voiddrawStringAlignCenter(Graphics2D g2d, String s, int x, int y)
draw String Align Center
int stringLen = (int) g2d.getFontMetrics().getStringBounds(s, g2d).getWidth();
g2d.drawString(s, x - stringLen / 2, y);