Life Cycle of an Applet

Review of Applets
• Applications and Applets
• Example 1: Passing Parameters to Applets
• The Graphics and the Graphics2D Classes
• Example 2: Loading Images and Playing Sounds
• How Java Loads and Displays Images
• Example 3: Applet and Application Hybrid
Unit 07
1
Applications and Applets
• A Java program can be an application, applet or both.
• A Java application is a stand-alone, unrestricted program.
• A Java applet is a restricted program
that relies on another program to
execute.
• A Java applet executes under a Web
browser or applet viewer.
• An applet is defined by extending the
Applet or the JApplet class.
Unit 07
2
Life Cycle of an Applet
•
•
An applet may call the following methods in its life cycle:
•
init()
•
start()
•
stop()
•
paint()
•
destroy()
These methods have empty implementations in the Applet
class.
Unit 07
3
Example 1: Passing Parameters to Applets
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
import javax.swing.*; import java.awt.*;
public class AppletParameters extends JApplet{
private int size;
private String font;
private String message;
public void init() {
size = Integer.parseInt(getParameter("size"));
font = getParameter("font");
message = getParameter("message");
}
public void paint(Graphics g) {
g.setColor(Color.green);
g.setFont(new Font(font, Font.PLAIN, size));
g.drawString(message,20, 50);
Font myFont = new Font("Dialog", Font.BOLD, 36);
g.setFont(myFont); g.setColor(Color.red);
g.drawString("You are Welcome to CCSE", 20, 100);
g.setColor(Color.blue);
g.setFont(new Font("Courier", Font.BOLD+Font.ITALIC, 24));
g.drawString("This is Introduction to Computer Science", 20, 150);
}
}
Unit 07
4
The Graphics and the Graphics2D Classes
• Drawing graphics is done differently on different computer
platforms.
• Java provides an abstract Graphics class that covers all
platforms.
• The Graphics class was included with the first version of
Java.
• A more sophisticated graphics class, Graphics2D, was
introduced later.
• The Graphics2D class extends rather than replace Graphics.
Unit 07
5
Graphics and Graphics2D (cont’d)
• In many programs, the public void paint(Graphics g) method includes the
statement
Graphics2D g2 = (Graphics2D)g;
• Each time paint is called, it is passed a Graphics2D object for drawing in
the display area
• An instance of Graphics or Graphics2D is called a graphics context.
• A graphics context represents a drawing surface.
• The Graphics2D object passed to paint is up-cast to Graphics.
• Additional functionality in Graphics2D is available after the down-cast.
Unit 07
6
Example 2: Loading Images, Playing Sounds
1. import javax.swing.*;import java.awt.*;import java.applet.*;
2. public class ImageAndSound extends Applet{
3.
Image image; AudioClip sound;
4.
public void init( ) {
5.
image = getImage(getDocumentBase() , "myImage.gif" ) ;
6.
sound = getAudioClip(getDocumentBase() , "mySound.au" ) ;
7.
setBackground(Color.red) ;
8.
}
9.
public void start( ) {
10.
repaint();
11.
if (sound != null)
12.
sound.loop();
13. }
14. public void stop( ) {
15.
sound.stop();
16. }
17. public void paint(Graphics g){
18.
Graphics2D g2 = (Graphics2D)g;
19.
g2.drawImage(image , 5 , 5 , this) ;
20. }
21. }
Unit 07
7
How Java Loads and Displays Images
• The preceding example reminds us of loading images and
playing sounds
• To display an image you need to:
1. retrieve the image from a file or from an Internet source;
2. draw the image.
• The getImage method starts the loading process and returns
immediately.
• Thus, Java loads images in an asynchronous manner.
• The benefit is that the program can do something if loading the
image takes time.
Unit 07
8
How Java Displays Images (cont’d)
• Images are drawn using the overloaded drawImage method:
drawImage(Image img, int x, int y, Color bgC, ImageObserver obs)
• The drawImage method starts the download and returns
immediately.
• ImageObserver is an interface that the Component class implements.
• An image observer is informed about many aspects of the image.
• The image observer usually calls repaint to update the applet.
Unit 07
9
Example 3: Applet and Application Hybrid
import java.awt.*;
import javax.swing.*;
public class AppletApplication extends JApplet{
public void paint(Graphics g){
Graphics2D g2 = (Graphics2D)g;
g2.drawString("Salaam Shabab!", 30,30);
}
public static void main(String args []){
JFrame f = new JFrame("Applet and Application");
AppletApplication applet = new AppletApplication();
Container cp = f.getContentPane();
cp.add(applet);
f.setSize(150,150);
f.setVisible(true);
}
}
Unit 07
10