Review of Applets - PowerPoint PPT Presentation

About This Presentation
Title:

Review of Applets

Description:

A Java program can be an application, applet or both. A Java application is ... A Java applet is a restricted program that relies on another program to execute. ... – PowerPoint PPT presentation

Number of Views:64
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
  • 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()
  • These methods have empty implementations in the
    Applet class.

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.BOLDFont.ITALIC, 24))

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 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
  • 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)

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)
  • 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