Java examples for 2D Graphics:Size
Calculates the line width at a given font
/**/*from w ww.j a v a2 s.c o m*/ * 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.Font; import java.awt.font.FontRenderContext; import java.awt.geom.AffineTransform; import java.awt.geom.Rectangle2D; public class Main { /** * Calculates the line width at a given font * @param line is the line * @param font is the font * @return the width */ private static int lineWidth(String[] line, Font font) { int width = 0; for (String word : line) { Rectangle2D measurement = font .getStringBounds(word + " ", new FontRenderContext( new AffineTransform(), true, true)); width += measurement.getWidth(); } return width; } }