Introduction to Computing
Download
Report
Transcript Introduction to Computing
Introduction to Applets
Passing parameters to an Applet
Displaying images in an Applet
Using Sound in Applets
1
Passing parameters to an Applet
The syntax to define parameters, in an HTML file, to be passed to an applet is:
<APPLET CODE= . . . WIDTH= . . .
<PARAM
NAME=parameter_name1
<PARAM
NAME=parameter_name1
.
.
.
<PARAM
NAME=parameter_nameN
</APPLET>
HEIGHT= . . . >
VALUE=parameter_value1>
VALUE=parameter_value1>
VALUE=parameter_valueN>
Example:
<APPLET CODE= "MyApplet.class" WIDTH=300 HEIGHT=200>
<PARAM
NAME=font
VALUE= "TimesRoman" >
<PARAM
NAME=size
VALUE="36" >
</APPLET>
2
Passing parameters to an applet (cont’d)
Parameters, defined by the PARAM tags, are passed to your applet when it is loaded.
In the init method of your applet you can retrieve a parameter by using the getParameter
method:
String getParameter(String parameterName)
The getParameter method returns null if no PARAM tag contains parameterName.
Since getParameter returns a string; if you want a parameter to be some other type, you
have to convert it yourself.
Example, to get the size parameter from the previous HTML file, we can have:
String s =
int theSize
getParameter("size") ;
= Integer.parseInt(s) ;
3
Passing parameters to an applet (cont’d)
import java.awt.*;
<APPLET code=DisplayMessage.class width=900 height=300>
import javax.swing.*;
<PARAM NAME=size VALUE="72">
public class DisplayMessage extends JApplet{
private int
sizeParameter;
<PARAM NAME=font VALUE="Serif">
private String fontParameter;
<PARAM NAME=message VALUE="The truth is out there!">
private String messageParameter;
</APPLET>
private Font
font ;
public void init(){
sizeParameter = Integer.parseInt(getParameter("size"));
fontParameter = getParameter("font");
messageParameter = getParameter("message");
font = new Font(fontParameter, Font.BOLD, sizeParameter);
}
public void paint(Graphics g){
Graphics2D g2 = (Graphics2D) g ;
g2.setFont(font);
g2.setColor(Color.yellow);
setBackground(Color.cyan);
g2.drawString(messageParameter,75,150);
}
}
4
Displaying images in an Applet
Basic image handling in Java is conducted through the Image class, which is part of the
java.awt package.
Java currently supports two image formats - GIF, PNG and JPEG.
The Applet class provides a method called getImage to load an image into an Image object.
One form of getImage takes two arguments - a URL, obtained by the call getDocumentBase( )
or getCodeBase( ), and a string that is the relative path to the image.
getDocumentBase( ) returns the URL of the HTML file that invoked the applet
Example of URLs: file///path/filename.html and http://www.siteName.com/filename.html
getCodeBase( ) returns the URL of the applet class file
Example:
Image
bookImage
=
getImage(getDocumentBase() , "images/book.gif" );
When the method getImage is called, it causes the image to be loaded while the program
continues its execution.
5
Displaying images in an Applet (cont’d)
After an image has been loaded into an Image object it can be displayed in the applet's paint
method by using the drawImage method of the associated graphics object.
To draw an image at its actual size, the drawImage method is called with four arguments:
g2.drawImage(imageObject , x_coordinate , y_coordinate , this) ;
An image can be displayed with a different size using six arguments:
g2.drawImage(imageObject , x_coordinate , y_coordinate , newWidth , newHeight , this) ;
Note:The this parameter in a drawImage call is a reference to an ImageObserver object. Such an
object can monitor an image while it loads.
The drawImage method returns immediately, even if the image has not finished being drawn. It is
the job of the image observer to monitor the image as it is being drawn.
6
Displaying images in an Applet (cont’d)
The methods getHeight and getWidth of an Image object may be useful when scaling the image.
The getHeight method returns the height of an image as an integer, and the getWidth method returns
the width:
int
imageWidth = myImage.getWidth(this); //this is a reference to an image observer
Example:
import javax.swing.*;
import java.awt.*;
public class Images extends JApplet{
Image scene;
public void init( ){
scene = getImage(getCodeBase() , "images/sunset.jpg" ) ;
setBackground(Color.white) ;
}
public void paint(Graphics g){
Graphics2D g2 = (Graphics2D)g;
g2.drawImage(scene , 5 , 5 , this) ;
int newHeight = 2 * scene.getHeight(this);
int newWidth = 2 * scene.getWidth(this);
g2.drawImage(scene , 200 , 160 , newHeight , newWidth , this ) ;
}
}
7
Using Sound in Applets
In Java 2 you can load and play digitized sound files in at least the following formats: AIFF,
AU, and WAV.
One way to play sound is to load the corresponding sound file into an AudioClip object by
using the applet's getAudioClip( ) method.. AudioClip is part of the java.applet package .
For example, the following statement loads a sound file into the adhan object:
AudioClip adhan = getAudioClip( getDocumentBase( ) , "audio/adhan.wav" ) ;
After you have created the AudioClip object, you can call the play( ), stop( ), and loop( )
methods on it.
The method play( ), plays the sound once, stop( ) halts the playback, and loop( ) plays it
repeatedly.
If the getAudioClip( ) method cannot find the sound file indicated by its arguments, the value
of the AudioClip object will be null.
Trying to play a null object results in an error. It is better to test for this condition before using
an AudioClip object:
if(adhan != null)
adhan.play() ;
8
Using Sound in Applets (cont’d)
import java.awt.*;
import java.applet.AudioClip;
import javax.swing.*;
public class Sound extends JApplet {
AudioClip adhan;
public void init( ) {
adhan = getAudioClip(getDocumentBase(), "audio/adhan.wav");
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.displayString(“This is the call to prayer . . .", 5, 20);
if(adhan != null)
adhan.loop();
}
}
9
Using Sound in Applets (cont’d)
A sound loop will not stop automatically when a Web user moves to another page.
To ensure that a sound file is played only while the Web page that contains the applet is being
viewed, the start( ) and stop( ) methods of the Applet can be used.
Example:
import java.awt.*;
import java.applet.AudioClip;
import javax.swing.*;
public class SoundApplet extends JApplet {
String soundFilename = "audio/adhan.wav";
AudioClip adhan;
public void init() {
adhan = getAudioClip(getDocumentBase(), soundFilename);
}
public void start(){
// start() is called when you go to the Web page containing the applet
repaint();// calls update method to clear the background
// The update method then calls the paint method
if(adhan != null)
adhan.loop();
}
10
Using Sound in Applets (cont’d)
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.drawString("Now playing " + soundFilename, 40, 50);
}
public void stop(){
// The browser calls stop() when you change to a different Web page
adhan.stop();
}
}
11