2. Applet Programming
• local and remote applets
• difference between applet and application
• applet life cycle
• developing executable applet code
3. Applet Introduction
• Applets are small Java programs that are used
in Internet computing
• Applets,
– Transported over the Internet
– Automatically installed in client machine
– Run as part of a web document
– Applets can be run using a tool ‘appletviewer’
4. • Applets interact with the user through the
AWT, not through the console-based I/O
classes
• Browser runs applet in a Java Virtual Machine
(JVM)
5. local and remote applets
• Applets can be classified into two types…
– Local applets
– Remote applets
• Local applet
– Developed and stored in local system
– No need for internet connection for the local
system
– Local directories were searched to locate the
specified applet
6. • Remote applet
– Developed by someone else
– Stored on a remote computer connected to the
internet
– Our system needs an internet connection
– The applet is downloaded into our local system via
internet
– Run it on our system
7.
8. • init: This method is intended for whatever
initialization is needed for your applet. It is
called after the param tags inside the applet
tag have been processed.
• start: This method is automatically called after
the browser calls the init method. It is also
called whenever the user returns to the page
containing the applet after having gone off to
other pages.
9. • stop: This method is automatically called when
the user moves off the page on which the applet
sits. It can, therefore, be called repeatedly in the
same applet.
• destroy: This method is only called when the
browser shuts down normally. Because applets
are meant to live on an HTML page, you should
not normally leave resources behind after a user
leaves the page that contains the applet.
10. • paint: Invoked immediately after the start()
method, and also any time the applet needs
to repaint itself in the browser. The paint()
method is actually inherited from the
java.awt.
11. developing executable applet code
• Let’s begin with the simple applet shown here:
import java.awt.*;
import java.applet.*;
public class SimpleApplet extends Applet {
public void paint(Graphics g) {
g.drawString("A Simple Applet", 20, 20);
}
}
12. • compile in the same way
• there are two ways in which you can run an
applet:
– Executing the applet within a Java-compatible web
browser.
– Using an applet viewer
14. • CODEBASE is an optional attribute that
specifies the base URL of the applet code
• CODE is a required attribute that gives the
name of the file containing your applet’s
compiled .class file
• ALT tag is an optional attribute used to specify
a short text message that should be displayed
if the browser recognizes the APPLET tag but
can’t currently run Java applets
15. • NAME is an optional attribute used to specify
a name for the applet instance
• WIDTH and HEIGHT are required attributes
that give the size (in pixels) of the applet
display area
• ALIGN is an optional attribute that specifies
the alignment of the applet
• VSPACE specifies the space, in pixels, above
and below the applet
16. • HSPACE specifies the space, in pixels, on each
side of the applet
• PARAM tag allows you to specify applet-
specific arguments
17. • Some Points About Applets…
– Applets are event driven
– Applets do not use main() method
– They run in a browser
– They cannot read or write the files from the local
system
– Applets cannot run any other program from the
local computer
18. Applet class methods
• destroy()
Called by the browser or applet viewer to inform this applet that it is
being reclaimed and that it should destroy any resources that it has allocated.
• getAppletContext()
Determines this applet's context, which allows the applet to query
and affect the environment in which it runs.
• getAppletInfo()
Returns information about this applet.
• getAudioClip(URL)
Returns the AudioClip object specified by the URL argument.
• getAudioClip(URL, String)
Returns the AudioClip object specified by the URL and name
arguments.
• getCodeBase()
Gets the base URL.
19. • getDocumentBase()
Gets the document URL.
• getImage(URL)
Returns an Image object that can then be painted on the
screen.
• getImage(URL, String)
Returns an Image object that can then be painted on the
screen.
• getLocale()
Gets the Locale for the applet, if it has been set.
• getParameter(String)
Returns the value of the named parameter in the HTML tag.
20. • getParameterInfo()
Returns information about the parameters than are
understood by this applet.
• init()
Called by the browser or applet viewer to inform this applet
that it has been loaded into the system.
• isActive()
Determines if this applet is active.
• play(URL)
Plays the audio clip at the specified absolute URL.
• play(URL, String)
Plays the audio clip given the URL and a specifier that is
relative to it.
21. • resize(Dimension)
Requests that this applet be resized.
• resize(int, int)
Requests that this applet be resized.
• setStub(AppletStub)
Sets this applet's stub.
• showStatus(String)
Requests that the argument string be displayed in the "status
window".
• start()
Called by the browser or applet viewer to inform this applet that it
should start its execution.
• stop()
Called by the browser or applet viewer to inform this applet that it
should stop its execution.
22. Component class method
• void setBackground(Color newColor)
• void setForeground(Color newColor)
• Color getBackground( )
• Color getForeground( )
23. Can you?
• Explain concept of applet life cycle.
• Differentiate applet and application.
• Develop code for simple Java applets.
• Explain applet tag and its parameter.
• Use the methods of the applet and
component classes required for a basic applet.