Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unit 071 Review of Applets Learning Outcomes oDistinguish between Java Applications and Java Applets. oWrite applet programs that can load images and play.

Similar presentations


Presentation on theme: "Unit 071 Review of Applets Learning Outcomes oDistinguish between Java Applications and Java Applets. oWrite applet programs that can load images and play."— Presentation transcript:

1 Unit 071 Review of Applets Learning Outcomes oDistinguish between Java Applications and Java Applets. oWrite applet programs that can load images and play sound. oExplain the limitations of constructors in applet programs. 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

2 Unit 072 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.

3 Unit 073 Life Cycle of an Applet An applet may call the following methods in its life cycle: 1.init() 2.start() 3.stop() 4.paint() 5.destroy() By default, these methods have empty implementations.

4 Unit 074 Example 1: Passing Parameters to Applets 1 import javax.swing.*; import java.awt.*; 2 public class AppletParameters extends JApplet{ 3 private int size; 4 private String font; 5 private String message; 6 public void init() { 7 size = Integer.parseInt(getParameter("size")); 8 font = getParameter("font"); 9 message = getParameter("message"); 10 } 11 public void paint(Graphics g) { 12 g.setColor(Color.green); 13 g.setFont(new Font(font, Font.PLAIN, size)); 14 g.drawString(message,20, 50); 15 Font myFont = new Font("Dialog", Font.BOLD, 36); 16 g.setFont(myFont); g.setColor(Color.red); 17 g.drawString("You are Welcome to CCSE", 20, 100); 18 g.setColor(Color.blue); 19 g.setFont(new Font("Courier", Font.BOLD+Font.ITALIC, 24)); 20 g.drawString("This is Introduction to Computer Science", 20, 150); 21 } 22 }

5 Unit 075 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.

6 Unit 076 Graphics and Graphics2D (cont’d) In many earlier 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.

7 Unit 077 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 }

8 Unit 078 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.

9 Unit 079 How Java Displays Images (cont’d) Java will start loading a new image only when we attempt to display or manipulate the image 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.

10 Unit 0710 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); }


Download ppt "Unit 071 Review of Applets Learning Outcomes oDistinguish between Java Applications and Java Applets. oWrite applet programs that can load images and play."

Similar presentations


Ads by Google