-
Notifications
You must be signed in to change notification settings - Fork 3
Chapter 5 : Applets
Saket Khopkar edited this page Jan 6, 2022
·
4 revisions
- Applets are small Java programs that are mainly used in Internet computing.
- Applets can be transported over Internet from one computer to another and run using Applet Viewer or any Web Browser that supports Java.
- Applet can do many things like arithmetic operations, display graphics, play sounds, accept user input, create animation and play interactive games.
- Java Applets revolutionized the way Internet users retrieve and use documents on WWW. It enabled them to create and use fully interactive multimedia Web Documents. A web page can now contain not only a simple text or a static image but also a java applet which, when run, can produce graphics, sounds and moving images.
- Java Applets therefore have begun to make a significant impact on the WWW.
Steps To Develop And Test Applets:
-
Building an Applet code (.java file)
-
Creating an executable Applet (.class file)
-
Designing a web page using HTML tags
-
Preparing tag
-
Incorporating tag into web page
-
Creating HTML file
-
Testing the applet code / Run Applet
-
Building An Applet Code (.java file):
import java.awt.*;
import java.applet.*;
---------------
---------------
public class AppletClassName extends Applet
{
---------------
---------------
public void paint (Graphics g)
{
--------------- // Applet operations code ---------------
}
}
// Example:
import java.awt.*;
import java.applet.*;
public class HelloJava extends Applet
{
public void paint (Graphics g)
{
g.drawString (“Hello Java”, 10,100);
}
}
- Creating an executable Applet (.class file): It is obtained by compiling the source code. Above HelloJava.java source code file can be compile as follows:
- Move to directory containing source code and type: javac HelloJava.java
- Compiled HelloJava.class file is placed in the same directory as the source.
- If error message received, then we must check and correct errors and compile Applet again.
- Designing a web page using HTML tags:
- We need to create web page that reference the Applet. Web page has three major sections.
- Preparing tag:
<Applet code=HelloJava.class width=400 height=200> </Applet>
- This HTML code tells the browser to load the compiled Java applet HeloJava.class, which is in the same directory as the HTML File. And also specifies the display area for the Applet output as 400 pixels width and 200 pixels height.
- Incorporating tag into web page:
<HTML>
<HEAD>
<TITLE> Welcome To Java Applet </TITLE>
</HEAD>
<BODY>
<Applet code=HelloJava.class width=400 height=200> </Applet>
</BODY>
<HTML>
- Creating HTML file:
- Name this file as HelloJava.html and save in the same directory as the compiled Applet.
- Run Applet:
- We are having three files:
- HelloJava.java Source Code
- HelloJava.class Byte Code
- HelloJava.html Web Page File
- To run applet we require either Java Enabled Web Browser or Java Applet viewer.
- To Run Applet Type: appletviewer HelloJava.html
- Most Applets override a set of methods that provides the basic mechanism to the applet and controls its execution. Four of these methods, init( ), start( ), stop( ), and destroy( ) are defined by Applet.
- Another, paint( ), is defined by the AWT Component class. Default implementations for all of these methods are provided.
- Applets do not need to override those methods which they do not use.
// These five methods can be assembled into the skeleton shown here:
import java.awt.*;
import java.applet.*;
/*
<applet code="AppletSkel" width=300 height=100>
</applet>
*/
public class AppletSkel extends Applet
{
// Called first.
public void init()
{
// initialization
}
/* Called second, after init(). Also called whenever the applet is restarted. */
public void start()
{
// start or resume execution
}
// Called when the applet is stopped.
public void stop()
{
// suspends execution
}
/* Called when applet is terminated. This is the last method executed. */
public void destroy()
{
// perform shutdown activities
}
// Called when an applet's window must be restored.
public void paint(Graphics g)
{
// redisplay contents of window
}
}
- Born or Initialization State: Applets enters the initialization state when it is first loaded. It is achieved by init() method of Applet class. Then applet is born.
- Running State: Applet enters the running state when the system calls the start() method of Applet class. This occurs automatically after the applet is initialized.
- Idle State: An applet becomes idle when it is stopped from running. Stopping occurs automatically when we leave the page containing the currently running applet. This is achieved by calling the stop() method explicitly.
- Dead State: An applet is said to be dead when it is removed from memory. This occurs automatically by invoking the destroy() method when we want to quit the browser.
Applet Life Cycle Methods / Applet Initialization and Termination:
- When an applet begins, the AWT calls the following methods, in this sequence:
- init( )
- start( )
- paint( )
- When an applet is terminated, the following sequence of method calls takes place:
- stop( )
- destroy( )
init( ):
- It is the first method to be called. Here we should initialize variables. This method is called only once during the run time of applet.
public void init()
{
Method body
}
start( ):
- It is called after init( ). It is also called to restart an applet. Whereas init( ) is called only once. start( ) is called each time an applet is displayed onscreen. So, if a user leaves a web page and comes back, the applet resumes execution at start( ).
public void start()
{
Method body
}
paint( ):
- It is called each time our applet’s output must be redrawn. The paint( ) method has one parameter of type Graphics. This parameter will contain the graphics context, which describes the graphics environment in which the applet is running.
public void paint(Graphics g)
{
Method body
}
stop( ):
- It is called when a web browser leaves the HTML document containing the applet or when it goes to another page. We should use stop( ) to suspend threads that don’t need to run when the applet is not visible. We can restart them when start( ) is called if the user returns to the page.
public void stop()
{
Method body
}
destroy( ):
- The destroy( ) method is called when applet needs to be removed completely from memory. At this point, we should free up any resources the applet may be using. The stop( ) method is always called before destroy( ).
public void destroy()
{
Method body
}
< 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>
- CODEBASE :- Specifies the URL of the directory in which the applet is stored. If applet is stored in the same directory as the HTML file, then CODEBASE will be ommitted.
- CODE :- Specifies the name of the applet class to be loaded. It is the name of already compiled .class file in which the executable Java bytecode for the applet is stored.
- ALT :- Specifies a short text message that should be displayed if the browser understands the APPLET tag but can’t currently run Java applets.
- NAME :- Specifies a name for the applet. Applets must be named so other applets on the same page can find and communicate with them. To obtain an applet by name, use getApplet( ) method.
- WIDTH and HEIGHT :- Specifies the size (in pixels) of the applet display area.
- ALIGN :- Specifies the alignment of the applet. Possible alignment values: LEFT, RIGHT, TOP, BOTTOM, MIDDLE, BASELINE, TEXTTOP, ABSMIDDLE, and ABSBOTTOM.
- VSPACE and HSPACE :- VSPACE specifies the space, in pixels, above and below the applet. HSPACE specifies the space, in pixels, on each side of the applet.
- PARAM NAME and VALUE :- Specifies applet specific arguments in an HTML page. Applets access their attributes with the getParameter( ) method.