draw String In Box - Java java.awt

Java examples for java.awt:Graphics2D

Description

draw String In Box

Demo Code


import java.awt.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;

public class Main{
    public static final int TEXT_TOPLEFT = 0;
    public static final int TEXT_TOP = 1;
    public static final int TEXT_TOPRIGHT = 2;
    public static final int TEXT_LEFT = 3;
    public static final int TEXT_CENTER = 4;
    public static final int TEXT_RIGHT = 5;
    public static final int TEXT_BOTTOMLEFT = 6;
    public static final int TEXT_BOTTOM = 7;
    public static final int TEXT_BOTTOMRIGHT = 8;
    public static void drawStringInBox(String s, Font font, int xRef,
            int yRef, int width, int height, int aligned, Graphics context) {
        //int align determines the position of the string in the box.
        FontMetrics fm = context.getFontMetrics(font);
        Rectangle2D strBound = fm.getStringBounds(s, context);
        int xDraw, yDraw;
        if (aligned == TEXT_TOPLEFT) {
            xDraw = xRef;/*  www.j a va  2s.  c om*/
            yDraw = yRef + (int) strBound.getHeight();
        } else if (aligned == TEXT_TOP) {
            xDraw = xRef + width / 2 - (int) strBound.getWidth() / 2;
            yDraw = yRef + (int) strBound.getHeight();
        } else if (aligned == TEXT_TOPRIGHT) {
            xDraw = xRef + width - (int) strBound.getWidth();
            yDraw = yRef + (int) strBound.getHeight();
        } else if (aligned == TEXT_LEFT) {
            xDraw = xRef;
            yDraw = yRef + height / 2 + (int) strBound.getHeight() / 2;
        } else if (aligned == TEXT_CENTER) {
            xDraw = xRef + width / 2 - (int) strBound.getWidth() / 2;
            yDraw = yRef + height / 2 + (int) strBound.getHeight() / 2;
        } else if (aligned == TEXT_RIGHT) {
            xDraw = xRef + width - (int) strBound.getWidth();
            yDraw = yRef + height / 2 + (int) strBound.getHeight() / 2;
        } else if (aligned == TEXT_BOTTOMLEFT) {
            xDraw = xRef;
            yDraw = yRef + height;
        } else if (aligned == TEXT_BOTTOM) {
            xDraw = xRef + width / 2 - (int) strBound.getWidth() / 2;
            yDraw = yRef + height;
        } else if (aligned == TEXT_BOTTOMRIGHT) {
            xDraw = xRef + width - (int) strBound.getWidth();
            yDraw = yRef + height;
        } else {
            //default alignment: bottomleft
            xDraw = xRef;
            yDraw = yRef;
        }
        Font tmpFont = context.getFont();
        context.setFont(font);
        context.drawString(s, xDraw, yDraw);
        context.setFont(tmpFont); //return to original font
    }
    public static void drawStringInBox(String s, int size, int xRef,
            int yRef, int width, int height, int aligned, Graphics context) {
        drawStringInBox(s, DrawingUtility.getDefaultFont(size), xRef, yRef,
                width, height, aligned, context);
    }
    public static Font getDefaultFont(int size) {
        return new Font("Tahoma", Font.BOLD, size);
    }
}

Related Tutorials