gpt4 book ai didi

java - JTextPane 中的字体颜色同质性问题

转载 作者:行者123 更新时间:2023-12-02 06:07:34 26 4
gpt4 key购买 nike

当我在黑色 JTextPane 上添加一些白色文本时,字体颜色不均匀,导致模糊效果。如果我在同一个 JTextPane 中使用drawString,则文本会被很好地绘制。更改 ANTIALIASING 并不能解决问题。

代码只是我的问题的一个简单示例,这就是我得到的:

enter image description here

感谢大家

public final class Example extends JTextPane {

public static void main(String... aArgs){
new Example();
}

Example() {
JFrame mainFrame= new JFrame();
mainFrame.setSize(200,200);
mainFrame.getContentPane().setLayout(new BorderLayout());
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setVisible(true);
setBackground(Color.black);

StyledDocument doc = getStyledDocument();
Style style = addStyle("I'm a Style", null);
StyleConstants.setForeground(style, Color.white);
StyleConstants.setFontFamily(style,"Courier New");
StyleConstants.setFontSize(style, 20);

try { doc.insertString(doc.getLength(), " Example1",style); }
catch (BadLocationException e){}

mainFrame.getContentPane().add(this);
}

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_FRACTIONALMETRICS,
RenderingHints.VALUE_FRACTIONALMETRICS_ON);

super.paintComponent(graphics2d);

graphics2d.setColor(Color.white);
Font courier = new Font("Courier New",0,20);
graphics2d.setFont(courier.deriveFont(20));
graphics2d.drawString(" Example2", 0, 150);
}

}

最佳答案

我找到了解决方案how to make JTextPane paint anti-aliased font?

我必须添加到 JTextPane

putClientProperty(SwingUtilities2.AA_TEXT_PROPERTY_KEY, null);

并且必须删除 RenderingHints.KEY_ANTIALIASING 和 RenderingHints.KEY_TEXT_ANTIALIASING

关于java - JTextPane 中的字体颜色同质性问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55933402/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com