Example usage for java.awt FontMetrics getHeight

List of usage examples for java.awt FontMetrics getHeight

Introduction

In this page you can find the example usage for java.awt FontMetrics getHeight.

Prototype

public int getHeight() 

Source Link

Document

Gets the standard height of a line of text in this font.

Usage

From source file:SplashScreenDemo.java

public static void main(String[] args) {
    SplashScreen splashScreen = SplashScreen.getSplashScreen();
    Dimension size = splashScreen.getSize();
    int borderDim = (int) (size.height * 0.05);
    Graphics g = splashScreen.createGraphics();
    g.setColor(Color.blue);// w w w  .ja v a 2  s . co m
    for (int i = 0; i < borderDim; i++)
        g.drawRect(i, i, size.width - 1 - i * 2, size.height - 1 - i * 2);

    FontMetrics fm = g.getFontMetrics();
    int sWidth = fm.stringWidth("Initializing...");
    int sHeight = fm.getHeight();
    if (sWidth < size.width && 2 * sHeight < size.height) {
        g.setColor(Color.blue);
        g.drawString("Initializing...", (size.width - sWidth) / 2, size.height - 2 * sHeight);
    }

    splashScreen.update();

    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String text = "java2s.com";

    BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = img.createGraphics();
    Font font = new Font("Arial", Font.PLAIN, 48);
    g2d.setFont(font);/*from  ww w. j  a  v  a  2s . co  m*/
    FontMetrics fm = g2d.getFontMetrics();
    int width = fm.stringWidth(text);
    int height = fm.getHeight();
    g2d.dispose();

    img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    g2d = img.createGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
            RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
    g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
    g2d.setFont(font);
    fm = g2d.getFontMetrics();
    g2d.setColor(Color.BLACK);
    g2d.drawString(text, 0, fm.getAscent());
    g2d.dispose();

    ImageIO.write(img, "png", new File("Text.png"));

}

From source file:Main.java

public static void main(String[] args) {
    final JTextPane textPane = new JTextPane();
    final JScrollPane scrollPane = new JScrollPane(textPane);

    String text = "Lorem ipsum dolor sit amet, " + "consectetur adipiscing elit."
            + "Fusce nec sapien id diam consequat adipiscing.";
    textPane.setText(text);/*from   w  w w.ja  va 2 s  . com*/

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(scrollPane);

    frame.setSize(new Dimension(200, 200));
    frame.setVisible(true);

    FontMetrics metrics = textPane.getFontMetrics(textPane.getFont());
    textPane.setMargin(new Insets(scrollPane.getViewport().getHeight() - metrics.getHeight(), 0, 0, 0));
}

From source file:Main.java

/**
 * "Fixates" the preferred width of the given label to the given text.
 * //from w w  w.j  av a2s  . c o  m
 * @param aLabel
 *          the label to fixate, cannot be <code>null</code>;
 * @param aMinimalText
 *          the text to use as minimal width indicator.
 */
public static final void fixLabelWidth(final JLabel aLabel, final String aMinimalText) {
    final FontMetrics fm = aLabel.getFontMetrics(aLabel.getFont());
    final int height = fm.getHeight();

    aLabel.setPreferredSize(new Dimension(fm.stringWidth(aMinimalText), height));
}

From source file:Main.java

public static Dimension getStringSize(JComponent c, FontMetrics metrics, String text) {
    int width = SwingUtilities2.stringWidth(c, metrics, text);
    int height = metrics.getHeight();
    return new Dimension(width, height);
}

From source file:Main.java

/**
 * Returns the height of the given component.
 *
 * @param c the component where the text is contained
 * @return the height of the text//from  w  w  w .  j a  v a 2  s  .co  m
 */
public static int getStringHeight(Component c) {
    // get metrics from the graphics
    FontMetrics metrics = c.getFontMetrics(c.getFont());
    // get the height of a line of text in this font and render context
    int hgt = metrics.getHeight();
    // calculate the height of a box to hold the text with some padding.
    return hgt + 2;
}

From source file:Main.java

/**
 * Returns the size of the given text computed towards to the given
 * component./*from w  w w . j  a v  a 2 s  .c o  m*/
 *
 * @param c the component where the text is contained
 * @param text the text to measure
 * @return the dimensions of the text
 */
