applet intro

23
Java Applets By – NiTiN BiRaRi

Upload: nitin-birari

Post on 27-Jan-2017

145 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Applet intro

Java Applets

By – NiTiN BiRaRi

Page 2: Applet intro

Applet Programming

• local and remote applets• difference between applet and application• applet life cycle• developing executable applet code

Page 3: Applet intro

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’

Page 4: Applet intro

• 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)

Page 5: Applet intro

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

Page 6: Applet intro

• 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

Page 7: Applet intro
Page 8: Applet intro

• 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.

Page 9: Applet intro

• 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.

Page 10: Applet intro

• 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.

Page 11: Applet intro

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);

}}

Page 12: Applet intro

• 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

Page 13: Applet intro

HTML APPLET Tag

Page 14: Applet intro

• 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

Page 15: Applet intro

• 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

Page 16: Applet intro

• HSPACE specifies the space, in pixels, on each side of the applet

• PARAM tag allows you to specify applet-specific arguments

Page 17: Applet intro

• 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

Page 18: Applet intro

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.

Page 19: Applet intro

• 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.

Page 20: Applet intro

• 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.

Page 21: Applet intro

• 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.

Page 22: Applet intro

Component class method

• void setBackground(Color newColor)• void setForeground(Color newColor)• Color getBackground( )• Color getForeground( )

Page 23: Applet intro

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.