APPLET METHODS

In addition to its life cycle methods (init, start, etc.), an applet has methods that manipulate its background color, get its dimensions, find its location and display status messages.

Applet Methods to Get and Set Background Color Method What it Does Color getBackground( ) Return the current background color of the applet. void setBackground( Color c ) Set the current background color of the applet to c.

Applet Methods to Get Applet Dimensions Method What it Does int getHeight( ) Get the height (in pixels) of the applet. int getWidth( ) Get the width (in pixels) of the applet.

Applet Methods to Get Applet Location Method What it Does URL getCodeBase( ) Get the URL locating the class file of this applet. URL getDocumentBase( ) Get the URL locating the HTML file that embeds this applet.

Applet Method to Display Text on the Status Bar void showStatus( String msg ) Not all web browsers have a status bar in which to display information. Those that do may not have the status bar showing.

Applet Methods Page 1 Example This applet uses the five set methods shown above to access information, which it displays in the status bar by calling showStatus. It uses the setBackground method to make the applet appear yellow.

A picture of how the applet appears in is shown on the next page. 1 import .applet.Applet; 2 import java.awt.Color; 3 import java.net.URL; 4 5 public class AppletMethods extends Applet 6 { 7 public void init( ) 8 { 9 this.setBackground( Color.BLUE ); 10 Color backColor = this.getBackground( ); 11 int height = this.getHeight( ); 12 int width = this.getWidth( ); 13 URL codeBase = this.getCodeBase( ); 14 URL docBase = this.getDocumentBase( ); 15 String out = backColor.toString( ) + ", " 16 + height + ", " 17 + width + ", " 18 + codeBase + ", " 19 + docBase; 20 this.showStatus( out ); 21 } 22 }

AppletMethods.

1 2 3 4 5 6

Applet Methods Page 2 If the status bar isn’t visible then right click on the title bar and select Status Bar.

Exercises

1. Modify the DollarToYenApplet applet so that output is shown in the status bar rather than a message dialog.

Applet Methods Page 3