在Windows和Linux之间加载ttf字体时JLabel的差异

时间:2013-04-30 16:02:52

标签: java swing fonts jlabel true-type-fonts

我使用以下代码在java上传过敏字体:

private Font loadFont(final String path) {
    Font font = null;

    InputStream fontFile = null;
    fontFile = FontLoaderClass.class.getResourceAsStream(path);

    if (fontFile != null) {
        try {
            font = Font.createFont(Font.PLAIN, fontFile);
        } catch (FontFormatException e) {
            LOGGER.error("Error with font format {}", e);
        } catch (IOException e) {
            LOGGER.error("Error accessing font {}", e);
        }
    }
    return font;
}

正确加载字体:

http://www.fontsquirrel.com/fonts/Aller

字体设置为所有“.font”更改java应用程序的默认设置,但是在Linux中显示正确但Windows不是。

private Font buildFont(final String key, final int size) {
    Font f = loadFont(ALLER_LT_FONT_PATH);
    GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(f);
    if (f == null) {
        f = (Font) UIManager.get(key);
    }
    f = f.deriveFont(Font.TRUETYPE_FONT, size);
    return f;
}

Linux显示: linux image pick

Windows显示: enter image description here

正如您在图像中看到的那样,Windows中有一些切断导致图像无法正确显示。

之前有没有人遇到过这个问题?

3 个答案:

答案 0 :(得分:4)

找到附加的两个小演示,它们可以为Swing组件分别进行抗锯齿处理。

用于Swing组件

// to enable antialiasing (AA) for Swing components
//
// either:
//    start the JVM with the option -Dawt.useSystemAAFontSettings=on
//    see also: http://docs.oracle.com/javase/6/docs/technotes/guides/2d/flags.html#aaFonts
// or:
//    System.setProperty("awt.useSystemAAFontSettings", "on");
//    - you must call it before the first Swing component is rendered
//    - if AA it's on by default you must set it "off", otherwise you can't
//      toggle it inside the application

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import static java.awt.RenderingHints.KEY_ANTIALIASING;
import static java.awt.RenderingHints.VALUE_ANTIALIAS_OFF;
import static java.awt.RenderingHints.VALUE_ANTIALIAS_ON;

public class SwingAntiAliasingDemo {

    public static void main(String[] args) {
        System.setProperty("awt.useSystemAAFontSettings", "off");
        initGui();
    }

    public static void initGui() {
        JFrame frame = new JFrame();

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent we) {
                System.exit(0);
            }
        });

        Font font = new Font("Serif", Font.TRUETYPE_FONT, 96);
        JPanel jpanel = new JPanel(new BorderLayout());

        JLabel labelAA = new JLabel("Antialiasing ON") {
            @Override
            public void paintComponent(Graphics g) {
                Graphics2D graphics2d = (Graphics2D) g;
                graphics2d.setRenderingHint(KEY_ANTIALIASING,
                        VALUE_ANTIALIAS_ON);
                super.paintComponent(g);
            }
        };
        labelAA.setFont(font);
        labelAA.setForeground(Color.WHITE);

        JLabel labelNoAA = new JLabel("Antialiasing OFF") {
            @Override
            public void paintComponent(Graphics g) {
                Graphics2D graphics2d = (Graphics2D) g;
                graphics2d.setRenderingHint(KEY_ANTIALIASING,
                        VALUE_ANTIALIAS_OFF);
                super.paintComponent(g);
            }
        };
        labelNoAA.setFont(font);
        labelNoAA.setForeground(Color.WHITE);

        jpanel.setBackground(new Color(0, 22, 95));
        jpanel.add(labelAA, BorderLayout.NORTH);
        jpanel.add(labelNoAA, BorderLayout.SOUTH);

        frame.setTitle("stackoverflow question 16304254");
        frame.getContentPane().add(jpanel);
        frame.setLocation(200, 200);
        frame.pack();
        frame.setResizable(false);
        frame.setVisible(true);
    }
}

用于绘制操作

// to enable antialiasing (AA) for draw operations

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;

import static java.awt.RenderingHints.KEY_ANTIALIASING;
import static java.awt.RenderingHints.VALUE_ANTIALIAS_OFF;
import static java.awt.RenderingHints.VALUE_ANTIALIAS_ON;

public class DrawAntiAliasingDemo extends JFrame {

    private Font font;
    private Color backGroundColor;

    public static void main(String[] args) {
        new DrawAntiAliasingDemo();
    }

    public DrawAntiAliasingDemo() {
        font = new Font("Serif", Font.TRUETYPE_FONT, 96);
        backGroundColor = new Color(0, 22, 95);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent we) {
                System.exit(0);
            }
        });

        setTitle("stackoverflow question 16304254");
        setSize(850, 260);
        setResizable(false);
        setVisible(true);
    }

    @Override
    public void paint(Graphics g) {
        Graphics2D d = (Graphics2D) g;
        d.setColor(backGroundColor);
        d.fillRect(0, 0, getWidth(), getHeight());
        d.setFont(font);
        d.setPaint(Color.white);

        d.setRenderingHint(KEY_ANTIALIASING, VALUE_ANTIALIAS_ON);
        d.drawString("Antialiasing ON", 10, 115);

        d.setRenderingHint(KEY_ANTIALIASING, VALUE_ANTIALIAS_OFF);
        d.drawString("Antialiasing OFF", 10, 230);
    }
}

欢呼声 弗兰克

答案 1 :(得分:1)

在我看来,Windows操作系统没有使用ClearType。 ClearType是一个选项,启用后它会平滑字体。有时它因调整或性能原因而被禁用。见http://support.microsoft.com/kb/306527。尝试启用它。

将ClearType用于屏幕字体:

  1. 单击“开始”,单击“控制面板”,然后单击“外观和主题” 然后单击显示。
  2. 在“外观”选项卡上,单击“效果”。
  3. 单击以选中“使用以下方法平滑屏幕字体边缘”复选框,然后单击列表中的“ClearType”。

答案 2 :(得分:1)

我之前遇到过类似的问题,直到我在SourceForge找到了这个名为Smooth Metal的库,您可以从Here

下载它

图书馆主页Smooth Metal

Smooth Metal外观与某些外观相辅相成,包括Java,并使用anti-aliased text进行增强。

您会注意到Windows中的ClearType选项不会影响结果...

在类路径中添加库的jar文件之后,我编写了这个小型java应用程序,它呈现JLabel并撤消渲染两个JButtons,结果非常清晰:

以下是您可以测试的代码:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.plaf.basic.BasicLabelUI;
import smoothmetal.SmoothLabelUI; // UI Class to set for the JLabel


public class LabelRender extends JFrame{

    JPanel panel = new JPanel();
    JPanel panel2 = new JPanel();
    JButton button = new JButton("RENDER");
    JButton button2 = new JButton("UNDO");
    JLabel label = new JLabel("HELLO WORLD");

    public LabelRender(){
        setSize(600, 250);
        setLocationRelativeTo(null);
        setTitle("JLabel Renderer");


        setLayout(new BorderLayout());

        label.setFont(new Font("Aller", Font.PLAIN, 70));
        label.setForeground(Color.WHITE);

        panel.add(label);
        panel2.add(button);
        panel2.add(button2);

        panel.setBackground(Color.BLACK);

        this.add(panel2, BorderLayout.NORTH );
        this.add(panel,BorderLayout.CENTER);

        setVisible(true);
        validate();


       // System.out.println(label.getUI());

        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                label.setUI(new SmoothLabelUI());
            }
        });

        button2.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                label.setUI(new BasicLabelUI());
            }
        });

    }

    public static void main(String args[]){
        new LabelRender();
    }
}