Here you can find the source of drawLine(Graphics g, FontMetrics fm, Rectangle rect, String text, int hAlign, int y, int mnemonic)
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. |
public static int drawLine(Graphics g, FontMetrics fm, Rectangle rect, String text, int hAlign, int y, int mnemonic)
//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; } }