Disable Anti-Aliasing in Swing?

For a given Swing component, I’d like to explicitly DISABLE anti-aliasing just for that component, not globally.

What’s the preferred way of doing this?

http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Graphics2D.html#setRenderingHint(java.awt.RenderingHints.Key,%20java.lang.Object)

Call it in paintComponent(Graphics g) of the component who’s appearance you want to alter.

That was actually one of the first things I tried. It didn’t work at all. To be specific, I’m turning off text anti-aliasing (since small fonts look terrible when anti-aliased), so I used both the normal AA key as well as the text-specific one. To ensure that my eyes weren’t deceiving me, I zoomed in 800% in Photoshop to verify this.

Is there something else I’m missing?

Try this:


yourComponent.putClientProperty(SwingUtilities2.AA_TEXT_PROPERTY_KEY, new Boolean( false ) );

Dmitri

I get a CCE.

“java.lang.Boolean cannot be cast to sun.swing.SwingUtilities2$AATextInfo”

Is there some documentation for SwingUtilities2, or is it one of those internal classes?

Unfortunately it’s one of those internal classes, and I guess the interface has changed.

You can tell the application to ignore the desktop hints (basically tell every text control
not to be antialiased) but I guess that’s not what you’re after.

http://java.sun.com/products/java-media/2D/reference/faqs/index.html#Q_How_can_I_specify_the_text_ant

Dmitri

That’s one of the great reasons you should never use a sun.* class, as they are subject to change without notice.

Listen to Mr. Gol, he is correct. Just extend the component, override the default paint/paintComponent method, then disable the antialiasing there.

Run this test as an example:


import javax.swing.*;
import java.awt.*;
class AAJLabel extends JLabel {
	boolean antialias;
	public AAJLabel(String str,boolean antialias) {
		super(str);
		this.antialias = antialias;
	}
	public void paintComponent(Graphics g) {
		Graphics2D g2d = (Graphics2D)g;
		if (!antialias) g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
		else g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
		super.paintComponent(g2d);
	}
}

public class Test extends JFrame {
	AAJLabel label[];
	public Test() {
		super("Test");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		label = new AAJLabel[2];
		label[0] = new AAJLabel("Test with Antialias",true);
		label[0].setFont(label[0].getFont().deriveFont(48.0f));
		label[1] = new AAJLabel("Test without Antialias",false);
		label[1].setFont(label[0].getFont());
		Container c = getContentPane();
		c.setLayout(new GridLayout(0,1));
		c.add(label[0]);
		c.add(label[1]);
		pack();
		setVisible(true);
	}
	public static void main(String args[]) {
		new Test();
	}
}

Or if you’re feeling lazy, you can just run the attached JAR.

The problem is that super.paintComponent() will override your setting.

Dmitri

Did you run the example I attached? It works.

Not on my Mac, for the reasons that Dmitri stated. It does work on my PC, which I should have stated in the OP.