Java Code Examples for java.awt.FontMetrics#getLineMetrics()

The following examples show how to use java.awt.FontMetrics#getLineMetrics() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TextFragment.java    From ccu-historian with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Calculates the vertical offset between the baseline and the specified 
 * text anchor.
 * 
 * @param g2  the graphics device.
 * @param anchor  the anchor.
 * 
 * @return the offset.
 */
public float calculateBaselineOffset(Graphics2D g2, TextAnchor anchor) {
    float result = 0.0f;
    final FontMetrics fm = g2.getFontMetrics(this.font);
    final LineMetrics lm = fm.getLineMetrics("ABCxyz", g2);
    if (anchor.isTop()) {
        result = lm.getAscent();
    }
    else if (anchor.isHalfAscent()) {
        result = lm.getAscent() / 2.0f;
    }
    else if (anchor.isVerticalCenter()) {
        result = lm.getAscent() / 2.0f - lm.getDescent() / 2.0f;
    }
    else if (anchor.isBottom()) {
        result = -lm.getDescent() - lm.getLeading();
    }
    return result;                                             
}
 
Example 2
Source File: TextFragment.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Calculates the vertical offset between the baseline and the specified
 * text anchor.
 *
 * @param g2  the graphics device.
 * @param anchor  the anchor.
 *
 * @return the offset.
 */
public float calculateBaselineOffset(Graphics2D g2, TextAnchor anchor) {
    float result = 0.0f;
    FontMetrics fm = g2.getFontMetrics(this.font);
    LineMetrics lm = fm.getLineMetrics("ABCxyz", g2);
    if (anchor == TextAnchor.TOP_LEFT || anchor == TextAnchor.TOP_CENTER
                                      || anchor == TextAnchor.TOP_RIGHT) {
        result = lm.getAscent();
    }
    else if (anchor == TextAnchor.BOTTOM_LEFT
            || anchor == TextAnchor.BOTTOM_CENTER
            || anchor == TextAnchor.BOTTOM_RIGHT) {
        result = -lm.getDescent() - lm.getLeading();
    }
    return result;
}
 
Example 3
Source File: TextFragment.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Calculates the vertical offset between the baseline and the specified 
 * text anchor.
 * 
 * @param g2  the graphics device.
 * @param anchor  the anchor.
 * 
 * @return the offset.
 */
public float calculateBaselineOffset(Graphics2D g2, TextAnchor anchor) {
    float result = 0.0f;
    FontMetrics fm = g2.getFontMetrics(this.font);
    LineMetrics lm = fm.getLineMetrics("ABCxyz", g2);
    if (anchor == TextAnchor.TOP_LEFT || anchor == TextAnchor.TOP_CENTER
                                      || anchor == TextAnchor.TOP_RIGHT) {
        result = lm.getAscent();
    }
    else if (anchor == TextAnchor.BOTTOM_LEFT 
            || anchor == TextAnchor.BOTTOM_CENTER
            || anchor == TextAnchor.BOTTOM_RIGHT) {
        result = -lm.getDescent() - lm.getLeading();
    }
    return result;                                             
}
 
Example 4
Source File: CircleProgressBar.java    From xdm with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void paint(Graphics g) {
	Graphics2D g2 = (Graphics2D) g;
	if (g2 == null) {
		return;
	}
	g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
			RenderingHints.VALUE_ANTIALIAS_ON);
	// g2.setRenderingHint(RenderingHints.KEY_RENDERING,
	// RenderingHints.VALUE_RENDER_QUALITY);

	int sweep_angle = (int)(((float)value * 360) / 100);
	g2.setColor(Color.GRAY);
	g2.setStroke(stroke);
	g2.drawArc(padding, padding, getWidth() - 2 * padding, getHeight() - 2
			* padding, getScaledInt(90), -360);
	// g2.drawArc(2, 2, getWidth() - 12, getHeight() - 12, 90, -360);
	if (value > 0) {
		g2.setColor(foreColor);
		// g2.drawArc(2, 2, getWidth() - 12, getHeight() - 12, 90,
		// -sweep_angle);
		g2.drawArc(padding, padding, getWidth() - 2 * padding, getHeight()
				- 2 * padding, getScaledInt(90), -sweep_angle);
	}

	g2.setFont(FontResource.getItemFont());
	FontMetrics fm = g2.getFontMetrics();
	String str = value + "%";
	int w = (int) fm.getStringBounds(str, g2).getWidth();// fm.stringWidth(str);
	LineMetrics lm = fm.getLineMetrics(str, g2);
	int h = (int) (lm.getAscent() + lm.getDescent());
	g2.drawString(str, (getWidth()  - w) / 2,
			((getHeight()   + h) / 2) - lm.getDescent());
}
 
