当我在黑色JTextPane上添加一些白色文本时,字体颜色不均匀,导致效果模糊。如果我在同一个JTextPane中使用drawString,则文本会很好地绘制。 更改ANTIALIASING不能解决问题。
代码只是我的问题的一个简单示例,这就是我得到的:
感谢所有人
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);
}
}
答案 0 :(得分:0)
我找到了解决方法how to make JTextPane paint anti-aliased font?
我必须添加到JTextPane
putClientProperty(SwingUtilities2.AA_TEXT_PROPERTY_KEY, null);
,并且必须同时删除RenderingHints.KEY_ANTIALIASING和RenderingHints.KEY_TEXT_ANTIALIASING