2013-01-17 37 views
4

這裏的任何人使用合金的外觀和感覺?我正面臨着一個奇怪的錯誤與反鋸齒和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(); 
     } 
    } 
} 
+0

我不知道什麼是錯的,但是,發佈一些屏幕截圖的文字前後,也[SSCCE](http://sscce.org)也不會傷害。只是爲了獲得額外的信息,一些文本最好不要反鋸齒...或者可能會發生模糊。閱讀[使用渲染提示顯示抗鋸齒文本](http://docs.oracle.com/javase/tutorial/2d/text/renderinghints.html) –

+0

好吧,我添加了一個簡短的例子來演示這個問題,你可以看看自己如何消除鋸齒。在這種情況下,使用非反鋸齒文本當然是不可取的,它使得文本顯得非常糟糕(特別是在我的應用中使用的字體 - Consolas)。 – n00bster

回答

3

一些非常令人沮喪的實驗我把它固定後..我還是不知道究竟是什麼引起的,但在這裏就是我如何固定它。顯然,合金中的UIDefaults中有一個關鍵缺失/損壞(我不知道哪一個)。所以我將最初的外觀和感覺的所有默認值添加到Alloy屬性中。這是一種快速和骯髒的解決方案(唯一的問題是菜單變得不透明),但它足夠滿足我的需求。也許其他人認爲它也有幫助。

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; 
    } 
}; 
0

我在很長一段時間都有同樣的問題。將「Menu.opaque」屬性設置爲true的建議解決方案確實有助於使文本變爲別名(奇怪),但它與我的菜單看起來的方式混淆,我不得不放棄它。

這裏真正的問題是合金L & F得到了很好的支持,但它確實看起來不錯,而且從我的經驗來看,性能非常好。反編譯混淆的代碼後(合金看&感覺V1.4.4),我追蹤的問題到了一個方法:

public class AlloyLookAndFeel extends BasicLookAndFeel{ 
    ... 
    private static boolean j = false; 
    ... 
    public static boolean isTextAntialiasingEnabled() { 
     return j; 
    } 
    ... 
} 

我無法找到一個方法來編程方式更改值,它是無法覆蓋靜態方法。所以我不得不求助於一些黑客行爲:

  1. 使用JD反編譯代碼(請參閱http://jd.benow.ca/)。代碼被模糊處理,所以變量和方法名稱不是很有意義。
  2. 複製com.incors.plaf.alloy.AlloyLookAndFeel到您的項目中的代碼取決於合金LAF(包名稱必須相同)
  3. 糾正Ĵ

    private static boolean j = true;

  4. 的價值
  5. 有一個布爾fb變量在反編譯後無法訪問的小問題,所以用fbValue替換所有出現的fb並向類中添加一個聲明(調試器將幫助您找到當前值,在我的情況下它是錯誤的)。

    private static boolean fbVariable = false;

後您編譯您的項目,你應該有文字反鋸齒。 這是一個黑客,但這是你必須做的,以保存一個良好的,但支持不力的圖書館。我希望有人有更簡單的解決方案。