這裏的任何人使用合金的外觀和感覺?我正面臨着一個奇怪的錯誤與反鋸齒和JTextComponents。合金默認情況下根本不使用抗鋸齒功能,所以我必須通過創建自己的UI類來強制執行。這在大多數情況下都能正常工作,但某些字符會對抗鋸齒造成嚴重破壞。Alloy Look and Feel and Anti-Aliasing
例如,如果該合金被設置爲外觀和感覺,我插入一些希伯來文到JTextComponent,e.g:שלום,מהשלומךשמיהואהאקזיד」,整個的JTextComponent突然失去抗鋸齒 - 永久。
奇怪的是,我甚至沒有擴展AlloyTextPaneUI,但BasicTextPaneUI做反鋸齒,所以我很困惑合金進入圖片的位置(其他外觀和感覺似乎工作得很好)。
我很難跟蹤這個bug ...任何人都面臨同樣的問題?
這裏有一個簡單的例子證明了問題:
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();
}
}
}
我不知道什麼是錯的,但是,發佈一些屏幕截圖的文字前後,也[SSCCE](http://sscce.org)也不會傷害。只是爲了獲得額外的信息,一些文本最好不要反鋸齒...或者可能會發生模糊。閱讀[使用渲染提示顯示抗鋸齒文本](http://docs.oracle.com/javase/tutorial/2d/text/renderinghints.html) –
好吧,我添加了一個簡短的例子來演示這個問題,你可以看看自己如何消除鋸齒。在這種情況下,使用非反鋸齒文本當然是不可取的,它使得文本顯得非常糟糕(特別是在我的應用中使用的字體 - Consolas)。 – n00bster