Using Component Methods in an Applet

Since applets are subclasses of java.awt.Component they also have all these methods. You can use these methods to set the default values of color, font, and so on used by the Graphics object passed to the paint() method. For example, Here's another way to write an applet that uses a Label whose text is 24 point, SansSerif, bold and blue and whose background color is yellow.
import java.awt.*;
import java.applet.*;


public class CubScoutApplet extends Applet {

  int height = 0;

  public void init() {
  
     this.setForeground(Color.blue);
     this.setBackground(Color.yellow);
     this.setFont(new Font("Sans", Font.BOLD, 24)); 
     this.height = this.getSize().height;
     
  }
  
  public void paint(Graphics g) {
  
    g.drawString("Cub Scouts!", 5, height/2);
  
  }

}
Cub Scouts applet

The main difference is that this sets the background of the entire applet to yellow, not just the label. Furthermore, the properties of the applet will be inherited by any components the applet contains.


Previous | Next | Top | Cafe au Lait

Copyright 1998 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified June 26, 1998