Jay
Jay

Reputation: 2454

Why does Font load differently on different jres?

I am trying to load a font to a label/button text on a swing app written in java 1.4. For some reason, the font loads without exception, but displays a completely diff font on the button. When I run the same code on 1.5 jre, it appears to work ok. Why does this happen?

UPDATE Printing a sysout on 1.4, 1.5 shows this:

jre 1.5 : java.awt.Font[family=Futura LT,name=Futura LT Medium,style=bold,size=14]

jre 1.4 : java.awt.Font[family=dialog,name=Futura LT Medium,style=bold,size=14]

The font name family is different! Why is that?

Below is an image to how it looks on diff jres (left is 1.5, right is 1.4)

enter image description here

Code

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.FontFormatException;
import java.io.IOException;
import java.io.InputStream;

import javax.swing.JButton;
import javax.swing.SwingUtilities;

import org.jdesktop.swingx.MultiSplitLayout;
import org.jdesktop.swingx.MultiSplitPane;
import org.jdesktop.swingx.MultiSplitLayout.Node;



/**
 * 
 * @author Hans Muller ([email protected])
 */
public class Example2 extends Example {
    protected void initialize(String[] ignore) {
        super.initialize(ignore);

        String layoutDef = "(COLUMN (LEAF name=column1 weight=0.25) (LEAF name=column2 weight=0.25) (LEAF name=column3 weight=0.25) (LEAF name=column4 weight=0.25) )";
        Node modelRoot = MultiSplitLayout.parseModel(layoutDef);

        MultiSplitPane multiSplitPane = new MultiSplitPane();
        multiSplitPane.setDividerSize(5);
        multiSplitPane.getMultiSplitLayout().setModel(modelRoot);
        JButton comp = new JButton("TEST TEXT");
        comp.setFont(loadFont("FuturaLT.ttf",14,Font.BOLD));
        multiSplitPane.add(comp, "column1");
        multiSplitPane.add(new JButton("Test Text"), "column2");
        Container cp = mainFrame.getContentPane();
        cp.add(multiSplitPane, BorderLayout.CENTER);
    }

    private static Font loadFont(String fontName, float size, int style) {

        InputStream openStream = FontColorScheme.class
                .getResourceAsStream("resources/fonts/FuturaLT/"
                        + fontName);
        ;
        try {
            Font font = Font.createFont(Font.TRUETYPE_FONT, openStream);
            Font finalFont = font.deriveFont((float) size).deriveFont(style);
            System.out.println("Loading font " + fontName + " " + finalFont);
            return finalFont;
        } catch (FontFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (openStream != null) {
                try {
                    openStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    public static void main(final String[] args) {
        System.setProperty("awt.useSystemAAFontSettings", "off");
        Runnable doCreateAndShowGUI = new Runnable() {
            public void run() {
                try {
                    Example2 app = new Example2();
                    app.initialize(args);
                    app.show();
                } catch (Exception e) {
                    // TBD log an error
                }
            }
        };
        SwingUtilities.invokeLater(doCreateAndShowGUI);
    }
}

Upvotes: 1

Views: 1219

Answers (2)

Aaron Digulla
Aaron Digulla

Reputation: 328604

This can have many reasons. To understand this better, you should print the font that Java is using:

 System.out.println(button.getFont().toString());

That should answer the question whether both JREs use the same default font. If they do, make sure the font is a TrueType font (TTF) and not a bitmap font; bitmap fonts don't scale well. While TTF does scale better, some fonts look bad at certain sizes. So a font might look well at 10 and 12 pixel but odd at 11.

To solve the issue, either set the font yourself, chose a different font if you already override the system default or try to tweak the font size.

Upvotes: 1

mKorbel
mKorbel

Reputation: 109813

rendering is correct, there are three areas

Upvotes: 2

Related Questions