n00*_*ter 5 java swing jtextpane antialiasing look-and-feel
这里有人使用合金外观吗?我在抗锯齿和 JTextComponents 方面遇到了一个奇怪的错误。Alloy 默认情况下根本不使用抗锯齿功能,因此我必须通过创建自己的 UI 类来强制使用它。这在大多数情况下都可以正常工作,但某些字符会对抗锯齿造成严重破坏。
\n\n例如,如果合金设置为外观,并且我将一些希伯来语文本插入到 JTextComponent 中,例如:\xd7\xa9\xd7\x9c\xd7\x95\xd7\x9d、\xd7\x9e\xd7\ x94 \xd7\xa9\xd7\x9c\xd7\x95\xd7\x9e\xd7\x9a \xd7\xa9\xd7\x9e\xd7\x99 \xd7\x94\xd7\x95\xd7\x90 \xd7\x94\ xd7\x90\xd7\xa7\xd7\x96\xd7\x99\xd7\x93\',整个 JTextComponent 突然永久失去抗锯齿功能。
\n\n奇怪的是,我什至没有扩展 AlloyTextPaneUI,而是扩展 BasicTextPaneUI 来进行抗锯齿,所以我很困惑合金在哪里出现(其他外观和感觉似乎工作得很好)。
\n\n我很难追踪这个错误...有人遇到过同样的问题吗?
\n\n这是一个演示该问题的简短示例:
\n\nimport com.incors.plaf.alloy.AlloyLookAndFeel;\nimport com.incors.plaf.alloy.themes.glass.GlassTheme;\n\nimport javax.swing.*;\nimport javax.swing.plaf.ComponentUI;\nimport javax.swing.plaf.basic.BasicTextPaneUI;\nimport javax.swing.text.BadLocationException;\nimport javax.swing.text.StyledDocument;\nimport java.awt.*;\n\npublic class Scrap {\n\n static {\n // NOTE: You need a license code for Alloy!\n AlloyLookAndFeel.setProperty("alloy.licenseCode", "your license here");\n\n UIManager.put("TextPaneUI", MyTextPaneUI.class.getName());\n\n try {\n UIManager.setLookAndFeel(new AlloyLookAndFeel(new GlassTheme()));\n } catch (UnsupportedLookAndFeelException e) {\n e.printStackTrace();\n }\n\n // With system Look and Feel everything works just fine...\n// try {\n// UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());\n// } catch (ClassNotFoundException e) {\n// e.printStackTrace();\n// } catch (InstantiationException e) {\n// e.printStackTrace();\n// } catch (IllegalAccessException e) {\n// e.printStackTrace();\n// } catch (UnsupportedLookAndFeelException e) {\n// e.printStackTrace();\n// }\n }\n\n public static void main(final String args[]) {\n JTextPane text = new JTextPane();\n\n text.setFont(new Font("Monospaced", Font.PLAIN, 14));\n\n StyledDocument doc = text.getStyledDocument();\n\n try {\n\n doc.insertString(doc.getLength(), "Here\'s some regular text. Nice and smooth with anti-aliasing.\\n\\n", null);\n\n doc.insertString(doc.getLength(), "Here\'s some more text Blaa Blaa Blaaa. Lorem ipsum... now try to uncomment the Hebrew text.\\n\\n", null);\n\n // Try to uncomment this line and the anti-aliasing breaks for the whole JTextPane\n// doc.insertString(doc.getLength(), "\xd7\xa9\xd7\x9c\xd7\x95\xd7\x9d, \xd7\x9e\xd7\x94 \xd7\xa9\xd7\x9c\xd7\x95\xd7\x9e\xd7\x9a \xd7\xa9\xd7\x9e\xd7\x99 \xd7\x94\xd7\x95\xd7\x90 \xd7\x94\xd7\x90\xd7\xa7\xd7\x96\xd7\x99\xd7\x93\xd7\x9f\\n\\n", null);\n\n // Here\'s another strange glyph that breaks the anti-aliasing\n// doc.insertString(doc.getLength(), "\xe0\xb2\xa0", null);\n\n } catch (BadLocationException e) {\n e.printStackTrace();\n }\n\n JFrame frame = new JFrame();\n\n JScrollPane scroll = new JScrollPane();\n scroll.setViewportView(text);\n scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);\n\n frame.add(scroll, BorderLayout.CENTER);\n frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);\n frame.setSize(600, 300);\n frame.setLocationRelativeTo(null);\n frame.setVisible(true);\n }\n\n public static class MyTextPaneUI extends BasicTextPaneUI {\n\n @Override\n protected void paintSafely(Graphics g) {\n Graphics2D g2 = (Graphics2D) g;\n g2.setRenderingHint(\n RenderingHints.KEY_TEXT_ANTIALIASING,\n RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);\n super.paintSafely(g);\n }\n\n public static ComponentUI createUI(JComponent c) {\n return new MyTextPaneUI();\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n
经过一些非常令人沮丧的实验后,我修复了它。我仍然不确定到底是什么导致了它,但这是我修复它的方法。合金中的 UIDefaults 中显然有一把钥匙丢失/损坏(我不知道是哪一个)。因此,我将初始外观和感觉中的所有默认值添加到合金属性中。这是一种快速而肮脏的解决方案(我遇到的唯一问题是菜单变得不透明),但它足以满足我的需求。也许其他人也觉得它很有帮助。
AlloyLookAndFeel laf = new AlloyLookAndFeel(theme) {
@Override
public UIDefaults getDefaults() {
UIDefaults defs = new UIDefaults();
defs.putAll(UIManager.getLookAndFeelDefaults());
initClassDefaults(defs);
initSystemColorDefaults(defs);
initComponentDefaults(defs);
defs.put("Menu.opaque", true);
return defs;
}
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1246 次 |
| 最近记录: |