Anti-aliased text on/off in a JTextArea in JDK1.6

I want to recompile an application in jdk1.6 which was previously built using jdk1.5.

The application has a JTextArea for which the user can toggle anti alias on and off. Never mind why, the important thing is (NOTE!) that it has to be done runtime, by the user checking/unchecking an "anti-alias-button", and the rest of the applications text-should not be affected, only the particular JTextArea.

In jdk1.5 I accomplished this by:

inputText.putClientProperty(com.sun.java.swing.SwingUtilities2.AA_TEXT_PROPERTY_KEY, antiAliasFlag);

where "inputText" is a JTextArea and antiAliasFlag is a boolean.

In jdk1.6 the SwingUtilities2 class is gone and I have not managed to find out how this is supposed to be done.

[747 byte] By [jenspa] at [2007-11-26 16:00:25]
# 1

That's what you get when you use unpublished internal class and unpublished internal property name. In JDK 6.0 the SwingUtilities2 class has been moved into another package and the support for that property was removed. Now all the components under core LAFs (Metal / GTK / Windows) respect the desktop AA settings.

kirillga at 2007-7-8 22:21:49 > top of Java-index,Desktop,Core GUI APIs...
# 2

So there is no way to achieve what i want in jdk1.6 (beeing able to toggle the anti alias of a JTextArea's text in runtime)?

(I was searching for an alternative way of doing this in jdk1.5 too but i only found the one I described above.

The application is kind of special, having to do with creating custom bitmap fonts from regular fonts, and there has to be an option to have anti aliasing turned on or off.)

jenspa at 2007-7-8 22:21:49 > top of Java-index,Desktop,Core GUI APIs...
# 3
You could override the paintComponent() and set the hints on Graphics before painting the text. Unless you really need the core functionality (HTML support, multiline support - which can be accommodated pretty easily), you can have complete control over the rendering hints.
kirillga at 2007-7-8 22:21:49 > top of Java-index,Desktop,Core GUI APIs...
# 4

Thanks!

Here is how I did it, in case anyone be interested:

public class JTextAreaToggleAntiAlias extends JTextArea

{

private boolean aaOn;

public void setAntiAlias(boolean aaOn)

{

this.aaOn = aaOn;

}

public void paintComponent(Graphics g)

{

if (aaOn)

{

((Graphics2D) g).addRenderingHints(new RenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON));

}

else

{

((Graphics2D) g).addRenderingHints(new RenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF));

}

super.paintComponent(g);

}

};

jenspa at 2007-7-8 22:21:49 > top of Java-index,Desktop,Core GUI APIs...
# 5

I also used this undocumented feature but I need to retain antialiasing for Java5 in a few places and have it run on Java6. I dont officially support v6 but people are using it anyway I dont need to enable/disable at runtime.

Is antialiasing on by default on java6, and is this code safe ?

System.getProperty("java.version");

if(version.startsWith("1.5")==true)

{

textPane.putClientProperty(com.sun.java.swing.SwingUtilities2.AA_TEXT_PROPERTY_KEY, new Boolean(true));

}

or is there a safe alternative

paultaylora at 2007-7-8 22:21:49 > top of Java-index,Desktop,Core GUI APIs...
# 6

No, that code isn't safe, for reasons already discussed. In Java 6, Swing queries the system desktop to determine whether it should use anti-aliasing. It's (IMO) unreasonably difficult to override the default behavior, but if you just want to force a particular component to always use AA, the trick posted by jensp should work. However, the info that's retrieved from the desktop is not just an on/off setting, it actually specifies which kind of anti-aliasing to use, the contrast setting, and possibly other stuff. Here's what I use to capture the desktop settings under Java 6 if a applicabale, while still forcing AA in a way that's compatible with Java 5. import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.RenderingHints;

import java.awt.Toolkit;

import java.util.Map;

import javax.swing.*;

import static java.awt.RenderingHints.*;

public class SwingUtils

{

static final RenderingHints aaHints =

new RenderingHints(KEY_TEXT_ANTIALIASING, VALUE_TEXT_ANTIALIAS_ON);

static

{

Map desktopHints = (Map)Toolkit.getDefaultToolkit()

.getDesktopProperty("awt.font.desktophints");

if (desktopHints != null)

{

aaHints.putAll(desktopHints);

}

}

/**

* Causes the given Graphics to draw text anti-aliased by adding the

* appropriate RenderingHints. Under JDK 1.5, this is a simple on/off

* setting; under 1.6, the desktop settings are used.

*/

public static void forceAntiAliasing(Graphics g)

{

if (aaHints != null && (g instanceof Graphics2D))

{

((Graphics2D)g).addRenderingHints(aaHints);

}

}

}

Just call it from within the paintComponent method as before: public void paintComponent(Graphics g)

{

SwingUtils.forceAntiAliasing(g);

super.paintComponent(g);

}

uncle_alicea at 2007-7-8 22:21:49 > top of Java-index,Desktop,Core GUI APIs...
# 7

>if (aaOn)

>{

> ((Graphics2D) g).addRenderingHints(new RenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON));

>}

> else

>{

> ((Graphics2D) g).addRenderingHints(new RenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF));

This is pretty redundant. This does the same:((Graphics2D) g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,

aaOn ? RenderingHints.VALUE_TEXT_ANTIALIAS_ON : RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);

Zom-Ba at 2007-7-8 22:21:49 > top of Java-index,Desktop,Core GUI APIs...