Calculates the size of a ToggleButton. ToggleButton does not wrap unless indicated to do so w/ \n. - Java 2D Graphics

Java examples for 2D Graphics:Size

Description

Calculates the size of a ToggleButton. ToggleButton does not wrap unless indicated to do so w/ \n.

Demo Code

/**/* w w w .j a  v  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.AffineTransform;
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 ToggleButton.<br>
     * ToggleButton does not wrap unless indicated to do so w/ \n. Also if two
     * names are used the second name appears at an offset. And since this is a
     * togglebutton a box and possible check mark in the box are added
     * @param text is the text of the togglebutton
     * @param text2 is the second text of the toggle button - added on a
     *            secondline and indented
     * @param party is the party of the candidate in the toggle button - right
     *            aligned on first line of button
     * @param fontsize the size of the font
     * @param wrappingWidth width of the button
     * @param bold whether the button is bold
     * @return the size of the ToggleButton
     */
    public static Dimension getToggleButtonSize(String text, String text2,
            String party, int fontsize, int wrappingWidth, 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();

        int padding = 10;
        int heightPos = padding + baseline;
        int lineWidth = padding;

        int maxWidth = 0; // the max width of any line

        if (!text2.equals("")) {
            heightPos += lineHeight(text, font);
        }

        maxWidth = Math.max(lineWidth, maxWidth);

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

    /**
     * Calculates the line height at a given font, by looking at the height of
     * the first word
     * @param line is the line
     * @param font is the font
     * @return the height
     */
    private static int lineHeight(String line, Font font) {
        Rectangle2D measurement = font.getStringBounds(line + " ",
                new FontRenderContext(new AffineTransform(), true, true));
        return (int) measurement.getHeight();

    }
}

Related Tutorials