Review of Applets - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

Review of Applets

Description:

14 g.drawString(message,20, 50); 15 Font myFont = new Font('Dialog', Font.BOLD, 36) ... 17 g.drawString('You are Welcome to CCSE', 20, 100); 18 g.setColor(Color.blue) ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 11
Provided by: QuaziAbid5
Category:

less

Transcript and Presenter's Notes

Title: Review of Applets


1
Review of Applets
  • Learning Outcomes
  • Distinguish between Java Applications and Java
    Applets.
  • Write applet programs that can load images and
    play sound.
  • Explain 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
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
Life Cycle of an Applet
  • An applet may call the following methods in its
    life cycle
  • init()
  • start()
  • stop()
  • paint()
  • destroy()
  • By default, these methods have empty
    implementations.

4
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(getParam
eter("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.BOLDFont.ITALI
C, 24)) 20 g.drawString("This is
Introduction to Computer Science", 20, 150) 21
22
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.

6
Graphics and Graphics2D (contd)
  • 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
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.r
ed) 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
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
How Java Displays Images (contd)
  • 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
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)
Write a Comment
User Comments (0)
About PowerShow.com