Unit 1
Unit 1
Unit 1
In applet life cycle, there are 5 stages which are given in above figure and these stages are
represented by 5 methods.
These methods called automatically by the browser whenever required for the execution of the
applet.
No need to call these methods by the user.
Following are the methods of the life cycle :
1) init() method
2) start() method
3) paint() method
4) stop() method
5) destroy() method
All above mention methods are defined in java.applet.Applet class except paint() method.
paint() method is defined in java.awt.Container class.
1) init() :
o It is used to initialize the Applet. It is invoked only once.
o This method is called before all the other methods.
2) Start() :
o This method is automatically called after the browser call the init method. It is also called
whenever the user returns to the page containing the applet after having gone off to other
pages.
3) Stop() :
o An applet comes in idle state when its execution has been stopped either implicitly or
explicitly.
o An applet is implicitly stopped when we leave the page containing the currently running
applet.
o An applet is explicitly stopped when we call stop() method to stop its execution.
o So, this method called repeatedly in the same applet.
import java.applet.Applet;
import java.awt.*;
public class Demo extends Applet
{
public void paint(Graphics g)
{
g.drawString("welcome",200,200);
}
}
< 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>
o Attribute written in square brackets are optional.
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 appletInstanceName It specifies a name for the applet instance, which makes it
possible for 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 and The PARAM tag allows you to specify applet-specific
NAME and Value arguments in an HTML page. Applets access their attributes
VALUE with the getParameter( ) method.
//AppletParameter.html //AppletParameter.java
<HTML> import java.applet.*;
<HEAD> import java.awt.*;
<TITLE>Java Applet Example</TITLE>
</HEAD> public class AppletParameter extends Applet
<BODY> {
<APPLET CODE=" AppletParameter.class" public String myString;
WIDTH="400" HEIGHT="50"> public void init()
<PARAM NAME="Hello" {
VALUE="Hello, Welcome to Java myString = getParameter("Hello");
World :)" > }
</APPLET> public void paint(Graphics g)
</BODY> {
</HTML> g.setColor(Color.red);
g.drawString(myString, 20, 20);
}
}
Methods of Applet
Methods Description
void init( ) It Called when an applet begins execution.
void start( ) It Called by the browser when an applet should start
(or resume) execution.
void stop( ) It Called by the browser to suspend execution of the
applet. Once stopped, an applet is restarted when
the browser calls start( ).
void destroy( ) It Called by the browser just before an applet is
terminated. It released all resources allocated to the
applet.
String getAppletInfo( ) Returns a string that describes the applet.
URL getCodeBase( ) Returns the URL associated with the invoking applet.
URL getDocumentBase( ) Returns the URL of the HTML document that invokes
the applet.
String getParameter(String paramName) Returns the parameter associated with paramName.
If not found then return NULL.
String[ ] [ ] getParameterInfo( ) Returns information about the parameters that are
understood by this applet.
boolean isActive( ) Returns true if the applet has been started else
returns False.
void play(URL url) Plays the audio clip at the specified absolute URL.
void play(URL url, String clipName) Plays the audio clip is found at the location specified
by url with the name specified by clipName.
Example : A simple applet that sets the foreground, background colors, draw rectangle, fill
rectangle, get base code ,get document code and display as a string.
import java.awt.*;
import java.applet.*;
import java.net.*;
/*
<applet code="AppletMethods" width=10000 height=500 name = "HelloApplet">
</applet>
*/
public class AppletMethods extends Applet
{
String msg,displayUrl;
//set the foreground and background colors.
public void init()
{
setBackground(Color.green);
setForeground(Color.black);
msg = "Inside init method ----";
}
//Initialize the string to be displayed.
public void start()
{
msg += " Inside start method ---";
}
Output :
Methods Description
void add(Component c) inserts a component on this component.
void setSize(int width,int height) sets the width and height of the component.
void setForeground(Color) Set the foreground or background color for the
void setBackground(Color) component
Color getForeground() Get the foreground or background color for the
Color getBackground() component.
void setName(String) Set or get the name of the component
String getName()
void setEnabled(boolean) Set or get whether the component is enabled.
boolean isEnabled()
void setVisible(boolean) Set or get whether the component is visible.
boolean isVisible()
int getWidth() Get the current width or height of the component
int getHeight() measured in pixels.
int getX() Get the current x or y coordinate of the component
int getY()
void repaint() Request that all or part of the component be
void repaint(int, int, int, int) repainted.
void remove(int) Remove one of or all of the components from this
void remove(Component) container.
void removeAll()
void setLayout(LayoutManager) Set or get the component's layout manager.
LayoutManager getLayout()
Rectangle getBounds() Gets the bounds of this component in the form of
Rectangle getBounds(Rectangle) a Rectangle object.
void setBounds(int, int, int, int) Moves and resizes this component.
void setBounds(Rectangle)
Example : Write a applet program to create simple GUI consist of Button, Radio Button, TextField, Checkbox
and Label.
import java.awt.*;
import java.applet.*;
// now we will specify the positions of the GUI components. This is done by
specifying the x and y coordinate and the width and height.
submit.setBounds(100,200,100,30);
text.setBounds(20,50,150,25);
male.setBounds(20,120,100,30);
female.setBounds(120,120,100,30);
option.setBounds(20,160,100,30);
name.setBounds(20, 15, 50, 50);
// now that all is set we can add these components to the applet.
add(submit);
add(text);
Output :