Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;

public class Main {
    /**
     * computes the width of a string drawn on a canvas
     *
     * @param g the graphics object where to draw
     * @param f the font used to draw
     * @param strValue the string to draw
     * @return
     */
    public static int getStringWidth(Graphics g, Font f, String strValue) {

        // gets the metrics of the font
        FontMetrics fm = g.getFontMetrics(f);

        // and returns its width
        return (int) fm.getStringBounds(strValue, g).getWidth();
    }
}