Java Utililty Methods Graphics Draw String

List of utility methods to do Graphics Draw String

Description

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

Method

voiddrawVerticalText(Graphics2D graphics, Font font, Dimension2D dimension, String text)
draws the given text vertically using the specified font.
List<String> strings = new ArrayList<String>();
for (Character c : text.toCharArray()) {
    strings.add(c.toString());
drawVerticalText(graphics, font, dimension, strings);
voiddrawVerticalTextCenter(Graphics2D g2, String str, int x, int y)
draw Vertical Text Center
int strWidth = getStringWidth(g2, str);
drawVerticalText(g2, str, x + g2.getFontMetrics().getHeight() / 2, y + strWidth / 2);
floatdrawWrappedText(Graphics2D g2, float x, float y, float width, String text)
Draw given text to given Graphics2D view starting at x,y and wrapping lines at width pixels.
if (text == null || text.length() == 0)
    return y;
FontRenderContext frc = g2.getFontRenderContext();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
        RenderingHints.VALUE_ANTIALIAS_ON);
g2.setStroke(new BasicStroke(2.0f)); 
AttributedString astr = new AttributedString(text);
astr.addAttribute(TextAttribute.FONT, g2.getFont(), 0, text.length());
...
voidpaintCenteredString(Graphics2D g, String str, Font font, int centerX, int centerY)
Paint a String centered at a given (x,y) coordinate.
g.setFont(font);
FontMetrics fm = g.getFontMetrics();
Rectangle2D r = fm.getStringBounds(str, g);
float x = (float) (centerX - r.getWidth() / 2);
float y = (float) (centerY - r.getHeight() / 2 - r.getY());
g.drawString(str, x, y);
voidpaintOutline(Graphics g, String s, int textX, int textY, int thickness)
paint Outline
if (thickness > 10) {
    thickness = 10;
if (thickness > 0) {
    g.setColor(getDarkGray(thickness));
    for (int i = -thickness; i <= thickness; i++) {
        for (int j = -thickness; j <= thickness; j++) {
            if (i != 0 || j != 0) {
...
voidpaintShadowTitleFat(Graphics g, String title, int x, int y, Color frente, Color shadow, int desp)
paint Shadow Title Fat
paintShadowTitle(g, title, x, y, frente, shadow, desp, FAT, SwingConstants.HORIZONTAL);
voidpaintTextEffect(final Graphics2D g2d, final String s, final Color c, final int size, final double tx, final double ty, final boolean isShadow)
paint Text Effect
final float opacity = 0.8f;
final Composite oldComposite = g2d.getComposite();
final Color oldColor = g2d.getColor();
float preAlpha = 0.4f;
if (oldComposite instanceof AlphaComposite
        && ((AlphaComposite) oldComposite).getRule() == AlphaComposite.SRC_OVER) {
    preAlpha = Math.min(((AlphaComposite) oldComposite).getAlpha(), preAlpha);
g2d.setColor(c);
g2d.translate(tx, ty);
final int maxSize = isShadow ? size - 1 : size;
for (int i = -size; i <= maxSize; i++) {
    for (int j = -size; j <= maxSize; j++) {
        final double distance = i * i + j * j;
        float alpha = opacity;
        if (distance > 0.0d) {
            alpha = (float) (1.0f / (distance * size * opacity));
        alpha *= preAlpha;
        if (alpha > 1.0f) {
            alpha = 1.0f;
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
        g2d.drawString(s, i + size, j + size);
g2d.translate(-tx, -ty);
g2d.setComposite(oldComposite);
g2d.setColor(oldColor);
g2d.drawString(s, 0, 0);
BufferedImagepaintTexture(BufferedImage pattern, BufferedImage template)
paint Texture
BufferedImage repainted = new BufferedImage(template.getWidth(), template.getHeight(),
        BufferedImage.TYPE_INT_ARGB);
Graphics g = repainted.getGraphics();
int width = pattern.getWidth();
int height = pattern.getHeight();
for (int x = 0; x < template.getWidth(); x++)
    for (int y = 0; y < template.getHeight(); y++) {
        float alpha = ((float) (template.getRGB(x, y) >>> 24) / 255F);
...