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