1. Applet Basics
At this point you should be comfortable with using most of the features of the Java programming language, and you have had a pretty thorough introduction to basic graphics programming in Java.
An applet is a special kind of Java program that a Java-enabled browser can download from the Internet and then run. You can find thousands of thousand Java applets are included in websites.
1.1 A Simple Applet
An applet is simply a Java class that extends the java.applet.Applet class and is an AWT component. From now on we are going to use Swing to implement applets.
Read the following simple program. I think there is nothing new to you.
However as there is no main function in this program, so you cannot run this program on the JVM.
1.2 Applet Vewing
To execute the applet, you need to carry out two steps:
- Compile your java source files into class file, say TestApplet.class
- Create an HTML file that tells the browser which class file to load first and how to size the applet displaying area on the webpage.
For our example, the corresponding HTML could be like
Save the above into an HTML file, say TestApplet.html. Then use a browser e.g. MS IE to open the file TestApplet.html, then you will see the message is displayed in the center of display area in the web browser.
Change width = 300, height = 300 in the html file to other width and height values to see what will happen.
Of course, a good style is to test your applet class file in the applet viewer before you view it in a browser.
The program applet viewer is part of the JDK. You can simply view your applet by
>appletviewer TestApplet.html
Please note you should input the html rather than the class file to the viewer program.
1.3 The basic method in Applet class
Every applet is a subclass of the JApplet class,
You don’t implement all the method, but it is better to put all the initialization work in this method.
2. Conversion between Applet and Application
In general, an applet can be converted to an application without loss of functionality. An application can be converted to an application as long as it does not violates the security restrictions imposed on applets.
The differences between an applet and application are
- Application is run on the JVM and its has a main frame, but applet don’t have a main frame because the frame will be created by the browser.
- Application has a main function but the applet does not.
To convert an application program to an applet, you only need to remove the initialization for the main frame and remove the main function and put other initial work into the init() method
To convert an applet to an application, add extra initialization for a frame and put them in a main function.
A possible way is to change the main method to the init() method, and conversely change the init() method to the main method.
3. Parameters to Applets
A parameter can be passed to applets, but the parameter must be declared in the HTML file and must be read by the applet when it is initialized.
Parameters are declared using the
tag of HTML. The
tag must be embedded in the
tag and has no end tag. The syntax for the
tag is given below
The tag specifies a parameter and its corresponding string value.
In your applet code, to read the parameter declared in the HTML file, you need to use the following method defined in the JApplet class
where the parametername must be the name specified in the HTML file.
Read the program in Listing 18.5 first. And then try the following program.
The applet code is:
4. Multimedia
Applets can handle both images and audio. But the image files must be in GIF, PNG and JPEG form, and the audio files in AU, AIFF, WAVE or MIDI form.
Usually the files are specified as a URL.
4.1 Encapsulating URLs
A URL is really nothing more than a description of a resource on the Internet. For example the CSU’s URL ishttp://www.csu.edu.au/index.html on the Internet.
Java uses the URL class to encapsulate a URL address. To make a URL instance in Java is quite simple
URL address = new URL(“http://www.csu.edu.au/index.html”);
That URL specifies the file index.html. Another URL constructor is to use relative URL, like
URL data = new URL(address, “data/student.dat”);
for the file student.dat , located in the data subdirectory of the URL address .
A common way of obtaining a URL is to ask an applet where it came from,
- What is the URL of the HTML page in which the applet is contained?
- What is the URL of the applet’s codebase directory?
with the method getDocumentBase and getCodeBase, respectively, in the applet code.
4.2 Getting multimedia files
You can retrieve images and audio files with the getImage and getAudioClip methods. For example
Image me = getImage(getCodeBase(), “images/gaophoto.jpg”);
AudioClip song = getAudioClip(getCodeBase(), “audio/mysong.wav”);
AudioClip song = getAudioClip(getCodeBase(), “audio/mysong.wav”);
Here, getCode Base method returns the URL from which your applet code is loaded. The second argument is the relative location of the files to the base code URL.
Once you have got the image, then you can use drawImage method of the Graphics calss to show the image.
To play an audio clip, simply invoke Applet ’s play method
play(song);
Or
play(getCodeBase(), “audio/mysong.wav”)
No comments:
Post a Comment