JButton文本的消除锯齿 [英] Anti-aliasing of JButton text

查看:196
本文介绍了JButton文本的消除锯齿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Font Awesome JButton创建一个可点击的图标,但是当尺寸较小时,生成的图标会显示为别名。就像一点背景, Font Awesome 是一个可下载的 ttf 文件(字体文件),其中每个字符是可缩放矢量图标。看了以前在谷歌和堆栈溢出回答,我试图通过重写JButton的 paintComponent 方法强制消除锯齿;这看起来没有任何效果:

$ p $ import java.awt。*;
import java.io.File;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Test扩展JFrame {

public Test(){
Font fontAwesome = null;
尝试{
fontAwesome = Font.createFont(Font.TRUETYPE_FONT,new File(font-awesome-4.2.0\\fonts\\fontawesome-webfont.ttf));
fontAwesome = fontAwesome.deriveFont(Font.PLAIN,100);
} catch(FontFormatException | IOException e){
e.printStackTrace();

$ b $ JButton iconButton = new JButton(\\\){
@Override
public void paintComponent(Graphics g){
Graphics2D graphics2d =(Graphics2D)g;
graphics2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
//graphics2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
//graphics2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
//graphics2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BICUBIC);
super.paintComponent(graphics2d);
}
};

iconButton.setFont(fontAwesome);
iconButton.setFocusPainted(false);

this.add(iconButton);
this.setVisible(true);
this.pack();


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




$ b $ p
$ b

以下图片以字体大小30,100和200:



如何对小字号强制进行抗锯齿?



更新:我测试了相同的代码使用内置的java字体,而不是字体真棒,和完全相同的问题适用。 JLabel做适当的文本提示,所以你可以把字体真棒文本放在一个JLabel中,然后把JLabel放到一个JButton中:

  JLabel iconLabel = new JLabel(\\\); 
iconLabel.setFont(fontAwesome);

JButton iconButton = new JButton();
iconButton.add(iconLabel);

这个解决方案成功解决了Linux上NimROD L& F。似乎有理由期望它可以在其他配置上工作,但是YMMV。

I'm using Font Awesome within a JButton to create a click-able icon, however the resulting icon appears aliased when at a small size. Just as a bit of background, Font Awesome is a downloadable ttf file (the font file) in which each character is a 'scalable vector icon'. Having looked at previous answers on Google and stack overflow, I have tried to force anti-aliasing by overriding the paintComponent method of JButton; this however has seemingly no effect:

import java.awt.*;
import java.io.File;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Test extends JFrame{

    public Test(){
        Font fontAwesome = null;
        try {
            fontAwesome = Font.createFont(Font.TRUETYPE_FONT, new File("font-awesome-4.2.0\\fonts\\fontawesome-webfont.ttf"));
            fontAwesome = fontAwesome.deriveFont(Font.PLAIN, 100);
        } catch (FontFormatException | IOException e) {
            e.printStackTrace();
        }

        JButton iconButton = new JButton("\uf0a8"){
            @Override
            public void paintComponent(Graphics g) {
                Graphics2D graphics2d = (Graphics2D) g;
                graphics2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                //graphics2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
                //graphics2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
                //graphics2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
                super.paintComponent(graphics2d);
            }
        };

        iconButton.setFont(fontAwesome);
        iconButton.setFocusPainted(false);

        this.add(iconButton);
        this.setVisible(true);
        this.pack();
    }

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

The following images show the resulting font icons at font sizes 30, 100 and 200:

How can I force anti-aliasing for small font sizes?

UPDATE: I have tested the same code using a built in java font rather than Font Awesome, and exactly the same issue applies.

解决方案

JLabel does proper text hinting, so you can put the Font Awesome text in a JLabel, and then put the JLabel in a JButton:

    JLabel iconLabel = new JLabel( "\uf0a8" );
    iconLabel.setFont( fontAwesome );

    JButton iconButton = new JButton( );
    iconButton.add( iconLabel );

This solves successfully works around the issue on Linux with the NimROD L&F. Seems reasonable to expect it to work on other configurations as well, but YMMV.

这篇关于JButton文本的消除锯齿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