I wrote a little Text Editor using the javax.swing package and noticed the fonts looked ugly. Anti-aliasing is turned off by default.
As an experiment, I created a subclass for a JTextArea and wrote a paintComponent() routine that enabled anti-aliasing. The fonts look blurry and that is a problem.
I have another version of the editor using the heavyweight components of just awt and the anti-aliasing looks great. I'm guessing it's because the heavyweights use native platform code.
Could someone point me in some kind of direction?? Here is my paintComponent() function and some other code related to how everything is put together. Am I missing something in there?
Code:
public class TextEditor {
AntiAliasedJTextArea textArea;
textArea = new AntiAliasedJTextArea();
textArea.setEditable(true);
textArea.setFont(new Font("Arial", Font.PLAIN, 14));
rootContainer.add(scrollPane); // The JTextArea is added to the
// JScrollPane then inserted to the
// rootContainer
}
private class AntiAliasedJTextArea extends JTextArea {
public AntiAliasedJTextArea() { }
protected void paintComponent(Graphics g) {
// Create a new Graphics 2D
Graphics2D g2d = null;
g2d = (Graphics2D) g ;
// Set rendering Hints
g2d.setRenderingHints(new RenderingHints(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON ));
// Paint component
super.paintComponent(g);
}
}