Text Antialiasing Appearance Inconsistent
Hello all,
I am developing an application that displays rotated text, on Mac OS X Snow Leopard antialiasing seems to be consistent and smooth regardless of the fonts size however on Windows 7 and Vista antialiasing is smooth just like on OS X when the font (Arial) is greater then 100pt and noticeably changes when less then 100pt. It seems that on Windows the method of antialiasing changes depending on the fonts size where as on OS X it is consistent.
Here is an image demonstrating this:
http://doodletype.com/Antialiasing/jagged2.png
I want the text to be antialiased as smooth as possible. On OS X small letters appear very smooth however as you can see in the image on Windows the letter begins to appear rough or jagged. Is there a way to keep the same antialiasing when the font is small? It seems the way the letter is antialiased changes depending on the font size but this only happens on Windows and is undesirable for the application I am developing as it seems to yield less smooth results.
Any help with this would be great. Thank you :)
Here is a sample application I developed. It scales though various font sizes, you should be able to see a noticeable change in antialiasing when the letter is smaller then 100pt on a Windows OS.
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package smoothtext;
import java.awt.*;
import javax.swing.*;
import javax.swing.Action.*;
import com.sun.awt.AWTUtilities.*;
import java.awt.Rectangle;
import java.awt.font.*;
public class SmoothText {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
SwingUtilities.invokeLater( new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(Exception err) {
System.out.println(err);
}
new SmoothText();
}
});
}
int offset = 1;
Font font = new Font("Arial", Font.PLAIN, 80);
PaintSurface canvas = new PaintSurface();
JFrame frame = new JFrame("Smooth Text");
public SmoothText() {
renderHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
renderHints.put(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
frame.setSize(500,500);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(canvas);
frame.setVisible(true);
new Thread(new AnimationThread()).start();
}
class AnimationThread implements Runnable {
public void run() {
while(true) {
try {
Thread.sleep(30);
} catch(InterruptedException err) {
//handle interrupted exception
}
if(font.getSize()>120) {
offset = -1;
} else if(font.getSize()<20) {
offset = 1;
}
font = new Font(font.getFamily(), font.getStyle(), font.getSize()+offset);
canvas.repaint();
}
}
}
class PaintSurface extends JComponent {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
//ALSO HAVE TRIED
//g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
g2.addRenderingHints(renderHints);
g2.setColor(Color.red);
g2.drawString("Font Size: "+font.getSize(), 20, 20);
g2.setColor(Color.black);
g2.setFont(font);
TextLayout layout = new TextLayout("D", font, g2.getFontRenderContext());
Rectangle bounds = layout.getPixelBounds(null, 0, 0);
g2.rotate(Math.toRadians(7), (getWidth()/2 - bounds.width/2)+bounds.width/2, ((getHeight()/2 - bounds.height/2)-(int)bounds.getY())+bounds.height/2);
g2.drawString("D", getWidth()/2 - bounds.width/2, (getHeight()/2 - bounds.height/2)-(int)bounds.getY());
}
}
}