有人在使用合金外观吗?我遇到了一个关于抗锯齿和JTextComponents的奇怪问题。默认情况下,合金不使用任何抗锯齿技术,因此我必须通过创建自己的UI类来强制执行它。这在大多数情况下都很好用,但有些字符会对抗锯齿产生破坏。
例如,如果将Alloy设置为外观,并在JTextComponent中插入一些希伯来文本,例如:שלום,מה שלומך שמי הוא האקזיד',整个JTextComponent突然永久失去了抗锯齿。
奇怪的是,我甚至没有扩展AlloyTextPaneUI,而是扩展了BasicTextPaneUI来进行抗锯齿处理,所以我很困惑Alloy从何处涉及(其他外观似乎都能正常工作)。
我很难追踪这个错误......有人遇到过相同的问题吗?
以下是一个简短的示例,演示了这个问题:
例如,如果将Alloy设置为外观,并在JTextComponent中插入一些希伯来文本,例如:שלום,מה שלומך שמי הוא האקזיד',整个JTextComponent突然永久失去了抗锯齿。
奇怪的是,我甚至没有扩展AlloyTextPaneUI,而是扩展了BasicTextPaneUI来进行抗锯齿处理,所以我很困惑Alloy从何处涉及(其他外观似乎都能正常工作)。
我很难追踪这个错误......有人遇到过相同的问题吗?
以下是一个简短的示例,演示了这个问题:
import com.incors.plaf.alloy.AlloyLookAndFeel;
import com.incors.plaf.alloy.themes.glass.GlassTheme;
import javax.swing.*;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicTextPaneUI;
import javax.swing.text.BadLocationException;
import javax.swing.text.StyledDocument;
import java.awt.*;
public class Scrap {
static {
// NOTE: You need a license code for Alloy!
AlloyLookAndFeel.setProperty("alloy.licenseCode", "your license here");
UIManager.put("TextPaneUI", MyTextPaneUI.class.getName());
try {
UIManager.setLookAndFeel(new AlloyLookAndFeel(new GlassTheme()));
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
// With system Look and Feel everything works just fine...
// try {
// UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
// } catch (ClassNotFoundException e) {
// e.printStackTrace();
// } catch (InstantiationException e) {
// e.printStackTrace();
// } catch (IllegalAccessException e) {
// e.printStackTrace();
// } catch (UnsupportedLookAndFeelException e) {
// e.printStackTrace();
// }
}
public static void main(final String args[]) {
JTextPane text = new JTextPane();
text.setFont(new Font("Monospaced", Font.PLAIN, 14));
StyledDocument doc = text.getStyledDocument();
try {
doc.insertString(doc.getLength(), "Here's some regular text. Nice and smooth with anti-aliasing.\n\n", null);
doc.insertString(doc.getLength(), "Here's some more text Blaa Blaa Blaaa. Lorem ipsum... now try to uncomment the Hebrew text.\n\n", null);
// Try to uncomment this line and the anti-aliasing breaks for the whole JTextPane
// doc.insertString(doc.getLength(), "שלום, מה שלומך שמי הוא האקזידן\n\n", null);
// Here's another strange glyph that breaks the anti-aliasing
// doc.insertString(doc.getLength(), "ಠ", null);
} catch (BadLocationException e) {
e.printStackTrace();
}
JFrame frame = new JFrame();
JScrollPane scroll = new JScrollPane();
scroll.setViewportView(text);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
frame.add(scroll, BorderLayout.CENTER);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(600, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static class MyTextPaneUI extends BasicTextPaneUI {
@Override
protected void paintSafely(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(
RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
super.paintSafely(g);
}
public static ComponentUI createUI(JComponent c) {
return new MyTextPaneUI();
}
}
}