Introduction to Applets - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Introduction to Applets

Description:

HTML (Hypertext Markup Language): A language used to define Web pages. ... Applets interact with the user through the Abstract Window toolkit (AWT) classes. ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 13
Provided by: ccseKf
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Applets


1
Introduction to Applets
  • What is an Applet?
  • General Form of an Applet
  • Applet initialization and termination
  • The paint method
  • Displaying strings in the applet window
  • How to place an applet in a web page
  • Executing a Java Applet

2
What is an Applet?
  • HTML (Hypertext Markup Language) A language used
    to define Web pages.
  • Web browser A Web browser is software that
    retrieves HTML documents and formats them for
    viewing.
  • A browser is the primary vehicle for accessing
    the worldwide web.
  • Java programs can be classified into two
    categories Applications and applets
  • Java application A Java program that can be run
    without the use of a Web browser.
  • Applet A Java program whose bytecode is linked
    into an HTML document. The bytecode is retrieved
    and executed using a Java-compatible Web browser
    or an applet viewer.

3
General Form of an Applet
  • All applets are subclasses of the Applet class
    contained in the java.applet package.
  • Applets interact with the user through the
    Abstract Window toolkit (AWT) classes.
  • The AWT contains support for a window-based,
    graphical interface.
  •  
  • Thus the general form of an Applet is
  • import java.awt.
  • import java.applet.
  •  
  • public class AppletName extends Applet
  • . . .

4
General Form of a JApplet
  • The class Applet has a subclass called JApplet
    that is contained in the javax.swing package.
  • JApplet has more functionality than Applet.
  • Applets that are subclasses of JApplet, called
    swing applets, have the general form
  •  
  • import java.awt.
  • import javax.swing.
  •  
  • public class AppletName extends JApplet
  • . . .

5
General Form of a JApplet (contd)
  • An applet overrides a set of methods that provide
    the basic mechanism by which the browser or
    applet viewer interfaces to the applet and
    controls its execution.
  • An applet does not need to override those methods
    it does not use.
  • The general structure of a swing applet is
  •   import java.awt.
  • import javax.swing.
  • public class AppletName extends JApplet
  • public void init() . . .
  •  
  • public void start() . . .
  •  
  • public void stop() . . .
  •  
  • public void destroy() . . .
  •  
  • public void paint(Graphics g ) . .
    .

6
Applet Initialization and Termination
  • When an applet begins, the AWT calls the
    following methods, in this sequence init, start,
    paint.
  • When an applet is terminated, the following
    sequence of methods is invoked stop, destroy.

7
The paint method
  • The paint method is called by the execution
    environment each time your applets output must
    be redrawn.
  • The paint method that is inherited from the class
    Applet is an empty method.
  • In order for anything to be displayed on the
    applet window, the paint method must be
    overridden, in your Applet subclass, with
    behavior to display text, graphics, and other
    things.
  • The paint method takes an object of class
    Graphics as an argument. This object is passed to
    the paint method by the execution environment.
  • public void paint(Graphics g)
  • . . .
  • It is the AWT that takes care of creating the
    Graphics object passed to paint.
  • An object of class Graphics represents a
    particular drawing surface and it contains
    methods for drawing, font manipulation, color
    manipulation, etc.

8
The paint method (contd)
  • A graphics object has a coordinate system that is
    illustrated below
  • The Graphics class has a subclass called
    Graphics2D that has more functionality than the
    Graphics class.
  • Since an object of class Graphics is passed to
    paint, to use the functionality provided by the
    Graphics2D class, the object must be converted to
    a Graphics2D object. This is done by a down-cast
  • public void paint(Graphics g)
  • Graphics2D g2 (Graphics2D) g
  • . . .

x
(0,0)
y
9
Displaying Strings in the Applet Window
  • To output a string starting at point (x, y) of an
    applet window, use the drawString method of the
    corresponding graphics object
  • void drawString(String string , int x ,
    int y)
  • Note The drawString method does not recognize
    the new line character '\n'.
  • Example The following applet displays the
    string Hello World in default colors black on
    a light gray background.
  • import javax.swing.
  • import java.awt.
  • public class HelloApplet extends JApplet
  • public void paint(Graphics g)
  • Graphics2D g2 (Graphics2D)g
  • g2.drawString("Hello world!", 50, 25)
  • This applet must be in a file called
    HelloApplet.java

10
How to place an Applet in a Web Page
  • An Applet is placed on a web (HTML) page using
    the ltAPPLETgt tag.
  • Example
  • ltAPPLET CODE "AppletName.class"
    WIDTH600 HEIGHT100gt lt/APPLETgt
  • In this example, the ltAPPLETgt tag includes three
    attributes
  • CODE Specifies the name of the applet's
    main class file
  • WIDTH Specifies the width, in pixels, of the
    applet window on the Web page.
  • HEIGHT Specifies the height of the applet
    window
  • The class file indicated by the CODE attribute
    must be in the same folder as the Web page
    containing
  • the applet, unless a CODEBASE attribute is
    used.
  • CODEBASE specifies the location of the applet
    class file relative to the applet's HTML file.
  • Example
  • ltAPPLET CODE "AppletName.class"
    CODEBASE"..\" WIDTH500 HEIGHT200gt lt/APPLETgt
  • It is customary, but not necessary, to give the
    HTML file the same name as that of the applet
    class inside.

11
Executing a Java Applet
  • A Java applet must be compiled into bytecode
    before it can be used in a Web page.
  • When a Web page containing an APPLET tag is
    opened, the associated bytecode is downloaded
    from the location specified by the CODE or
    CODEBASE attribute. This location can be in the
    local machine or in a machine across the Web.
  • A Java interpreter embedded in the
    Java-compatible Web-browser then executes the
    applet.
  • Note A Java applet can be executed by an Applet
    viewer, a utility that comes with the Java
    Software Development Kit.

12
Executing a Java Applet (contd)
  • An applet typically runs under tighter security
    constraints than an application.
  • Java distinguishes between trusted and untrusted
    applets
  • An untrusted applet cannot
  • Read or write files on the local host.
  • Load libraries or define native (i.e., non-Java)
    methods.
  • Run any programs on the local host.
  • Connect to any machine other than its code base.
Write a Comment
User Comments (0)
About PowerShow.com