Example 5
Source File: GameTreeNode.java    From FancyBing with GNU General Public License v3.0 5 votes vote down vote up
private void drawText(Graphics graphics)
{
    GameTreePanel.Label labelMode = m_gameTreePanel.getLabelMode();
    if (labelMode == GameTreePanel.Label.NONE)
        return;
    Move move = m_node.getMove();
    int size = m_gameTreePanel.getNodeSize();
    String text;
    if (labelMode == GameTreePanel.Label.MOVE)
    {
        if (move.getPoint() == null)
            return;
        text = move.getPoint().toString();
    }
    else
        text = Integer.toString(m_moveNumber);
    FontMetrics fontMetrics = graphics.getFontMetrics();
    LineMetrics lineMetrics = fontMetrics.getLineMetrics(text, graphics);
    int textWidth = fontMetrics.stringWidth(text);
    int ascent = (int)lineMetrics.getAscent();
    int xText = (size - textWidth) / 2;
    int yText = (ascent + size) / 2;
    if (move.getColor() == BLACK)
        graphics.setColor(Color.white);
    else
        graphics.setColor(Color.black);
    graphics.drawString(text, xText, yText);
}
 
Example 6
Source File: TextSprite.java    From stendhal with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a new <code>TextSprite</code>
 *
 * @param text The text to be rendered
 * @param textColor Color of the text
 * @return TextSprite with the wanted text
 */
public static TextSprite createTextSprite(String text, final Color textColor) {
	final GraphicsConfiguration gc = getGC();
	FontMetrics metrics = graphics.getFontMetrics();
	LineMetrics lm = metrics.getLineMetrics(text, graphics);
	final Image image = gc.createCompatibleImage(metrics.stringWidth(text)
			+ 2, Math.round(lm.getHeight()) + 2, TransparencyMode.TRANSPARENCY);

	drawOutlineString(image, textColor, text, 1, Math.round(lm.getAscent()));

	return new TextSprite(image);
}
 
Example 7
Source File: Field.java    From FancyBing with GNU General Public License v3.0 4 votes vote down vote up
private void drawLabel(int fieldX, int fieldY, Graphics boardGraphics,
                       Image boardImage, int boardWidth)
{
    setComposite(COMPOSITE_97);
    setFont(m_graphics, m_size);
    FontMetrics fontMetrics = m_graphics.getFontMetrics();
    LineMetrics lineMetrics =
        fontMetrics.getLineMetrics(m_label, m_graphics);
    int width = fontMetrics.stringWidth(m_label);
    int height = fontMetrics.getHeight();
    int ascent = (int)lineMetrics.getAscent();
    int x = Math.max((m_size - width) / 2, 0);
    int y = (ascent + m_size) / 2;
    if (m_ghostStone == null)
    {
        if (m_color == BLACK)
            m_graphics.setColor(Color.white);
        else
            m_graphics.setColor(Color.black);
    }
    else
    {
        if (m_ghostStone == BLACK)
            m_graphics.setColor(Color.white);
        else
            m_graphics.setColor(Color.black);
    }
    Rectangle oldClip = m_graphics.getClipBounds();
    width = Math.min(width, (int)(0.95 * m_size));
    m_graphics.setClip(x, y - ascent, width, height);
    if (m_color == EMPTY && m_ghostStone == null)
    {
        Rectangle oldBoardClip = boardGraphics.getClipBounds();
        boardGraphics.setClip(fieldX + x, fieldY + y - ascent,
                              width, height);
        boardGraphics.drawImage(boardImage, 0, 0, boardWidth, boardWidth,
                                null);
        if (m_fieldColor != null)
            drawFieldColor();
        boardGraphics.setClip(oldBoardClip);
        m_graphics.setColor(Color.black);
    }
    m_graphics.drawString(m_label, x, y);
    m_graphics.setClip(oldClip);
}