Unit 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

UNIT-1

Java Applets
Applet
• Small Java applications that can be accessed
on an Internet server, transported over
Internet, and can be automatically installed
and run as a part of a web document.
• Extends the java.applet.Applet class.
• Does not have any main() method.
• Viewed using JVM.
Applet
• To run the applet:
– Plug-in of the Web browser.
– Separate runtime environment (appletviewer).
• Designed to be embedded within an HTML
page.
• When a user views an HTML page that
contains an applet, the code for the applet is
downloaded to the user's machine.
Applet v/s Application
Applet Application
It requires some third party tool like a It called as stand-alone application as
browser to execute. application can be executed from
command prompt.
In applet main() method is not present. In application main() method is present.
It cannot access anything on the system It can access any data or software
except browser’s services. available on the system.
It requires highest security for the system It does not require any security.
as they are untrusted.
Applet starts execution after init() Applications start execution after main()
method. method.
Security Manager provided by Browser. No default Security Manager. All code is
Remote code is untrusted. trusted by default.
Lifecycle controlled by Browser. Lifecycle controlled by JRE.
Local Applet v/s Remote Applet
Local Applet Remote Applet
It is developed and stored in local system. It is developed and stored on remote
system.
The web page will search the local system The web page will require an internet
directories, find the local applet and connection to locate and load the remote
execute it. applet from the remote computer.
Execution of local applet does not require Execution of remote applet must require
internet connection. internet connection.
Example: Example:
<applet codebase="path" <applet codebase="URL "
code="xyz.class" width=120 height=120 > code="xyz.class" width=120 height=120 >
</applet> </applet>
path = Path of an applet on local system. URL = Url at which applet is located.
Applet Life Cycle
Applet Life Cycle
• 5 stages:
– init(), start(), paint(), stop(), destroy()
• Methods called automatically whenever
required for the execution of the applet.
• Methods are defined in java.applet.Applet
class except paint() method.
• paint() is defined in java.awt.Component
class.
Applet Life Cycle
• init():
– Used to initialize the Applet.
– Invoked only once.
– Called before all the other methods.
• start():
– Automatically called after the browser call the init
method.
– Also called whenever the user returns to the page
containing the applet.
– Can be called repeatedly in the same applet.
Applet Life Cycle
• stop():
– Called when an applet comes in idle state either
implicitly or explicitly.
– Implicitly stopped when we leave the page
containing the currently running applet.
– Explicitly stopped when we call stop() method to
stop its execution.
– Can be called repeatedly in the same applet.
Applet Life Cycle
• destroy():
– Called when the browser shuts down normally.
– Called only once.
– Called just before an applet object is removed
from the memory.
Applet Life Cycle
• paint():
– Invoked immediately after the start() method.
– Also, any time the applet needs to repaint itself.
– Provides Graphics class object that can be used for
drawing oval, rectangle etc.
Applet Life Cycle Example
import java.awt.*;
import java.applet.*;
public class AppletMethods extends Applet
{
String msg;
public void init()
{
msg = "Inside init method ----";
}
public void start()
{
msg += " Inside start method ---";
}
Applet Life Cycle Example
public void paint(Graphics g)
{
showStatus(“Painting”);
g.drawString(msg, 20, 10);
}
public void stop()
{
msg += "Inside stop method ----";
}
public void destroy()
{
msg += "Inside destroy method ----";
}
}
Run an Applet
• Two ways to run an applet :
– Using HTML file.
– Using AppletViewer tool.
Using HTML File
// Demo.java //Applet.html
import java.applet.Applet; <html>
import java.awt.Graphics; <body>
public class Demo extends Applet <applet code="Demo.class"
{ width="300" height="300">
public void paint(Graphics g) </applet>
{ </body>
g.drawString("welcome",200,200); </html>
}
}
Using Appletviewer Tool
// Demo.java
import java.applet.Applet;
import java.awt.Graphics;
/*
<applet code="Demo.class" width="300" height="300">
</applet>
*/
public class Demo extends Applet
{
public void paint(Graphics g) On Command Prompt:
{ > javac Demo.java
g.drawString("welcome",200,200); > appletviewer Demo.java
}
}
Other Applet Methods
Method Description
void setBackground (Color colorname) Set Background of an applet.
void setForeground (Color colorname) Set Foreground of an applet.
void showStatus (String str) To display status message in the status bar.
URL getDocumentBase() Returns the URL of the HTML document that
invokes the applet.
URL getCodeBase() Returns the URL associated with the invoking
applet.
String getAppletInfo( ) Returns a string that describes the applet.
void resize(int width, int height) Resizes the applet according to the
dimensions specified by width and height.
Applet Tag
• Used for embedding a Java applet within an
HTML document.
• Syntax: (Attribute in square brackets are optional.)
< APPLET
[CODEBASE = codebaseURL]
CODE = appletFile
[ALT = alternateText]
[NAME = appletInstanceName]
WIDTH = pixels
HEIGHT = pixels
[ALIGN = alignment]
[VSPACE = pixels]
[HSPACE = pixels] >
[< PARAM NAME = AttributeName VALUE = AttributeValue>]
[< PARAM NAME = AttributeName2 VALUE = AttributeValue>] . . .
[HTML Displayed in the absence of Java]
</APPLET>
Applet Tag Attributes
Attribute Value Description
CODEBASE url URL of the directory or folder that contains the applet code.
CODE .Class file Name of the file that contains the applet's compiled Applet subclass.
ALT alternateText It specifies any text that should be displayed if the browser
understands the APPLET tag but can't run Java applets.
NAME appletInstance It specifies a name for the applet instance, which makes it possible for
Name applets on the same page to find (and communicate with) each other.
WIDTH pixels It givea the initial width (in pixels) of the applet display area.
HEIGHT pixels It give a the initial height (in pixels) of the applet display area.
ALIGN alignment It specifies the alignment of the applet.
VSPACE pixels It specifies the number of pixels above and below the applet
HSPACE pixels It specifies the number of pixels on each side of the applet
PARAM Attribute Name The PARAM tag allows you to specify applet-specific arguments in an
NAME and and Value HTML page. Applets access their attributes with the getParameter( )
VALUE method.
Passing Parameters to Applet
//AppletParameter.java //AppletParameter.html <HTML>
import java.applet.*; <HEAD>
import java.awt.*;
<TITLE> Java Applet Example</TITLE>
public class AppletParameter extends Applet
{ </HEAD>
public String myString; <BODY>
public void init() <APPLET CODE =
{ "AppletParameter.class"
myString = getParameter("Hello"); WIDTH="400" HEIGHT="50">
}
<PARAM NAME="Hello" VALUE="Hello,
public void paint(Graphics g)
Welcome to Java World :)" >
{
g.setColor(Color.red); </APPLET>
g.drawString(myString, 20, 20); </BODY>
} </HTML>
}

You might also like