如何修复Java图形中的文本质量? [英] How to fix text quality in Java graphics?

查看:129
本文介绍了如何修复Java图形中的文本质量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.JFrame;

public class TextRectangle extends JFrame {

    private static final long serialVersionUID = 1L;

    public static void main(String[] a) {
        TextRectangle f = new TextRectangle();
        f.setSize(300, 300);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(
            RenderingHints.KEY_TEXT_ANTIALIASING,
            RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2d.setRenderingHint(
            RenderingHints.KEY_RENDERING,
            RenderingHints.VALUE_RENDER_QUALITY);

        g2d.setColor(Color.WHITE);
        g2d.fillRect(0, 0, getWidth(), getHeight());

        g2d.setColor(Color.BLACK);
        g2d.setFont(new Font("Calibri", Font.BOLD, 18));
        g2d.drawString("Why does this text look so ugly?", 30, 150);
    }

}

结果:

正如您所看到的,字母间距不均匀。有时它是1像素,有时它是2或甚至3像素。另一个问题是'i'上面的点比底部宽。

As you can see the letter spacing is uneven. Sometimes it's 1px, sometimes it's 2 or even 3 pixels. Another issue is the dot above 'i' is wider than the bottom part.

在文本编辑器中编写的相同文本看起来要好得多。你知道如何解决这个问题吗?

The same text written in a text editor looks much better. Do you know how to fix this problem?

我的环境是Windows 8.1,Java 1.8。

My environment is Windows 8.1, Java 1.8.

推荐答案

图形上下文默认使用整数度量 - 意味着舍入应用于字形形状的位置向量。

The graphics context uses integer metrics by default - meaning that rounding is applied to the location vectors of the glyph shapes.

您可以切换到小数指标使用:

g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
    RenderingHints.VALUE_FRACTIONALMETRICS_ON);

如您所见,没有使用子像素抗锯齿(仅灰色抗锯齿像素)。您可以启用子像素抗锯齿以提高LCD屏幕的清晰度:

As you can see, no subpixel antialiasing was used (only gray antialias pixel). You can enable subpixel antialiasing for better legibility on LCD screens:

g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, 
    RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);

有4种模式:


  • VALUE_TEXT_ANTIALIAS_LCD_HRGB :水平定向RGB

  • VALUE_TEXT_ANTIALIAS_LCD_HBGR :水平定向BGR

  • VALUE_TEXT_ANTIALIAS_LCD_VRGB :垂直方向RGB

  • VALUE_TEXT_ANTIALIAS_LCD_VBGR :垂直方向BGR

  • VALUE_TEXT_ANTIALIAS_LCD_HRGB: horizontally oriented RGB
  • VALUE_TEXT_ANTIALIAS_LCD_HBGR: horizontally oriented BGR
  • VALUE_TEXT_ANTIALIAS_LCD_VRGB: vertically oriented RGB
  • VALUE_TEXT_ANTIALIAS_LCD_VBGR: vertically oriented BGR

我还没有找到查询当前显示和方向的适当值的位置。某些显示可以倾斜(横向/纵向)甚至旋转,需要在绘画时重新检测模式。

I haven't found out where to query the appropriate value of the current display and orientation. Some displays can be tilted (landscape/portrait) or even rotated, requiring to redetect the mode when painting.

编辑

我在 Filthy Rich Clients 书中找到了一些内容:显然,Java AWT工具包可以提供适当的渲染提示:

I found something in the Filthy Rich Clients book: apparently, the Java AWT Toolkit can provide appropriate rendering hints:

Map<?, ?> desktopHints = 
    (Map<?, ?>) Toolkit.getDefaultToolkit().getDesktopProperty("awt.font.desktophints");

Graphics2D g2d = (Graphics2D) g;
if (desktopHints != null) {
    g2d.setRenderingHints(desktopHints);
}
// no need to set more rendering hints

在我的系统上,这将使用小数度量和LCD HRGB抗锯齿呈现文本,与上面的代码相同。我系统上的提示是:

On my system, this renders the text with fractional metrics and LCD HRGB antialiasing, same as with the code above. The hints on my system are:


  • 特定于文本的抗锯齿启用密钥: LCD HRGB抗锯齿文本模式

  • 文字专用LCD对比度键: 120

  • Text-specific antialiasing enable key: LCD HRGB antialiasing text mode
  • Text-specific LCD contrast key: 120

这篇关于如何修复Java图形中的文本质量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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