Prev Up Next
Go backward to Adding an Applet to a Web Page
Go up to Top
Go forward to Other Applet Possibilities

A More Useful Applet

To give you a bit more experience with applets, we will now constuct an applet that implements a simple, active button for a web page.

Bascially, what we will do first is modify the applet so that it loads the picture of the button and displays it in the applet screen area.

The first method in the applet is named "begin". It is intended to hold actions you would like performed when an applet first becomes active. When the applet is used in a web page, this happens when the page is first visited. This is when the image should be loaded and displayed. So:

The first command tells the applet to load the image. Uses of "getImage" are only allowed within the "begin" method.

The second command actually displays the image in the applet screen area.

To make things look better, we should shrink the applet window so that the button just fits. To do this,

Finally, to make the button "active", we will take two steps. We will add some code to make it change appearance when the mouse is moved into the button. We will also add code to load a new web page when the user clicks on the button.

You will have noticed that clicking the mouse on the button still does not load a new page. That is because the Applet viewer is not smart enough to load a page. To test this behavior of your new applet, you will need to view the applet in a browser. So, add a link from your "cs105labs" page to the "DisplayApplet.html" page in the "ButtonApplet" folder. Use Netscape to display your cs105labs page and follow the new link to test the applet.

For more fun, copy the <APPLET...> and </APPLET> tags found in "DisplayApplet.html" to another of your .html files to add the active button to that page. To make it work, you will also need to place a copy of "AppletClasses.jar" and "button.gif" in the same folder as any .html file to which you add the applet.


Prev Up Next