1   /**
2    * Distribution License:
3    * JSword is free software; you can redistribute it and/or modify it under
4    * the terms of the GNU Lesser General Public License, version 2.1 as published by
5    * the Free Software Foundation. This program is distributed in the hope
6    * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7    * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8    * See the GNU Lesser General Public License for more details.
9    *
10   * The License is available on the internet at:
11   *       http://www.gnu.org/copyleft/lgpl.html
12   * or by writing to:
13   *      Free Software Foundation, Inc.
14   *      59 Temple Place - Suite 330
15   *      Boston, MA 02111-1307, USA
16   *
17   * Copyright: 2005
18   *     The copyright to this program is held by it's authors.
19   *
20   * ID: $Id: AntiAliasedTextPane.java 2050 2010-12-09 15:31:45Z dmsmith $
21   */
22  package org.crosswire.common.swing;
23  
24  import java.awt.Graphics;
25  import java.awt.Graphics2D;
26  import java.awt.RenderingHints;
27  
28  import javax.swing.JTextPane;
29  
30  /**
31   * An extension of JTextPane that does Anti-Aliasing.
32   * JDK15(joe): we will need to take a bit of care not clashing with J2SE5 AA
33   * 
34   * @see gnu.lgpl.License for license details.<br>
35   *      The copyright to this program is held by it's authors.
36   * @author Joe Walker [joe at eireneh dot com]
37   */
38  public class AntiAliasedTextPane extends JTextPane {
39      /*
40       * (non-Javadoc)
41       * 
42       * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
43       */
44      @Override
45      public void paintComponent(Graphics g) {
46          if (g instanceof Graphics2D) {
47              Graphics2D g2 = (Graphics2D) g;
48  
49              if (antiAliasing) {
50                  g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
51                  g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
52              }
53          }
54          super.paintComponent(g);
55      }
56  
57      /**
58       * @return Returns the anti aliasing status.
59       */
60      public static boolean isAntiAliasing() {
61          return antiAliasing;
62      }
63  
64      /**
65       * @param antiAliasing
66       *            The new anti aliasing status.
67       */
68      public static void setAntiAliasing(boolean antiAliasing) {
69          AntiAliasedTextPane.antiAliasing = antiAliasing;
70          // Set it system wide for the next run
71          System.setProperty("swing.aatext", Boolean.toString(antiAliasing));
72  
73      }
74  
75      /**
76       * Serialization ID
77       */
78      private static final long serialVersionUID = 3256728398477734965L;
79  
80      /**
81       * Do we anti-alias the text box?
82       */
83      private static boolean antiAliasing;
84  }
85