Introduction to Java Applets - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Introduction to Java Applets

Description:

Applets are Java programs embedded in an HTML document. ... Applets are embedded in web pages. You must create a web page for your applet to execute it. ... – PowerPoint PPT presentation

Number of Views:65
Avg rating:3.0/5.0
Slides: 16
Provided by: grego1
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Java Applets


1
Introduction to Java Applets
  • Chapter 20

2
Applets
  • Applets are Java programs embedded in an HTML
    document.
  • When a browser loads a web page containing an
    applet, the applet downloads into the browser and
    executes.
  • The browser executing the applet is its
    container.
  • For testing, another container called the applet
    viewer can be used.

3
Sample Applets
  • The JDK comes with some sample applets in the
    demo directory.
  • One such applet is the game of TicTacToe.

4
JApplet
  • To create our own applet, we extend JApplet.
  • JApplet has methods for init, start, stop, paint,
    and destroy.
  • These can be overridden for our own applet.
  • When a container loads the applet, it calls, in
    sequence, init, start, and paint.

5
JApplet
  • init is used for initialization of the applet.
  • start is called when the applet is loaded and
    each time the page is revisited by the browser.
  • stop is called when the browser leaves the web
    page of the applet.
  • paint is called whenever the applet needs to be
    painted.
  • destroy is called when the container removes the
    applet from memory.

6
Applets
  • Applets are embedded in web pages.
  • You must create a web page for your applet to
    execute it.
  • The applet viewer can be invoked like this
  • appletviewer WelcomeApplet.html
  • You can also open the web page from your browser.

7
Example
  • The next example is a simple applet to draw a
    string.
  • First is the html page containing the applet,
    then the applet code itself follows

8
  • lthtmlgt
  • ltapplet code "WelcomeApplet.class" width
    "300" height "45"gt
  • lt/appletgt
  • lt/htmlgt

9
  • // Fig. 20.6 WelcomeApplet.java
  • // A first applet in Java.
  • import java.awt.Graphics // program uses class
    Graphics
  • import javax.swing.JApplet // program uses class
    JApplet
  • public class WelcomeApplet extends JApplet
  • // draw text on applet's background
  • public void paint( Graphics g )
  • // call superclass version of method paint
  • super.paint( g )
  • // draw a String at x-coordinate 25 and
    y-coordinate 25
  • g.drawString( "Welcome to Java
    Programming!", 25, 25 )
  • // end method paint
  • // end class WelcomeApplet

10
Example
  • The next example demonstrates the use of the init
    method.
  • Here init is used to input some values from the
    user.
  • The applet then prints a string containing the
    sum of the values

11
  • lthtmlgt
  • ltapplet code "AdditionApplet.class" width
    "300" height "50"gt
  • lt/appletgt
  • lt/htmlgt

12
  • // Fig. 20.10 AdditionApplet.java
  • // Adding two floating-point numbers.
  • import java.awt.Graphics // program uses
    class Graphics
  • import javax.swing.JApplet // program uses
    class JApplet
  • import javax.swing.JOptionPane // program uses
    class JOptionPane
  • public class AdditionApplet extends JApplet
  • private double sum // sum of values entered
    by user
  • // initialize applet by obtaining values from
    user
  • public void init()
  • String firstNumber // first string
    entered by user
  • String secondNumber // second string
    entered by user
  • double number1 // first number to add
  • double number2 // second number to add

13
  • // convert numbers from type String to type
    double
  • number1 Double.parseDouble( firstNumber
    )
  • number2 Double.parseDouble( secondNumber
    )
  • sum number1 number2 // add numbers
  • // end method init
  • // draw results in a rectangle on applets
    background
  • public void paint( Graphics g )
  • super.paint( g ) // call superclass
    version of method paint
  • // draw rectangle starting from (15, 10)
    that is 270
  • // pixels wide and 20 pixels tall
  • g.drawRect( 15, 10, 270, 20 )
  • // draw results as a String at (25, 25)
  • g.drawString( "The sum is " sum, 25, 25
    )
  • // end method paint

14
Security
  • Applets use a security model known as the Sandbox
    Security model.
  • The idea here is that the applet is limited in
    what it can do, in essence, staying inside the
    sandbox.

15
End of Slides
Write a Comment
User Comments (0)
About PowerShow.com