Applets Differ from
to Write Applets,
Building Applet Code, Applet Life Cycle, Creating an Executable Applet, Designing a Web Page, Applet Tag, Adding Applet to HTML File, Running the Applet, More About Applet Tag, Passing Parameters to Applets, Aligning the Display, More About HTML Tags, Displaying Numerical Values, Getting Input from the User, Event Handling.
Introduction
Applet Fundamentals
Introduction ...
Applet Fundamentals
public class SimpleApplet extends Applet { public void paint(Graphics g) {
g.drawString("A Simple Applet", 20, 20);
}
}
Introduction ...
Applet Fundamentals
the class Applet. Every applet that you create must be a subclass of Applet.
Introduction ...
Applet Fundamentals
Introduction ...
Applet Fundamentals
the upper-left corner is location 0,0. The call to drawString( ) in the applet
causes the message “A Simple Applet” to be displayed beginning at
location 20,20.
Introduction ...
Applet Fundamentals
Introduction ...
Applet Fundamentals
<applet code="SimpleApplet" width=200 height=60>
</applet>
Introduction ...
Applet Fundamentals
called RunApp.html, then the following command line will run
SimpleApplet:
C:\>appletviewer RunApp.html
Introduction ...
Applet Fundamentals
However, a more convenient method exists that you can use to speed up testing. Simply include a comment at the head of your Java source code file that contains the APPLET tag.
By doing so, your code is documented with a prototype of the necessary HTML statements, and you can test your compiled applet merely by starting the applet viewer with your Java source code file. If you use this method, the SimpleApplet source file looks like this:
Introduction ...
Applet Fundamentals
Introduction ...
Applet Fundamentals
applets use the interface provided by the AWT or Swing.
Introduction
computer (web server) to another (client computers).
Introduction ...
Advantages
including Linux, Windows, Mac Os etc.
Drawback
How Applets Differ from Applications
to the local system.
Building Applet Code: An Example
//HelloWorldApplet.java import java.applet.Applet; import java.awt.*;
public class HelloWorldApplet extends Applet { public void paint(Graphics g) {
g.drawString ("Hello World of Java!",25, 25);
}
}
Embedding Applet in Web Page
<HTML>
<HEAD>
<TITLE>
Hello World Applet
</TITLE>
</HEAD>
<body>
<h1>Hi, This is My First Java Applet on the Web!</h1>
<APPLET CODE="HelloWorldApplet.class" width=500 height=400>
</APPLET>
</body>
</HTML>
Accessing Web page (runs Applet)
An Applet Skeleton / Applet Life Cycle
An Applet Skeleton / Applet Life Cycle ...
An Applet Skeleton / Applet Life Cycle ...
maximized. It is used to start the Applet.
stop or browser is minimized.
Passing Parameters to Applet
<HTML>
<HEAD>
<TITLE>
Hello W orld Applet
</TITLE>
</HEAD>
<body>
<h1>Hi, This is My First Communicating Applet on the W eb!</h1>
<APPLET CODE="HelloAppletMsg.class" width=500 height=400>
<PARAM NAME="Greetings" VALUE="Hello Friend, How are you?">
</APPLET>
</body>
</HTML>
Applet Program Accepting Parameters
//HelloAppletMsg.java import java.applet.Applet; import java.awt.*;
public class HelloAppletMsg extends Applet {
String msg;
public void init()
{
msg = getParameter("Greetings"); if( msg == null)
msg = "Hello";
}
public void paint(Graphics g) { g.drawString (msg,10, 100);
}
}
This is name of parameter specified in PARAM tag; This method returns the value of paramter.
What happen if we don’t pass parameter?
See HelloAppletMsg1.html
<HTML>
<HEAD>
<TITLE>
Hello World Applet
</TITLE>
</HEAD>
<body>
<h1>Hi, This is My First Communicating Applet on the Web!</h1>
<APPLET CODE="HelloAppletMsg.class" width=500 height=400>
</APPLET>
</body>
</HTML>
getParameter() returns null. Some default value may be used.
Displaying Numeric Values
//SumNums.java
import java.applet.Applet;
import java.awt.*;
public class SumNums extends Applet { public void paint(Graphics g) {
int num1 = 10; int num2 = 20;
int sum = num1 + num2;
String str = "Sum: "+String.valueOf(sum);
g.drawString (str,100, 125);
}
}
SumNums.html
Hello World Applet
<HTML>
<HEAD>
<TITLE>
</TITLE>
</HEAD>
<body>
<h1>Sum of Numbers</h1>
<APPLET CODE="SumNums.class" width=500 height=400>
</APPLET>
</body>
</HTML>
Interactive Applet
work in a graphical environment. Therefore,
applets treats inputs as text strings.
Interactive Applet ...
//SumNumsInteractive..java import java.applet.Applet; import java.awt.*;
public class SumNumsInteractive extends Applet { TextField text1, text2;
public void init()
{
text1 = new TextField(10); text2 = new TextField(10); text1.setText("0");
text2.setText("0");
add(text1); add(text2);
}
public void paint(Graphics g) {
int num1 = 0; int num2 = 0; int sum;
String s1, s2, s3;
g.drawString("Input a number in each box ", 10, 50);
try {
s1 = text1.getText();
num1 = Integer.parseInt(s1); s2 = text2.getText();
num2 = Integer.parseInt(s2);
}
catch(Exception e1)
{}
sum = num1 + num2;
String str = "THE SUM IS: "+String.valueOf(sum); g.drawString (str,100, 125);
}
public boolean action(Event ev, Object obj)
{
repaint(); return true;
}
}
Applet and Security
Summary
operate in Internet and Web
Applet Programming
Resources:
By: DR. SOUMI GHOSH,
ASSISTANT PROFESSOR, MAIT