Writing an Applet

     

Let's just write an applet and try to view it and see what comes out of the woodwork, shall we?

SimpleApplet.java

 

 package net.javagarage.applets; import java.awt.Color; import java.awt.Container; import java.awt.FlowLayout; import javax.swing.JApplet; import javax.swing.JButton; import javax.swing.JLabel; /**  * <p>  * Example of very simple JApplet.  * @author eben hewitt  */ public class SimpleApplet extends JApplet { public void init() { Container content = getContentPane(); content.setBackground(Color.PINK); content.setLayout(new FlowLayout()); content.add(new JLabel("Hello, world")); content.add(new JButton("Poke me"));    } } 

This applet doesn't do much at all. You see a label, you see a button, you press the button, nothin' happens. Put whatever code you want inside there. My purpose is to show you that you need to extend JApplet or Applet , and then write some stuff in the init() method body.

Now that the writing is out of the way, let's view that sucker. If you're using Eclipse or another IDE, you know what to do ”cheat. If you want to rack up a little extra karma, you can write the HTML file now and view it with the appletviewer tool that ships with the JDK. Here's a file we can use.

ShowSimpleApplet.html

 

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>Applet Test</title> </head> <body> <applet align="center"      code="net.javagarage.applets.SimpleApplet.class"      width="200" height="100"> </applet> </body> </html> 

Notice a couple of things here. First, we use the .class file extension. Second, we use the fully qualified class name , which means including package names .

Now, you can just open your browser and point it to that HTML page to view the applet. Or, you can be fancy and use the JDK appletviewer tool at a terminal window, like this:

 

 >appletviewer F:\eclipse\workspace\garage\net\javagarage\applets\Show SimpleApplet.html 

This is the full path to the file on my Windows XP machine. Note that you need not include the drive. Adjust the command for the location of your HTML file. This should launch the viewer program and display the applet, as shown in Figure 29.2.

Figure 29.2. Our little applet is all grown up and out in the world.
graphics/29fig02.gif

If something went wrong and your HTML page just shows a gray box with a little red X in the upper-left corner, either there is a fatal error of some kind or (most likely) your applet cannot be found. You can check the path to the file and try again (make sure you included the fully qualified name of the applet class, including packages) ”OR, you can right-click on the applet and select Open Java Console. This will show you the stack trace leading up to the problem. It is very likely that one of the following occurred:

  1. You have specified the path to the applet incorrectly.

  2. You have left off the .class extension from the name of the applet class in the HTML.

  3. You have placed the HTML file in a place where it can't find the applet class: remember that the HTML should be at the root of your classes directory (the one that has "net" in it) ”that's where java executes from.

Notice what I had to do here. If you look in the address bar, you'll see that the HTML page is referenced from within the classes directory. That's because that's where the class is. If you make a new file in your editor, it is likely to be sitting in the source directory ”not the classes directory. Don't tell me that you have your source code in the same directory as your classes ”that would be punishable by something pretty bad. A whole week without playing Road Blaster or Gauntlet, for example.

Applet Life Cycle Methods

There are several methods that allow you a little control over what happens during the life of an applet. Here they are in brief.

 

 public void init() {   // Called once by the browser when it starts the applet     }     public void start() {        //called when the page that contains           //the applet is made visible     }     public void stop() {        //called when the page that contains the applet           //is made invisible     }     public void destroy() {       //called when the browser destroys the applet     }     public void paint(Graphics g) {        //called when the applet is repainted     } 



Java Garage
Java Garage
ISBN: 0321246233
EAN: 2147483647
Year: 2006
Pages: 228
Authors: Eben Hewitt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net