public static Dimension getStringSize(Component c, String text) {
    // get metrics from the graphics
    FontMetrics metrics = c.getFontMetrics(c.getFont());
    // get the height of a line of text in this font and render context
    int hgt = metrics.getHeight();
    // get the advance of my text in this font and render context
    int adv = metrics.stringWidth(text);
    // calculate the size of a box to hold the text with some padding.
    return new Dimension(adv + 2, hgt + 2);
}

From source file:Main.java

public static Dimension getTextDimension(final String text, final Font font) {
    if (defaultLabel == null) {
        defaultLabel = new JLabel();
    }/*from  w  ww.  j  a  v  a  2  s.  com*/

    // get metrics from the graphics
    FontMetrics fontMetrics = defaultLabel.getFontMetrics(font);

    // get the height of a line of text in this
    // font and render context
    int hgt = fontMetrics.getHeight();
    // get the advance of my text in this font
    // and render context
    int adv = fontMetrics.stringWidth(text);
    // calculate the size of a box to hold the text
    Dimension size = new Dimension(adv, hgt);

    return size;
}

From source file:org.mili.core.graphics.GraphicsUtil.java

/**
 * Prints a character image in defined font in a file.
 *
 * @param dir directory./*from   w ww.  j a v a2 s .  c o m*/
 * @param fn filename.
 * @param font font.
 * @param c character to print in file.
 * @param mx character size x.
 * @param my character size y.
 * @throws IOException if io exception occurs.
 */
public static void writeChar(File dir, String fn, Font font, char c, int mx, int my) throws IOException {
    BufferedImage bi = new BufferedImage(mx, my, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = (Graphics2D) bi.createGraphics();
    g.setFont(font);
    FontMetrics fm = g.getFontMetrics();
    int fh = fm.getHeight();
    int cw = fm.charWidth(c);
    int asc = g.getFontMetrics().getAscent();
    int x0 = mx / 2 - cw / 2;
    int y0 = my / 2 - fh / 2 + asc;
    g.drawString(String.valueOf(c), x0, y0);
    g.dispose();
    File f = new File(dir, fn);
    GraphicsUtil.writeImage(f, bi);
}

From source file:org.drugis.addis.gui.wizard.TreatmentCategorizationOverviewWizardStep.java

public static VisualizationViewer<DecisionTreeNode, DecisionTreeEdge> buildDecisionTreeView(
        final DecisionTree tree) {
    // Crazy hack because sizes start at 600x600 by default.
    final Layout<DecisionTreeNode, DecisionTreeEdge> layout = new TreeLayout<DecisionTreeNode, DecisionTreeEdge>(
            new DecisionTree(new LeafNode()), 150, 75);
    layout.getSize().height = 1;/*  w ww  .  j  a v  a2s.  c  o m*/
    layout.getSize().width = 1;
    layout.setGraph(tree);

    final VisualizationViewer<DecisionTreeNode, DecisionTreeEdge> vv = new VisualizationViewer<DecisionTreeNode, DecisionTreeEdge>(
            layout);

    vv.setVertexToolTipTransformer(new ToStringLabeller<DecisionTreeNode>());
    vv.getRenderContext().setVertexFillPaintTransformer(new Transformer<DecisionTreeNode, Paint>() {
        public Paint transform(final DecisionTreeNode node) {
            return (node instanceof LeafNode) ? new Color(0.55f, 0.55f, 1.0f) : Color.ORANGE;
        }
    });

    vv.getRenderContext().setVertexShapeTransformer(new Transformer<DecisionTreeNode, Shape>() {
        public Shape transform(final DecisionTreeNode input) {
            final FontMetrics fontMetrics = vv.getGraphics().getFontMetrics();
            final double width = fontMetrics.stringWidth(input.toString()) + 6;
            final double height = fontMetrics.getHeight() + 2;
            final double arc = 5;
            return new RoundRectangle2D.Double(-width / 2, -height / 2, width, height, arc, arc);
        }
    });

    vv.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);
    vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<DecisionTreeNode>());

    vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller<DecisionTreeEdge>());
    vv.getRenderContext().getEdgeLabelRenderer().setRotateEdgeLabels(false);
    vv.getRenderContext().setEdgeLabelClosenessTransformer(
            new ConstantDirectionalEdgeValueTransformer<DecisionTreeNode, DecisionTreeEdge>(0.5, 0.4));
    return vv;
}