Java 8 FontMetrics - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 FontMetrics class.

This code example shows how to use the following methods:getAscent, getDescent, getHeight, stringWidth
    	 
    	int centerY = (int)paintArea.getCenterY();
    	int leftX = (int)paintArea.getMinX() + ((int)paintArea.getWidth() - Math.max(topPx, bottomPx)) / 2;
    	 
    	FontMetrics metrics = g2d.getFontMetrics(g2d.getFont());
    	int prongHeight = metrics.getHeight() + metrics.getDescent();
    	 
    	g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
 
    	// Creates a rectangle only if is defined, if not is "transparent" like Google Maps 
    	if (!this.transparent) {
    		Rectangle frame = new Rectangle(
    			leftX - 4, centerY - prongHeight - 4, 
    			Math.max(topPx, bottomPx) + 8, 8 + prongHeight * 2
    		); 
 
    		// fill the rectangle 
    		g2d.setColor(bgcolor);
    		g2d.fill(frame);
 

7
CodeRank
This code example shows how to use the following methods:getAscent, getDescent, getHeight, getLeading, getLineMetrics
    private int drawText(final Graphics g, final int top, final int size) {
        Font font = new Font("sansserif", 0, size);
        g.setFont(font);
        FontMetrics fm = g.getFontMetrics();
 
        int lineHeight = fm.getHeight();
        int baselineOffset = fm.getLeading() + fm.getAscent();
 
        g.setColor(Color.LIGHT_GRAY);
        g.drawRect(left - 10, top, width + 20, lineHeight - 1);
 
        g.setColor(Color.LIGHT_GRAY);
        g.drawLine(left - 10, top + baselineOffset, left + 10 + width, top + baselineOffset);
 
        g.setColor(Color.BLUE);
        g.drawLine(left, top + fm.getLeading(), left + width, top + fm.getLeading());
 
        g.setColor(Color.GREEN);
        g.drawLine(left, top + baselineOffset + fm.getDescent(), left + width, top + baselineOffset + fm.getDescent());
         

7
CodeRank
This code example shows how to use the following methods:getHeight, stringWidth
      textBox = new Rectangle();
      triangle = new Rectangle();
 
      font = new Font("sansserif", Font.BOLD, 14);
      fm = getFontMetrics(font);
      textBox.setSize(fm.stringWidth(text),fm.getHeight());
      textBox.grow(PADDING_X, PADDING_Y);
       
      setDirection(DIRECTION_EAST);
   } 
 
   public void setLocationRelativeToRegion(Region region, Layout side) {
      if (side == Layout.TOP){
         setDirection(DIRECTION_SOUTH);
      } else if (side == Layout.BOTTOM){
         setDirection(DIRECTION_NORTH);
      } else if (side == Layout.LEFT){
         setDirection(DIRECTION_EAST);
      } else if (side == Layout.RIGHT){
         setDirection(DIRECTION_WEST);
6
CodeRank
This code example shows how to use the following methods:getAscent, getHeight, stringWidth
		final int height = getHeight();
 
		// Get size of text 
		graphics.setFont(getFont());
		final FontMetrics fontMetrics = graphics.getFontMetrics();
		final int dxText = fontMetrics.stringWidth(label);
		final int dxMargin = 10;
 
		// Does text fit with a nice margin? 
		if (dxText > width - dxMargin)
		{ 
			// Re-render as a larger button 
			setWidth(dxText + dxMargin);
			return false; 
		} 
		else 
		{ 
			// Turn on anti-aliasing 
			graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);

6
CodeRank
This code example shows how to use the following methods:getHeight, stringWidth
			g2.drawLine(0, 0, getWidth(), getHeight());
			g2.drawLine(0, getHeight(), getWidth(), 0);
 
			String str = image == null ? "Connecting To Server" : "No Image";
			FontMetrics metrics = g2.getFontMetrics(getFont());
			int w = metrics.stringWidth(str);
			int h = metrics.getHeight();
 
			g2.setColor(Color.WHITE);
			g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
			g2.drawString(str, (getWidth() - w) / 2, cy - h);
			// w = metrics.stringWidth(str); 
			// h = metrics.getHeight(); 
			// g2.drawString(str, (getWidth() - w) / 2, cy - 2 * h); 
 
		} else { 
			// owner.getGraphics().drawImage(image, 0, 0, image.getWidth(), 
			// image.getHeight(), null); 
			// g2.clearRect(0, 0, image.getWidth(), image.getHeight()); 
			int width = image.getWidth();
6
CodeRank
This code example shows how to use the following methods:getDescent, getHeight, stringWidth
        //g.setFont(fnt); 
        final FontMetrics fm = g.getFontMetrics();
         
        // Using the font metrics, centre the text in the button face. 
        final int x = (int) ((buttonRect.width/2.0f)-
        		fm.stringWidth(button.getText())/2.0f);
        final int y = (int) ((buttonRect.height/2.0f) + 
                (fm.getHeight()-fm.getDescent())/2.0f);
         
        // If the button face is dark or does not contrast well with the  
        // black text turn the text white. 
        if (col.getValue() < 0.6 || (col.getHue() > 0.6 && 
           col.getSaturation() > 0.7) || greyedOut)
            g.setPaint(Color.white);
        else 
            g.setPaint(Color.black);
        g.drawString(button.getText(), x, y);
    } 
     
    /** 
6
CodeRank
This code example shows how to use the following methods:charWidth, getLeading, getMaxAscent, getMaxDescent
		g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, usesFractionalMetrics ? RenderingHints.VALUE_FRACTIONALMETRICS_ON : RenderingHints.VALUE_FRACTIONALMETRICS_OFF);
		g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
 
		// get character measurements 
		FontMetrics fm = g.getFontMetrics();
		int ascent = fm.getMaxAscent();
		int descent = fm.getMaxDescent();
		int advance = fm.charWidth('W'); // width of widest char, more reliable
											// than getMaxAdvance(); 
		int leading = fm.getLeading();
 
		// calculate size of 10x10 character grid 
		int fontHeight = ascent + descent + (leading / 2);
		int fontWidth = advance;
		int maxCharSize = Math.max(fontHeight, fontWidth);
		return maxCharSize;
	} 
 
	/** 
	 * Build the character set display list from the given texture. Creates one 
6
CodeRank
This code example shows how to use the following methods:getAscent
This code example shows how to use the following methods:getMaxAscent, getMaxDescent
     *          <code>availHeight</code>, <code>false</code> otherwise.  
     */ 
    private boolean isFontSuitable(int availHeight, Graphics2D g2D)
    { 
        FontMetrics fm = g2D.getFontMetrics(font);
        int maxH = fm.getMaxAscent()+fm.getMaxDescent();
        return (maxH <= availHeight);
    } 
     
    /** 
     * Sets {@link #font} to a derived font such that {@link #title} can be  
     * rendered on the given graphics context within the specified height. 
     *   
     * @param availHeight The available height in which {@link #title} should  
     *                    fit. 
     * @param g2D The graphics context in which {@link #title} will be rendered. 
     */ 
    private void ensureSuitableFont(int availHeight, Graphics2D g2D)
    { 
        float size = font.getSize2D();
5
CodeRank
This code example shows how to use the following methods:getHeight, stringWidth
See Code Examples for Java 8 FontMetrics Methods: