R
Roedy Green
here is a simple extension to JTextArea to let you control whether the
fonts are displayed with anti-aliasing. The only catch is, it never
anti-aliases the fonts. Similar logic for AWT works fine. Does anyone
know what I am doing wrong? Do I have to override some method other
than paint?
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 = true;
/**
* 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.
*/
public void paint ( 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 ( DEBUGGING )
{
System.out.println( "AntialiasedJTextArea.paint called
antialias:"
+ antialias );
}
super.paint( 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 );
}
}
--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm
Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
fonts are displayed with anti-aliasing. The only catch is, it never
anti-aliases the fonts. Similar logic for AWT works fine. Does anyone
know what I am doing wrong? Do I have to override some method other
than paint?
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 = true;
/**
* 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.
*/
public void paint ( 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 ( DEBUGGING )
{
System.out.println( "AntialiasedJTextArea.paint called
antialias:"
+ antialias );
}
super.paint( 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 );
}
}
--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm
Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes