Font measuring and Apple properties

    The text does not fit in the Canvas calculated to hold it, at least for those for which the "apple.awt.textantialiasing" property reports as "on" when Safari is opened and the applet is run.  However, turning the "apple.awt.textantialiasing" property "on" or "off" using the button in the applet does not seem to affect whether the text fits into the box.  

    The nomenclature of "on" and "off" was used because the property reports as "on" if nothing is done to set it on my system.  The 1.4.1 Release Notes use "true" and "false" names for the states of this property, but using that nomenclature instead doesn't seem to affect whether the text fits into the box either.

    Setting some other properties in applets doesn't seem to work, as judged by the behavior of a test applet, so perhaps the property is not getting set even though it reports as being set.

    If you have any insights, workarounds or comments about this test page please contact Mickey Segal.  A listing of  many Java resources is at this link.

Source code:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class apple_properties3 extends Applet implements ActionListener {

LabelCanvas myLabel;
Button b;
boolean propertyValue = false;

public void init() 
{
    setBackground(new Color(240, 225, 255));
    setAppleProperty("awt.textantialiasing", getPropertyName());
    myLabel = new LabelCanvas("Text should just fit horizontally on the Canvas");
    myLabel.setBackground(Color.cyan);
    add(myLabel);
    b = new Button(getButtonText());
    b.addActionListener(this);
    add(b);
    invalidate();
    validate();
}

String getPropertyName()
{
    return(propertyValue? "on" : "off");
}

String getButtonText()
{
    return("Turn textantialiasing " + (propertyValue? "off" : "on"));
}

void setAppleProperty(String s, String value)
{
    try 
    {
        Class stringClass = Class.forName("java.lang.String");
        Class.forName("java.lang.System").getMethod("setProperty", new Class[]{stringClass,
            stringClass}).invoke(null, new Object[]{"apple." + s, value});
    } 
    catch (Exception e) {}
}

public void actionPerformed(ActionEvent ae)
{
    if (ae.getSource() == b) 
{
    propertyValue = !propertyValue;
    setAppleProperty("awt.textantialiasing", getPropertyName());
    b.setLabel(getButtonText());
    repaint();
}
}

public final void paint(Graphics g)
{
    g.drawString("width of box = " + myLabel.width, 10, getSize().height - 25);
    g.drawString("Property apple.awt.textantialiasing = " + System.getProperty("apple.awt.textantialiasing"),
        10, getSize().height - 5);

} // END OF Class apple_properties3



class LabelCanvas extends Canvas { 

int height;
int width;
Font f;
String label;
boolean needToResetWidth;

LabelCanvas(String label)
{
    this.label = label;
    f = new Font("Serif", Font.BOLD, 17);
    height = 30;
    needToResetWidth = true;
}

final void setWidth(Graphics g)
{
    g.setFont(f);
    FontMetrics fm = g.getFontMetrics();
    width = fm.stringWidth(label);
    needToResetWidth = false; 
}

public void addNotify()
{
    super.addNotify();
    repaint();
}

public final void paint(Graphics g)
{
    g.setFont(f);
    if (needToResetWidth) setWidth(g);
    g.drawString(label, 0, height - 5);


public final Dimension getMinimumSize()
{
    if (needToResetWidth)
    {
        Graphics g = getGraphics();
        setWidth(g);
        g.dispose();
    }
    return (new Dimension(width, height));
}

public final Dimension getPreferredSize()
{
    return(getMinimumSize());
}

public final void update(Graphics g)
{
    paint(g);
}
} // END OF Class LabelCanvas