Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

Antialised text in JEditorPane

Thread view: 
Ross Clement (Email address invalid - do not use) - 02 Feb 2006 16:34 GMT
Hi. How do I get a JEditorPane to do aliased text. I have the following
code in my program which actually seems to work:

   JEP = new JEditorPane()
   {
     public void paintComponent( Graphics g )
     {
       Graphics2D g2d = (Graphics2D) g;
       g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING,
                             RenderingHints.VALUE_ANTIALIAS_ON );
       super.paintComponent( g );
     }
   };

but I'm thinking that there must be a much better way of dloing this. I
could try a getGraphics() on the JEP after creation, and then set the
rendering hint, but will the Graphics object be properly defined before
I've setVisible(true)'d my frame?

Cheers,

Ross-c
Roedy Green - 02 Feb 2006 19:14 GMT
On 2 Feb 2006 08:34:10 -0800, "Ross Clement (Email address invalid -
do not use)" <clemenr@wmin.ac.uk> wrote, quoted or indirectly quoted
someone who said :

>but I'm thinking that there must be a much better way of dloing this. I
>could try a getGraphics() on the JEP after creation, and then set the
>rendering hint, but will the Graphics object be properly defined before
>I've setVisible(true)'d my frame?

It is clumsy!

package com.mindprod.fontshower;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.JTextArea;
import javax.swing.text.Document;

/**
* Like JTextArea, but allows you to control whether fonts are
anti-aliased.
*
* @author Roedy Green
*/
public class AntialiasedJTextArea extends JTextArea {

   /**
    * true if want extra debugging output
    */
   public final static boolean DEBUGGING = false;

   /**
    * true if want anti-aliased fonts
    */
   private boolean antialias = true;

   /**
    * called whenever system has a slice to render
    *
    * @param g
    *        Graphics defining where and region to paint.
    */
   protected void paintComponent ( Graphics g )
       {
       Graphics2D g2d = (Graphics2D)g;
       if ( antialias )
           {
           g2d.setRenderingHint(
RenderingHints.KEY_TEXT_ANTIALIASING,
               RenderingHints.VALUE_TEXT_ANTIALIAS_ON );
           g2d.setRenderingHint( RenderingHints.KEY_RENDERING,
               RenderingHints.VALUE_RENDER_QUALITY );
           // if wanted to smooth geometric shapes too
           // g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING,
           // RenderingHints.VALUE_ANTIALIAS_ON );
           }

       if ( DEBUGGING )
           {
           System.out
               .println( "AntialiasedJTextArea.paintComponent called
with antialias:"
                   + antialias
                   + " "
                   + g2d.getClipBounds() );
           }
       super.paintComponent( g2d );
       }

   /**
    * Control whether fonts are antialiased. Takes extra time, but
makes
    * smoother edges.
    *
    * @param antialias
    *        true if want anti-aliasing.
    */
   public void setAntialias ( boolean antialias )
       {
       this.antialias = antialias;
       this.repaint();
       }

   /**
    * no arg constructor
    */
   public AntialiasedJTextArea()
       {
       super();
       }

   /**
    * Constructs a new AntialiasedJTextArea with the specified number
of rows
    * and columns, and the given model. All of the constructors feed
through
    * this constructor.
    *
    * @param doc
    *        the model to use, or create a default one if null
    * @param text
    *        the text to be displayed, null if none
    * @param rows
    *        the number of rows >= 0
    * @param columns
    *        the number of columns >= 0
    * @exception IllegalArgumentException
    *            if the rows or columns arguments are negative.
    */
   public AntialiasedJTextArea( Document doc, String text, int rows,
       int columns )
       {
       super( doc, text, rows, columns );
       }

   /**
    * Constructs a new empty AntialiasedJTextArea with the specified
number of
    * rows and columns. A default model is created, and the initial
string is
    * null.
    *
    * @param rows
    *        the number of rows >= 0
    * @param columns
    *        the number of columns >= 0
    * @exception IllegalArgumentException
    *            if the rows or columns arguments are negative.
    */
   public AntialiasedJTextArea( int rows, int columns )
       {
       super( rows, columns );
       }

   /**
    * constructor
    *
    * @param message
    *        text to display
    */
   public AntialiasedJTextArea( String message )
       {
       super( message );
       }

   /**
    * Constructs a new AntialiasedJTextArea with the specified text
and number
    * of rows and columns. A default model is created.
    *
    * @param text
    *        the text to be displayed, or null
    * @param rows
    *        the number of rows >= 0
    * @param columns
    *        the number of columns >= 0
    * @exception IllegalArgumentException
    *            if the rows or columns arguments are negative.
    */
   public AntialiasedJTextArea( String text, int rows, int columns )
       {
       super( text, rows, columns );
       }

}
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Ross Clement (Email address invalid - do not use) - 03 Feb 2006 09:39 GMT
Thanks. I was assuming that there would be some method in JEditorPane
or classes it inherits from to set the antialising. From your answer I
take it that there isn't. Your method is neater than mine, but even
more verbose. So, I'll stick with mine for the meantime.

Cheers,

Ross-c


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.