[prog] Java/Swing and antialiased fonts

Almut Behrens almut-behrens at gmx.net
Wed Mar 31 10:17:47 EST 2004


On Mon, Mar 29, 2004 at 11:29:32PM +0200, Riccarda Cassini wrote:
> Hi everyone,
> 
> I'm new here, so I thought, a short introduction might be appropriate.
> I'm a woman in her late twenties, who's finally discovered her love for
> programming.

Hi Riccarda, welcome aboard!


> I've managed to acquire some moderate expertise in Java, and some
> rudimentary knowledge of scripting in Perl and Python. I still consider
> myself a newbie.

We all started one day... :)


> Not too long ago, after quite some hunting, I finally managed to get a
> job in the IT sector. With some luck, I was able to convince those
> guys, that I'd be the right one for the job. Yay!!  Feels good to have
> "Software developer" on one's business card :-)

Congratulations!  Hope, you're enjoying your new job.

Some years ago, I was in a similar situation. Background: I have
no formal education in software engineering. Yet, as long as I can
remember, I've been fascinated by technical things. First hardware
(electronics), then software -- thankfully, my parents never cared to
educate me "girl-like", and let me play with whatever I wanted.

After having studied psychology (first tried electrical engineering,
which wasn't all that pleasant), I began a career in scientific
research. This interestingly gave me several opportunities to develop
my programming skills. One day, I decided that fulltime programming
would be more fun, so I switched. That was more than five years ago.
I haven't regretted it, so far.

What I want to say: go for it, if you feel like it! :)


> I'm currently working on an application which I took over from someone
> else. The client who's paying for it, seems to have made up his mind,
> that antialiased fonts would look fancier. His wish is my command, so
> it's my job to find out how to implement that...

What Dan suggested, actually Should Work, in principle ;) - at least it
does for me (for JRE versions 1.4.0 or above).

Maybe, an example of how to integrate it helps.  I love coherent,
recipe-like examples when I'm new at something. They let you understand
the overall picture of things much better than hundreds of pages of
reference documentation...  So, I've attached one below.

The central idea is to derive your own subclass, in which you override
the paint() method. This is called whenever the widget is drawn. In
there, before you make the call to the superclass, you have to set some
rendering hints, which enable the desired functionality (see the
respective javadoc for details). For this you have to cast the Graphics
object passed in to a Graphics2D object.

You'll need one of those subclasses for _every_ widget you want to have
drawn with antialiasing. This seems a little cumbersome to me, but I
didn't find any other way to make it work. (If anyone knows of a better
way, please let me/us know...!)


> I've not been able to make this work, however hard I tried. I'm afraid,
> I'm just not getting something very fundamental...

Don't worry - it also took me quite some time to find out what to do
exactly, because googling didn't turn up too much useful information...
Either there is no proper documentation - or we both are too blind to
see it ;)  Also, I'm still not entirely sure whether this is the only
way to do it - or at least the way you're supposed to do it. I'm not a
java guru after all.

With respect to your "special" guy (I've had those, too ;)) it might
interest you, that for that same program, which required me to solve
the antialiasing issue, I wrote a little class that lets you use any
truetype font you have to make your application look "prettier".
Usable with or without antialiasing.  (I assume, you have your own
collection of favorite fonts - if not, I'd recommend the MS "webfonts"
set - in particular, the font family Verdana looks quite decent, IMHO.
Very nice for clear, uncluttered layouts.)
Anyway, let me know, if you'd find the class useful.

Good luck,

Almut


---------- example of a subclassed JLabel widget ----------

package AA;

public class JLabel extends javax.swing.JLabel {

    public JLabel() {
        super();
    }

    public JLabel(String label) {
        super(label);
    }

    public JLabel(String label, int alignment) {
        super(label, alignment);
    }

    public JLabel(String label, javax.swing.ImageIcon icon, int alignment) {
        super(label, icon, alignment);
    }

    // that's the essential part:

    public void paint(java.awt.Graphics g) {
        ((java.awt.Graphics2D) g).setRenderingHint(
            java.awt.RenderingHints.KEY_TEXT_ANTIALIASING,
            java.awt.RenderingHints.VALUE_TEXT_ANTIALIAS_ON
        );
        super.paint(g);
    }
}

---------- example mini "application" ----------

public class AADemo extends javax.swing.JFrame {

    public AADemo() {

        setTitle("Antialiasing Demo");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                System.exit(0);
            }
        });

        // setup two labels

        javax.swing.JLabel bigText = new AA.JLabel("Hi Riccarda!");
        bigText.setFont(new java.awt.Font("Dialog", 0, 50) );

        javax.swing.JLabel tinyText = new AA.JLabel("Programming is fun...");
        tinyText.setFont(new java.awt.Font("Dialog", 0, 10) );

        // ...and place them, using some layout

        java.awt.Container content = getContentPane();
        content.setLayout(new java.awt.GridLayout(2,1));
        content.add(bigText);
        content.add(tinyText);

        pack();
    }

    public static void main(String args[]) {
        new AADemo().show();
    }
}



More information about the Programming mailing list