1 / 10

Review of Applets

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. Applications and Applets.

whiteruth
Download Presentation

Review of Applets

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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 Appletor theJAppletclass. Unit 07

  3. 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

  4. Example 1: Passing Parameters to Applets • 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

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

  6. 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

  7. Example 2: Loading Images, Playing Sounds • import javax.swing.*;import java.awt.*;import java.applet.*; • public class ImageAndSound extends Applet{ • Image image; AudioClip sound; • public void init( ) { • image = getImage(getDocumentBase() , "myImage.gif" ) ; • sound = getAudioClip(getDocumentBase() , "mySound.au" ) ; • setBackground(Color.red) ; • } • public void start( ) { • repaint(); • if (sound != null) • sound.loop(); • } • public void stop( ) { • sound.stop(); • } • public void paint(Graphics g){ • Graphics2D g2 = (Graphics2D)g; • g2.drawImage(image , 5 , 5 , this) ; • } • } Unit 07

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

  9. 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 Componentclass implements. • An image observer is informed about many aspects of the image. • The image observer usually calls repaint to update the applet. Unit 07

  10. 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

More Related