Applet intro
-
Upload
nitin-birari -
Category
Education
-
view
146 -
download
0
Transcript of Applet intro
Java Applets
By – NiTiN BiRaRi
Applet Programming
• local and remote applets• difference between applet and application• applet life cycle• developing executable applet code
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’
• 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)
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
• 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
• 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.
• 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.
• 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.
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);
}}
• 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
HTML APPLET Tag
• 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
• 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
• HSPACE specifies the space, in pixels, on each side of the applet
• PARAM tag allows you to specify applet-specific arguments
• 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
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.
• 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.
• 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.
• 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.
Component class method
• void setBackground(Color newColor)• void setForeground(Color newColor)• Color getBackground( )• Color getForeground( )
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.