A3Helper.java:


import java.awt.Rectangle;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author Mark S. Hancock [MSH]
 *
 * A3Helper provides class methods to help with Assignment #3.
 */

public final class A3Helper
{
    /**
     * A private contsructor to prevent anyone from trying to
     * create an instance of an A3Helper.
     */

    private A3Helper()
    {
    }

    /**
     * Finds the bounding box for a particular string and font. The resulting
     * rectangle has the following properties:
     *
     * rect.x - the x-offset to start drawing at if you want the string
     *          to appear at the left of the drawing window (x = 0).
     *          
     * rect.y - the y-offset to start drawing at if you want the string
     *          to appear at the top of the drawing window (y = 0).
     *          
     * rect.width - the total amount of horizontal space occupied by this
     *              string.
     *              
     * rect.height - the total amount of vertical space occupied by this
     *              string.
     *
     * @param fontname - the name of the font (e.g., "Times New Roman")
     * @param pointSize - the point size of the font
     * @param style - the style of the font (one of "PLAIN", "BOLD", or "ITALIC")
     * @param str - the string whose bounds should be returned
     * @return The bounding rectangle for this string if drawn with this font.
     */

    public static Rectangle getStringBounds(String fontname, int pointSize,
            String style, String str)
    {
        java.awt.Font font = new java.awt.Font(fontname, getFontStyle(style),
                pointSize);

        TextLayout layout = new TextLayout(str, font, new FontRenderContext(
                new AffineTransform(), false, false));

        Rectangle2D rect = layout.getBounds();

        Rectangle bounds = new Rectangle(-(int) rect.getX(),
                -(int) rect.getY(), (int) rect.getWidth(), (int) rect
                        .getHeight());

        return bounds;
    }

    /**
     * Retrieves the contents of a file as a list of strings.
     *
     * @param filename the name of the file to read.
     * @return a list of strings containing each word in the file.
     */

    public static List<String> getFile(String filename)
    {
        try
        {
            Scanner scanner = new Scanner(new File(filename));

            ArrayList<String> list = new ArrayList<String>();
            while (scanner.hasNext())
            {
                String nextWord = scanner.next();
                nextWord = nextWord.replaceAll("[^A-Za-z']", "");
                if (nextWord.length() > 0)
                {
                    list.add(nextWord);
                }
            }

            return list;
        }
        catch (Exception x)
        {
            return null;
        }
    }

    /**
     * Interpolates between minValue and maxValue for a specified frequency
     * and maxFrequency.
     *
     * @param minValue the minimum value of the result
     * @param maxValue the maximum value of the result
     * @param frequency the frequency to interpolate to
     * @param maxFrequency the maximum frequency for any word
     * @return the interpolated value.
     */

    public static float interpolate(float minValue, float maxValue,
            int frequency, int maxFrequency)
    {
        return minValue + ((float) frequency - 1) / ((float) maxFrequency - 1)
                * (maxValue - minValue);
    }

    /**
     * Converts a style from a string to an int (used by getStringBounds).
     *
     * @param style the style in String format.
     * @return the style in int format.
     */

    private static int getFontStyle(String style)
    {
        if (style.equalsIgnoreCase("PLAIN"))
        {
            return java.awt.Font.PLAIN;
        }
        else if (style.equalsIgnoreCase("BOLD"))
        {
            return java.awt.Font.BOLD;
        }
        else if (style.equalsIgnoreCase("ITALIC"))
        {
            return java.awt.Font.ITALIC;
        }

        return 0;
    }

    /**
     * This main function tests and demonstrates the reading of a file.
     * The program takes a single argument that is the name of a file to read.
     *
     * @param args the program's arguments.
     */

    public static void main(String[] args)
    {
        List<String> lines = A3Helper.getFile(args[0]);
        for (String line : lines)
        {
            System.out.println(line.trim());
        }
    }
}