Antialised text in JEditorPane

  • Thread starter Ross Clement (Email address invalid - do not use)
  • Start date
R

Ross Clement (Email address invalid - do not use)

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
 
R

Roedy Green

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

}
 
R

Ross Clement (Email address invalid - do not use)

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top