Java 8 Font - 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 Font class.

This code example shows how to use the following methods:getFamily, getSize, isBold, isItalic
        final Font font = (Font) object;
        final AttributeList attribs = new AttributeList(); 
        if (mPlexAttribute != null) {
            attribs.setAttribute(mPlexAttribute, mPlexValue);
        } 
        attribs.setAttribute("family", font.getFamily());
        attribs.setAttribute("size", String.valueOf(font.getSize()));
        attribs.setAttribute("style", String.valueOf(getFontStyle(font)));
        writer.writeTag(tagName, attribs, true);
    } 
 
    private String getFontStyle(final Font font) {
        if (font.isBold() && font.isItalic()) {
            return "bold-italic"; 
        } 
        if (font.isBold()) {
            return "bold"; 
        } 
        if (font.isItalic()) {
            return "italic"; 

7
CodeRank
This code example shows how to use the following methods:getFontName, getName
            canDisplayLocalizedText(tahoma, Locale.SIMPLIFIED_CHINESE, false);
            canDisplayLocalizedText(tahoma, Locale.TRADITIONAL_CHINESE, false);
            canDisplayLocalizedText(tahoma, Locale.JAPANESE, false);
            canDisplayLocalizedText(tahoma, Locale.KOREAN,   false);
        } 
        Font msSansSerif = new Font("Microsoft Sans Serif", Font.PLAIN, 12);
        if (msSansSerif.getName().equals(msSansSerif.getFontName())) {
            canDisplayLocalizedText(msSansSerif, Locale.ENGLISH, true);
            canDisplayLocalizedText(msSansSerif, Locale.GERMAN,  true);
            canDisplayLocalizedText(msSansSerif, Locale.FRENCH,  true);
            canDisplayLocalizedText(msSansSerif, Locale.ITALIAN, true);
            canDisplayLocalizedText(msSansSerif, Locale.CHINESE, false);
            canDisplayLocalizedText(msSansSerif, Locale.SIMPLIFIED_CHINESE, false);
            canDisplayLocalizedText(msSansSerif, Locale.TRADITIONAL_CHINESE, false);
            canDisplayLocalizedText(msSansSerif, Locale.JAPANESE, false);
            canDisplayLocalizedText(msSansSerif, Locale.KOREAN,   false);
        } 
    } 
 
 

6
CodeRank
This code example shows how to use the following methods:getName, getSize, getStyle
        int size, style;
        if ( frac < 0.5 ) {
            name  = f1.getName();
            style = f1.getStyle();
        } else { 
            name  = f2.getName();
            style = f2.getStyle();
        } 
        size = (int)Math.round(frac*f2.getSize()+(1-frac)*f1.getSize());
        return getFont(name,style,size);
    } 
     
} // end of class FontLib 
6
CodeRank
This code example shows how to use the following methods:getFamily, getSize, isBold, isItalic
@PropertyEditorFor(Font.class)
public class FontPropertyEditor extends AbstractPropertyEditor { 
    public String getAsText() {
        if (null == getValue()) return null; 
        Font font = (Font) getValue();
        return font.getFamily() + "-" + formatStyle(font) + "-" + font.getSize();
    } 
 
    private String formatStyle(Font font) {
        if (font.isBold() && font.isItalic()) {
            return "BOLDITALIC"; 
        } else if (font.isBold()) {
            return "BOLD"; 
        } else if (font.isItalic()) {
            return "ITALIC"; 
        } 
        return "PLAIN"; 
    } 
 
    protected void setValueInternal(Object value) { 
6
CodeRank
This code example shows how to use the following methods:equals, getFamily, getSize
	public Font getStyledFont(Font font)
	{ 
		if(font == null)
			throw new NullPointerException("font param must not"
				+ " be null"); 
		if(font.equals(lastFont))
			return lastStyledFont;
		lastFont = font;
		lastStyledFont = new Font(font.getFamily(),
			(bold ? Font.BOLD : 0)
			| (italic ? Font.ITALIC : 0),
			font.getSize());
		return lastStyledFont;
	} 
 
	/** 
	 * Returns the font metrics for the styled font. 
	 */ 
	@SuppressWarnings("deprecation") 
	public FontMetrics getFontMetrics(Font font)
6
CodeRank
This code example shows how to use the following methods:
6
CodeRank
This code example shows how to use the following methods:getName, isBold, isItalic, isPlain
				} 
			} 
		} 
 
		// Grab the base name of the font they've asked about 
		String fontName = font.getName();
 
		// Some fonts support plain/bold/italic/bolditalic variants 
		// Others have different font instances for bold etc 
		// (eg font.dialog.plain.* vs font.Californian FB Bold.*) 
		String fontStyle = "";
		if (font.isPlain())
			fontStyle += "plain";
		if (font.isBold())
			fontStyle += "bold";
		if (font.isItalic())
			fontStyle += "italic";
 
		// Do we have a definition for this font with just the name? 
		// If not, check with the font style added 
		if (fontMetricsProps.get(FontDetails.buildFontHeightProperty(fontName)) == null
6
CodeRank
This code example shows how to use the following methods:canDisplayUpTo, getSize, getStyle
                "CheckBoxMenuItem.font", "CheckBoxMenuItem.acceleratorFont", 
                "RadioButtonMenuItem.font", 
                "RadioButtonMenuItem.acceleratorFont", "Spinner.font", 
                "Tree.font", "ToolBar.font", "OptionPane.messageFont", 
                "OptionPane.buttonFont", "ToolTip.font", }; 
 
        boolean displayable = false;
        for (int i = 0; i < comps.length; i++)
            displayable |= checkFont(comps[i], newFont, testString, false);
 
        // Then do it the automagic way. 
        // note that this could work all the time (without requiring the above) 
        // if Java 1.4 didn't introduce Locales, and it could even still work 
        // if they offered a way to get all the keys of possible resources. 
        for (Map.Entry<Object, Object> next : UIManager.getDefaults().entrySet()) {
            if (next.getValue() instanceof Font) {
                Font f = (Font) next.getValue();
                if (f != null && !newFont.equalsIgnoreCase(f.getName())) {
                    if (!FontUtils.canDisplay(f, testString)) {
                        f = new Font(newFont, f.getStyle(), f.getSize());

6
CodeRank
This code example shows how to use the following methods:
		drawRect(g2, w, h);
		drawRect(g2, w, h);
		drawRect(g2, w, h);
		drawRect(g2, w, h);
 
		Font font = new Font("sans-serif", Font.BOLD, 16);
 
		g2.setFont(font);
 
		FontMetrics metrics = g2.getFontMetrics(font);
		int sw = (w - metrics.stringWidth(s)) / 2;
		int sh = (h - metrics.getHeight()) / 2 + metrics.getHeight() / 2;
 
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2.setColor(Color.BLACK);
		g2.drawString(s, sw + 1, sh + 1);
		g2.setColor(Color.WHITE);
		g2.drawString(s, sw, sh);
 
		g2.dispose();
6
CodeRank
This code example shows how to use the following methods:getLineMetrics
     * @param text  the text. 
     */ 
    private void drawStringInRect(Graphics2D g2, Rectangle2D bounds, Font font,
                                  String text) {
 
        g2.setFont(font);
        FontMetrics fm = g2.getFontMetrics(font);
        Rectangle2D r = TextUtilities.getTextBounds(text, g2, fm);
        double x = bounds.getX();
        if (r.getWidth() < bounds.getWidth()) {
            x = x + (bounds.getWidth() - r.getWidth()) / 2;
        } 
        LineMetrics metrics = font.getLineMetrics(
            text, g2.getFontRenderContext()
        ); 
        g2.drawString(
            text, (float) x, (float) (bounds.getMaxY()
                - this.bottomInnerGap - metrics.getDescent())
        ); 
    } 
6
CodeRank
See Code Examples for Java 8 Font Methods: