11

我正在使用以下代码在 java 中上传 aller 字体:

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图像选择

窗口显示: 在此处输入图像描述

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

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

4

3 回答 3

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);
    }
}

欢呼弗兰克

于 2013-05-04T12:57:45.530 回答
1

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

要将 ClearType 用于屏幕字体:

  1. 单击开始,单击控制面板,单击外观和主题,然后单击显示。
  2. 在外观选项卡上,单击效果。
  3. 单击以选中使用以下方法平滑屏幕字体的边缘复选框,然后单击列表中的 ClearType。
于 2013-05-03T08:16:03.340 回答
1

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

图书馆首页光滑金属

Smooth Metal外观和感觉是对某些外观和感觉的补充,包括使用、Java增强它们anti-aliased text

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

在类路径中添加库的 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();
    }
}
于 2013-05-08T17:28:04.797 回答