Java Swing Font Metrics drawLine(Graphics g, FontMetrics fm, Rectangle rect, String text, int hAlign, int y, int mnemonic)

Here you can find the source of drawLine(Graphics g, FontMetrics fm, Rectangle rect, String text, int hAlign, int y, int mnemonic)

Description

Draws one of the lines on a multiline label/button.

License

Open Source License

Parameter

Parameter Description
g the Graphics context of the component.
fm the font metrics.
rect the rectangle that the text needs to be painted in.
text the text that needs to be drawn.
hAlign the horizontal alignment.
y the y position of the text.
mnemonic the mnemonic.

Return

the x starting position of the text

Declaration

public static int drawLine(Graphics g, FontMetrics fm, Rectangle rect,
        String text, int hAlign, int y, int mnemonic) 

Method Source Code

//package com.java2s;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.FontMetrics;

import javax.swing.SwingUtilities;
import javax.swing.SwingConstants;

import javax.swing.plaf.basic.BasicGraphicsUtils;

public class Main {
    /**//w  w w. ja va2 s .c o  m
     * Draws one of the lines on a multiline label/button.
     *
     * @param g the Graphics context of the component.
     * @param fm the font metrics.
     * @param rect the rectangle that the text needs to be painted in.
     * @param text the text that needs to be drawn.
     * @param hAlign the horizontal alignment.
     * @param y the y position of the text.
     * @param mnemonic the mnemonic.
     *
     * @return the x starting position of the text
     */
    public static int drawLine(Graphics g, FontMetrics fm, Rectangle rect,
            String text, int hAlign, int y, int mnemonic) {
        int x = rect.x;

        if (text != null) {
            int width = SwingUtilities.computeStringWidth(fm, text);

            if (hAlign == SwingConstants.CENTER) {
                x = rect.x + ((rect.width - width) / 2);
            } else if (hAlign == SwingConstants.RIGHT) {
                x = rect.x + (rect.width - width);
            }

            BasicGraphicsUtils.drawString(g, text, mnemonic, x, y);
        }

        return x;
    }
}

Related

  1. clipString(JComponent c, FontMetrics fm, String string, int availTextWidth)
  2. clipStringIfNecessary(JComponent c, FontMetrics fm, String string, int availTextWidth)
  3. clipText(FontMetrics fm, String val, int width)
  4. computeMultiLineStringDimension(FontMetrics oFontMetrics, String[] astr)
  5. computeStringWidth(FontMetrics fontMetrics, String str)
  6. getBreakLocation(Segment s, FontMetrics metrics, int x0, int x, TabExpander e, int startOffset)
  7. getClippedText(String text, FontMetrics fm, int maxWidth)
  8. getClippedText(String text, FontMetrics fm, int maxWidth)
  9. getClippedText(String text, FontMetrics fm, int maxWidth)