Lecture 10 Applets
Lecture 10 Applets
There are two types of java programs: applets and stand-alone programs (Java applications).
An applet is a java program that runs in a java enabled web browser. To create an applet, you need to create a
class that inherits from the Applet or JApplet class. This base classes provides a number of useful methods.
Benefits of Applets
i). Easy to deploy (“web components”)
ii). No need for installation or upgrades
iii). Provide more sophisticated functionality than a web page/form
iv). Allow for proprietary client-server protocols
v). Re-use code from traditional applications
vi). Very secure
HTML
Hypertext Markup Language (HTML) is the core technology that controls what Web browsers displays on
screen. Generally, HTML are documents containing both basic formatting commands (known as markup) and
links to other information (known as hypertext or hyperlinks).
HTML Tags
HTML tags(commands) have the following form: <name_of_tag>...</name_of_tag>
<html> is the first tag of any HTML page. It indicates that the contents of the page is in HTML. HTML files
are plain text files created with any text editor. Every HTML document consists of two parts: head and body
Note: HTML is not case sensitive, i.e. tags can be in upper or lower case letters or even mixtures of cases
Syntax:
<APPLET [CODEBASE = applet_url] CODE="Name_Of_.class_File" WIDTH=Integer HEIGHT=Integer>
</APPLET>
The applet tag can be used to set several properties about the applet: - the class to load and run as an applet,
height and width, and location of class files (codebase)
Writing Applets
Writing applet involves creating a sub-class of java.applet.Applet, or javax.swing.JApplet i.e. JApplet is a top-
level Swing container hence can add usual Swing components
Example 1
import java.applet.*;
import java.awt.*;
/**
* The Hello class implements an applet that
* simply displays "Hello World!".
*/
public class Hello extends Applet {
public void init() {
}
public void paint (Graphics g) {
// Display "Hello World!"
g.drawString("Hello world!", 0, 10);
}
}
<HTML>
<HEAD>
<TITLE>A Simple Program</TITLE>
</HEAD>
<BODY>
<h1>Here is the output of my program :</h1>
<APPLET CODE=Hello WIDTH=1500 HEIGHT=250>
</APPLET>
</BODY>
</HTML>
The paint method is used to paint the screen (draw graphics) and it takes Graphics object g as a parameter as
shown below:
public void paint(Graphics g)
You need to call methods of object g to draw on an applet
g.drawString("String to draw", x, y);
Initialised
start()
Running
stop() start()
Stopped
destroy()
Destroyed
Note: Every applet needs to implement one or more of the init(), the start( ) and the paint( ) methods and are
called automatically for all applets
Example
The code below is for the program shapes .java which illustrates how shapes are drawn in java programs
import java .awt.*;
import java .applet.*;
public class DrawShapes extends Applet{
public void paint (Graphics g){
//Displays a string.
g.drawString("Below are some shapes",20,20);
//sets applets background color to yellow.
<HTML>
<HEAD>
<TITLE>A Sample Program</TITLE>
</HEAD>
<BODY>
<h1>Here is the output of my program :</h1>
<APPLET CODE=DrawShapes WIDTH=400 HEIGHT=300>
</APPLET>
</BODY>
</HTML>
Output
Example
import javax.swing.*;
public class WelcomeApplet extends JApplet {
public void init() {
add(new JLabel("Welcome to Java", JLabel.CENTER));
}
}
Example
The following is an applet and associated HTML document that would produce the output shown below when
rendered by the browser.
import java.applet.Applet;
import java.awt.*;
<HTML>
<HEAD>
<TITLE>A Simple Program</TITLE>
</HEAD>
<BODY>
<h1>Here is the output of my program :</h1>
<APPLET CODE=GraphicalDisplay WIDTH=240 HEIGHT=300>
</APPLET>
</BODY>
</HTML>