Java FontMetrics getConsoleFontMetrics(Font font)

Here you can find the source of getConsoleFontMetrics(Font font)

Description

Returns metrics of given font.

License

Open Source License

Parameter

Parameter Description
font a parameter

Return

font metrics object

Declaration

public static FontMetrics getConsoleFontMetrics(Font font) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

public class Main {
    /**Returns metrics of given font.
     * @param font//from   ww  w  . j  a v  a2s.  c  om
     * @return font metrics object
     */
    public static FontMetrics getConsoleFontMetrics(Font font) {
        BufferedImage image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
        Graphics graphics = image.getGraphics();

        Graphics2D g = (Graphics2D) graphics;

        g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);

        g.setFont(font);
        FontMetrics metrics = g.getFontMetrics();
        return metrics;
    }
}

Related

  1. cutString(String text, FontMetrics fontMetrics, int maxWidth)
  2. fitString(String s, Graphics2D g2, Rectangle2D rect)
  3. getAllStylesSameWidthsFontsFamillyName()
  4. getCenterOffset(Graphics g, Font f, char ch)
  5. getComponentAverageCharacterWidth(Component component)
  6. getLineBreakIndex(Graphics g, String text, int index, double maxWidth)
  7. getLineHeight(Component c, int defaultHeight)
  8. getLineHeight(Component c, int defaultHeight)
  9. getLineHeight(Component component, int count)