Java Reference
In-Depth Information
// Paint a message in the applet window.
public void paint (java.awt.Graphics g)
{
This line is a comment
This paints the string " Test "
in the applet window.
g.drawString ("Test", 20, 20);
}
}
After inserting code into your applet, follow the same procedure as we discussed
above for the HelloWorld applet: compile the program, create a web page with
the appropriate applet tags, and place the web page and class file into the same
directory.
Yo u can test the applet by loading the web page into a browser or you can use
the appletviewer program that comes with the SDK tools. If, for example,
you put your applet into a web page named MyFirstApplet.html ,yourun
appletviewer on it from the command line as follows:
> appletviewer MyFirstApplet.html
It is usually more convenient to use the appletviewer during debugging and
tuning of your applets. Then once they work satisfactorily with appletviewer ,
you can examine how they appear in the browser. With the older JVMs that came
installed in the browsers, it could be difficult to force the browser to load a new
class file rather than use the old one in the cache. With Sun's Java Plug-in, which
should have been installed into your browsers when you installed the SDK and
is also available for downloading from www.java.com , the console window
allows for various commands to the JVM including the “x” command that clears
the cache.
We note here that we focus on applications in much of the course and even
show later how you can add a main() method to an applet so that it can also
runasanapplication. An applet has the inconvenient requirement that you need
an HTML file containing the applet tags if you want to test it in a browser or
appletviewer .Welike applets, however, since they are pedagogically very
useful for the Web Course. You can immediately see the applet running in a web
page and can compare its output to the source code, which is displayed for every
applet. You can download the code, make modifications, and test it. To avoid the
need to make a web page to test the applet, we use the trick of putting the tags
within a comment block in all of our applet source code files as shown here for
the HelloWorld example:
/*<applet code =" HelloWorld.class " width =" 150 "
height =" 50 ">
</applet > */
public class HelloWorld {
...
 
Search WWH ::




Custom Search