Java 合金外观和抗锯齿
有人在这里使用合金的外观和感觉吗?我面临一个奇怪的反走样和JTextComponents错误。Alloy在默认情况下根本不使用抗锯齿,所以我必须通过创建自己的UI类来强制使用它。这在大多数情况下都可以正常工作,但某些字符会严重破坏抗锯齿 例如,如果将合金设置为外观,我将一些希伯来语文本插入JTextComponent,例如:שלום、מהש㪡ךךאאקייכד,则整个JTextComponent突然永久失去抗锯齿功能 奇怪的是,我甚至没有扩展AlloyTextPaneUI,而是基本上扩展了AlloyTextPaneUI来进行抗锯齿处理,因此我对合金出现在图片中的位置感到困惑(其他外观和感觉似乎很好) 我很难追踪这只虫子。。。有人面临同样的问题吗 下面是一个简短的示例,演示了该问题:Java 合金外观和抗锯齿,java,swing,look-and-feel,jtextpane,antialiasing,Java,Swing,Look And Feel,Jtextpane,Antialiasing,有人在这里使用合金的外观和感觉吗?我面临一个奇怪的反走样和JTextComponents错误。Alloy在默认情况下根本不使用抗锯齿,所以我必须通过创建自己的UI类来强制使用它。这在大多数情况下都可以正常工作,但某些字符会严重破坏抗锯齿 例如,如果将合金设置为外观,我将一些希伯来语文本插入JTextComponent,例如:שלום、מהש㪡ךךאאקייכד,则整个JTextComponent突然永久失去抗锯齿功能 奇怪的是,我甚至没有扩展AlloyTextPaneUI,而是基本上扩展了All
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();
}
}
}
经过一些令人沮丧的实验,我把它修好了。。我仍然不确定到底是什么原因导致了它,但下面是我如何修复它的。显然,在合金中有一把钥匙丢失/损坏(我不知道是哪把)。因此,我添加了从最初的外观到合金属性的所有默认设置。这是一种快速而肮脏的解决方案(我唯一的问题是菜单变得不透明),但它足以满足我的需要。也许其他人也觉得这很有帮助
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;
}
};
我有同样的问题已经很长时间了。建议的将“Menu.opaque”属性设置为true的解决方案确实有助于消除文本的锯齿(奇怪),但它扰乱了我的菜单外观,我不得不放弃它 这里真正的问题是Alloy L&F的支撑不好,但根据我的经验,它看起来确实不错,而且性能非常好。在对模糊代码(Alloy Look&Feel v1.4.4)进行反编译后,我将问题追溯到一种方法:
public class AlloyLookAndFeel extends BasicLookAndFeel{
...
private static boolean j = false;
...
public static boolean isTextAntialiasingEnabled() {
return j;
}
...
}
我找不到以编程方式更改值的方法,也不可能重写静态方法。所以我不得不求助于一些黑客手段:
private静态布尔j=true代码>
私有静态布尔变量=false代码>
这是一个黑客,但这是你必须做的,以保存一个好的,但支持差的库。我希望有人能有一个更简单的解决方案。我不知道是什么问题,但是,在前后发布一些文本截图,也不会有什么坏处。为了获得更多信息,某些文本最好不带抗锯齿。。。或者可能会出现模糊。仔细阅读,我添加了一个演示问题的简短示例,您可以看到抗锯齿是如何分解的。在这种情况下,当然不喜欢使用非抗锯齿文本,这会使文本看起来非常糟糕(尤其是我在应用程序控制台中使用的字体)。