Skip to content

Chapter 5 : Applets

Saket Khopkar edited this page Jan 6, 2022 · 4 revisions

Introduction:

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

  1. Building an Applet code (.java file)

  2. Creating an executable Applet (.class file)

  3. Designing a web page using HTML tags

  4. Preparing tag

  5. Incorporating tag into web page

  6. Creating HTML file

  7. Testing the applet code / Run Applet

  8. 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); 
    } 
}
  1. 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.
  1. Designing a web page using HTML tags:
  • We need to create web page that reference the Applet. Web page has three major sections.
  1. 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.
  1. 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>
  1. Creating HTML file:
  • Name this file as HelloJava.html and save in the same directory as the compiled Applet.
  1. Run Applet:
  • We are having three files:
  1. HelloJava.java Source Code
  2. HelloJava.class Byte Code
  3. 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

Applet Skeleton / Structure:

  • 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 
   } 
}
Clone this wiki locally