Reviews : Java Books :
Beginning Java 2- JDK 1.3 Version : Images and Animation

Buy this book
Title: Beginning Java 2- JDK 1.3 Version
ISBN: 1861003668
US Price: $ 49.99
Canadian Price:
C$ 74.95
UK Price: £ 35.99
Publication Date: March 2000
Pages: 1230
© Wrox Press Limited, US and UK.

Beginning Java 2- JDK 1.3 Version
Images and Animation

HTML for Applets

In this section we will show HTML tags and attributes in upper case to highlight them, although they are not case sensitive. The basic HTML tags you need for embedding an applet in a Web page are as follows:

<APPLET CODE = "AppletName.class"
	WIDTH = "Applet_width_in_pixels"
	HEIGHT = "Applet_height_in_pixels" >
Optional alternate text displayed if the
APPLET tag is not supported
</APPLET>

The optional default text after the APPLET tag is useful to signal when a browser does not support the APPLET tag – when the applet will not be executed. Other than that, the HTML tags above are the minimum required to include an applet in a page. The value for the CODE attribute in the APPLET tag identifies the .class file for the applet. This presumes the applet is in the same directory as the page containing the applet. If this is not the case you must not put the path as part of the CODE value. Instead, you should add a CODEBASE attribute that has a URL as a value that identifies the source for the applet – the URL can be absolute (which would start with "http://") or it can be relative to the directory containing the Web page. While the WIDTH and HEIGHT attributes are mandatory, they only determine the initial space allocated to the applet. The applet can adjust these itself, as we shall see in this chapter. The double quotes around the parameter values can be omitted if the parameter value does not contain spaces or other characters that might cause confusion.

You can also specify parameter values to be read by your applet by placing <PARAM> tags between the <APPLET> and </APPLET> entries, one for each parameter. For example:

<APPLET CODE=MyApplet.class WIDTH=200 HEIGHT=100>
<PARAM  NAME="frameRate" VALUE="10">
<PARAM  NAME="description" VALUE="Some text">

</APPLET>

These tags will execute the MyApplet applet from the directory containing the HTML file, and make a parameter with the name frameRate available with the value "10", plus a second parameter, description, with the value "Some text". These values can be retrieved programmatically from within the applet – usually from within the init() method – by calling the getParameter() method with a String defining the parameter name for which the value is requested as the argument. If it is available, the parameter value is returned as a String, which you can then convert to numeric form where necessary. For example, to retrieve the value of the parameter with the name, frameRate, we could write:

  // Default frame rate
int frameRate = 5;
 // Get the value of the parameter frameRate
String value = getParameter("frameRate");
if(value != null)
  frameRate = Integer.parseInt(value);

It is important to verify that the String returned by the getParameter() method is not null. It will be if there is no PARAM tag specifying the value. Note that the parameter name is not case sensitive, so "FRAMERATE" would specify the same parameter as "framerate".

When your applet expects to read parameter values, it is a good idea to override the default getParameterInfo() method in your applet class to identify their names, their types and their description in an array of strings. The version of the method that your applet class will inherit from the Applet class returns null. This method can be used by the applet context (the Web page) to find out about your applet. For the applet MyApplet, that expects values for the parameters frameRate and description, you would implement the getParamterInfo() method as:

public String[][] getParameterInfo()
{
 String[][] pInfo = {
        { "frameRate",   "integer", "The frames per second"      },
        { "description", "String", "Description of the animation"}
                    };
return pInfo;
}

There must be three elements in each row of the array providing information about a particular parameter. The three elements in a row describe the parameter name, the type of value, and the purpose of the parameter respectively.

Remember to use the OBJECT and EMBED tags as shown in Chapter 1 to add Java 2 support to browsers other than appletviewer.

If you are using this book in non-linear fashion you will find more on deploying applets at the end of Chapter 12 where I also briefly discuss the security issues relating to applets. This is an extensive topic and a thorough discussion of it is beyond the scope of this book. For the most up-to-date documentation regarding the new security architecture in Java 2, refer to the Web pages at: http://java.sun.com/security/

Let's now turn to the fundamentals of dealing with images, and then look at how we can implement an applet from the ground up – this time to handle an image.

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.