Calculates the size of a button. Note: Buttons do not automatically wrap - must be wrapped w/ a \n in the text of the button - Java 2D Graphics

Java examples for 2D Graphics:Size

Description

Calculates the size of a button. Note: Buttons do not automatically wrap - must be wrapped w/ a \n in the text of the button

Demo Code

/**/*w w w  .j  av a  2  s .  c  om*/
 * This file is part of VoteBox.
 * 
 * VoteBox is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as published by
 * the Free Software Foundation.
 * 
 * You should have received a copy of the GNU General Public License
 * along with VoteBox, found in the root of any distribution or
 * repository containing all or part of VoteBox.
 * 
 * THIS SOFTWARE IS PROVIDED BY WILLIAM MARSH RICE UNIVERSITY, HOUSTON,
 * TX AND IS PROVIDED 'AS IS' AND WITHOUT ANY EXPRESS, IMPLIED OR
 * STATUTORY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, WARRANTIES OF
 * ACCURACY, COMPLETENESS, AND NONINFRINGEMENT.  THE SOFTWARE USER SHALL
 * INDEMNIFY, DEFEND AND HOLD HARMLESS RICE UNIVERSITY AND ITS FACULTY,
 * STAFF AND STUDENTS FROM ANY AND ALL CLAIMS, ACTIONS, DAMAGES, LOSSES,
 * LIABILITIES, COSTS AND EXPENSES, INCLUDING ATTORNEYS' FEES AND COURT
 * COSTS, DIRECTLY OR INDIRECTLY ARISING OUR OF OR IN CONNECTION WITH
 * ACCESS OR USE OF THE SOFTWARE.
 */
//package com.java2s;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.font.FontRenderContext;

import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

public class Main {
    /**
     * The standard font to use
     */
    public static final String FONT_NAME = "Lucida Sans";
    /**
     * A dummy 1x1 image used for getting the sizes of components
     */
    private static final BufferedImage DUMMY_IMAGE = new BufferedImage(1,
            1, BufferedImage.TYPE_INT_ARGB);

    /**
     * Calculates the size of a button.<br>
     * Note: Buttons do not automatically wrap - must be wrapped w/ a \n in the
     * text of the button
     * @param text the text of the button
     * @param fontsize the size of the font to use
     * @param bold whether the button has bold text
     * @return the size of the Button
     */
    public static Dimension getButtonSize(String text, int fontsize,
            boolean bold) {

        Font font = new Font(FONT_NAME, (bold) ? Font.BOLD : Font.PLAIN,
                fontsize);

        BufferedImage wrappedImage = DUMMY_IMAGE;

        Graphics2D graphs = wrappedImage.createGraphics();
        graphs.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        graphs.setFont(font);

        int baseline = graphs.getFontMetrics().getAscent();

        String[] words = text.split(" ");
        int padding = 10;
        int heightPos = padding + baseline;
        int lineWidth = padding;

        int maxWidth = 0; // the max width of any line
        for (String word : words) // For each word try placing it on the line,
        // if not jump down a line and then write it
        {
            Rectangle2D measurement = font.getStringBounds(word + " ",
                    new FontRenderContext(null, true, true));
            int wordWidth = (int) measurement.getWidth();
            int wordHeight = (int) measurement.getHeight();
            lineWidth += wordWidth;

            if (word.equals("\n")) {
                maxWidth = Math.max(lineWidth, maxWidth);
                heightPos += wordHeight;
                lineWidth = padding;
            }
        }
        maxWidth = Math.max(lineWidth, maxWidth);

        return new Dimension(maxWidth, heightPos + padding);
    }
}

Related Tutorials