Applets - PowerPoint PPT Presentation

About This Presentation
Title:

Applets

Description:

Applications are invoked by the Java interpreter, and applets are ... is invoked to redraw the graphical representation of the applet. The update() Method ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 20
Provided by: yda76
Category:
Tags: applets | redraw

less

Transcript and Presenter's Notes

Title: Applets


1
Applets
  • The Applet Class
  • The ltappletgt HTML Tag
  • Passing Parameters to Applets

2
HelloWorld Applet
  • // This application program prints Hello World!
  • public class HelloWorld
  • public static void main(String args)
  • System.out.println(Hello World )
  • // This applet prints Hello World!
  • import java.awt.
  • import java.applet.
  • public class HelloWorldApplet extends Applet
  • public void paint(Graphics g)
  • g.drawString(Hello, World!)

3
Applications vs. Applets
  • Similarities
  • Since they both are subclasses of the Container
    class, all the user interface components, layout
    managers, and event-handling features are the
    same for both classes.
  • Differences
  • Applications are invoked by the Java interpreter,
    and applets are invoked by the Web browser.
  • Applets have security restrictions
  • Web browser creates graphical environment for
    applets, GUI applications are placed in a frame.

4
Security Restrictions on Applets
  • Applets are not allowed to read from, or write
    to, the file system of the computer viewing the
    applets.
  • Applets are not allowed to run any programs on
    the browsers computer.
  • Applets are not allowed to establish connections
    between the users computer and another computer
    except with the server wherethe applets are
    stored.

5
Conversions Between Applications and Applets
  • Conversions between applications and applets are
    simple and easy.
  • You can convert an applet into an application.
  • You can convert an application to an applet as
    long as security restrictions are not violated.

6
The Applet Class
  • public void init()
  • public void start()
  • public void stop()
  • public void destroy()
  • public void paint(Graphics g)
  • public void update(Graphics g)

7
Browser Calling Applet Methods
8
The init() Method
  • Invoked when the applet is first loaded and
    again if the applet is reloaded.
  • Common functions implemented in this method
    include creating threads, loading images, setting
    up user-interface components, and getting
    parameters from the ltappletgt tag in theHTML
    page.

9
The start() Method
  • Invoked after the init() method is executed
    also called whenever the applet becomes active
    again after a period of inactivity (for example,
    when the user returns to the page containing the
    applet after surfing other Web pages).
  • Functionality might include restarting
    threads(for example, to resume an animation) or
    simply telling the applet to run again.

10
The stop() Method
  • The opposite of the start() method, which is
    called when the user moves back to the page
    containing the applet the stop() method is
    invoked when the user moves off the page.
  • When the user leaves the page, any threads the
    applet has startedbut not completedwill
    continue to run.

11
The destroy() Method
  • Invoked when the browser exits normally to
    inform the applet that it is no longer needed and
    that it should release any resources it has
    allocated.
  • Usually, you will not need to override this
    method unless you need to release specific
    resources, such as threads that the applet
    created.

12
The paint() Method
  • public void paint(Graphics g)
  • is invoked to redraw the graphical representation
    of the applet.

13
The update() Method
  • Override update(Graphics g) method to avoid
    flickering effect.
  • public void update(Graphics g)
  • // do something
  • public void paint(Graphics g)
  • update(g)

14
Example Applets
  • Geocoordinates conversion
  • http//www.cs.joensuu.fi/koles/utm/utm.html
  • Vector map visualization
  • http//www.cs.joensuu.fi/koles/svf/svf.htm
    l
  • Electronic Documents Management (EDM)
  • http//www.cs.joensuu.fi/koles/edm/edm2.ht
    ml

15
Writing Applets
  • Always extends the Applet class or extends the
    JApplet class, which is a subclass of Applet for
    Swing components.
  • Override init(), start(), stop(), and destroy()
    if necessary. By default, these methods are
    empty.
  • Add your own methods and data if necessary.
  • Applets are always embedded in an HTML page.

16
The ltappletgt HTML Tag
  • ltapplet
  • codeclassfilename.class
  • widthapplet_viewing_width_in_pixels
  • heightapplet_viewing_height_in_pixels
  • archivearchivefile
  • codebaseapplet_url
  • vspacevertical_margin
  • hspacehorizontal_margin
  • alignapplet_alignment
  • altalternative_text
  • gt
  • ltparam nameparam_name1 valueparam_value1gt
  • lt/appletgt

17
Passing Parameters to Applets
  • ltapplet
  • code MyApplet.class"
  • width 200
  • height 50gt
  • alt"You must have a Java-enabled
  • browser to view the applet"
  • ltparam nameMESSAGE valueHello world!"gt
  • ltparam nameX value20gt
  • ltparam nameY value20gt
  • lt/appletgt

18
Parsing Parameters in Applet
  • public class MyApplet extends Applet
  • private String message default message
  • private int x 10
  • private int y 10
  • public void init()
  • message getParameter(MESSAGE)
  • try
  • x Integer.parseInt(getParameter(X))
  • y Integer.parseInt(getParameter(Y))
  • catch(Exception e)

19
Examples of Applets
  • See code and applet on web
  • http//www.cs.armstrong.edu/liang/introjb4.h
    tml
  • See more examples of applets on web
  • http//www.dgp.toronto.edu/mjmcguff/learn/ja
    va/
Write a Comment
User Comments (0)
About PowerShow.com