Applet Methods

Applet Methods

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 Internet Explorer is shown on the next page. 1 import java.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.html 1 <html> 2 <body> 3 <applet code="AppletMethods.class" width="288" height="288"> 4 </applet> 5 </body> 6 </html> 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.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    3 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us