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