OpenJDK、4K 显示和文本质量

问题描述 投票:0回答:1

我在这里使用缩放比例为 175% 的 4K 显示器和 Windows 11。我有一个 Java 应用程序,它将文本写入位图并显示该位图。使用 Java 1.8.0_371 时,文本质量良好且非常流畅。我尝试过的所有 OpenJDK 版本都会产生较差且像素化的输出,如图所示:

Text with Java 1.8.0_371

Text with OpenJDK 11

Text with OpenJDK 22

有没有办法提高文字质量?

源代码是:

package 4ktest;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

public class TestWindow1 extends JPanel {

  private BufferedImage image;

  public TestWindow1(String text) {
      image = new BufferedImage(600, 100, BufferedImage.TYPE_INT_ARGB);

      Graphics2D g2d = image.createGraphics();
      g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      g2d.setColor(Color.WHITE);
      g2d.fillRect(0, 0, image.getWidth(), image.getHeight());
      g2d.setColor(Color.BLACK);
      g2d.setFont(new Font("Times New Roman", Font.PLAIN, 50));      
      g2d.drawString(text, 10, 50);
      g2d.dispose();
  }

  @Override
  protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      g.drawImage(image, 0, 0, this);
  }

  public static void main(String[] args) {
      SwingUtilities.invokeLater(() -> {
          JFrame frame = new JFrame("Text to Bitmap");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setSize(600, 150);
          TestWindow1 panel = new TestWindow1("Version " + System.getProperty("java.version") );
          frame.add(panel);
          frame.setVisible(true);
      });
  }
}
java swing java-11 java-17
1个回答
0
投票

我可以重现该问题,并且我能够通过在 jframe 渲染中使用渲染提示来解决该问题。

public static void highQualityRenderingHints(Graphics2D g2d){
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
        g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    }

我没有检查哪个实际提示导致了改进。

© www.soinside.com 2019 - 2024. All rights reserved.