Professional Documents
Culture Documents
INTRODUCTION
applets are small applications that are accessed on an Internet
compatible browser.
User I/O is not accomplished with Javas stream I/O classes.
Instead, applets use the interface provided by the AWT.
SIMPLE APPLET
import java.awt.*;
import java.applet.*;
public class SimpleApplet extends Applet {
public void paint(Graphics g) {
g.drawString("A Simple Applet", 20, 20);
}
}
IMPORT
This applet begins with two import statements. The first imports the Abstract Window
Toolkit (AWT) classes. Applets interact with the user through the AWT, not through the
console-based I/O classes. The AWT contains support for a window-based, graphical
interface.
The second import statement imports the applet package, which contains the class
Applet.
Every applet that you create must be a subclass of Applet.
paint()
The paint( ) method has one parameter of type Graphics. This parameter
contains the graphics context, which describes the graphics environment in
which the applet is running.
This context is used whenever output to the applet is required.
Inside paint( ) is a call to drawString( ), which is a member of the Graphics
class.
This method outputs a string beginning at the specified X,Y location. It has the
following general form: void drawString(String message, int x, int y)
Here, message is the string to be output beginning at x,y. In a Java window,
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.
Details-1
init( )
Details-2
paint( )
Details-3
stop( )
showStatus()
// Using the StatusWindow.
import java.awt.*;
import java.applet.*;
/*
<applet code="StatusWindow" width=300 height=50>
</applet>
*/
public class StatusWindow extends Applet{
public void init() {
setBackground(Color.cyan);
}
// Display msg in applet window.
public void paint(Graphics g) {
g.drawString("This is in the applet window.", 10, 20);
showStatus("This is shown in the status window.");
}
}
11
12
Parameter passing
// Use Parameters
import java.awt.*;
import java.applet.*;
/*
<applet code="ParamDemo" width=300 height=80>
<param name=fontName value=Courier>
<param name=fontSize value=14>
</applet>
*/
13
Contd..
public class ParamDemo extends Applet{
String fontName;
int fontSize;
float leading;
boolean active;
// Initialize the string to be displayed.
public void start() {
String param;
fontName = getParameter("fontName");
if(fontName == null)
fontName = "Not Found";
param = getParameter("fontSize");
try {
14
getDocumentBase(),getCodeBase()
Often, you will create applets that will need to explicitly load
media and text. Java will allow the applet to load data from the
directory holding the HTML file that started the applet (the
document base) and the directory from which the applets class file was
loaded (the code base).
These directories are returned as URL objects by
getDocumentBase( ) and getCodeBase( ).
15
Example..
import java.awt.*;
import java.applet.*;
import java.net.*;
/*
<applet code="Bases" width=300 height=50>
</applet>
*/
public class Bases extends Applet{
// Display code and document bases.
public void paint(Graphics g) {
String msg;
URL url = getCodeBase(); // get code base
msg = "Code base: " + url.toString();
g.drawString(msg, 10, 20);
url = getDocumentBase(); // get document base
msg = "Document base: " + url.toString();
g.drawString(msg, 10, 40);
}
}
16
Event Model
applets are event-driven programs. Thus, event handling
18
Events
Events
In the delegation model, an event is an object that describes a
19
Event Sources
Event Sources
A source is an object that generates an event.This occurs when the internal state of
that object changes in some way. Sources may generate more than one type of
event.
A source must register listeners in order for the listeners to receive
notifications about a specific type of event. Each type of event has its own
registration method.
Here is the general form:
public void addTypeListener(TypeListener el)
Here, Type is the name of the event and el is a reference to the event listener. For example,
the method that registers a keyboard event listener is called addKeyListener( ).
The method that registers a mouse motion listener is called
addMouseMotionListener( ).
20
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class context3 extends Applet
{
Button b1,b2,b3;
String s1="";
String s2="";
public void init()
{ b1=new Button("go");
b2=new Button("back");
b3=new Button("refresh");
b1.addActionListener(new But1());
b2.addActionListener(new But2());
b3.addActionListener(new But3());
setBackground(Color.yellow);//NOTE
}
public void start()
{
add(b1);
add(b2);
add(b3);
}
21
{ g.setColor(Color.blue);//NOTE
showStatus(s1);
setBackground(Color.red);
g.drawString(s2,10,200);
repaint();
}
}
class But3 implements ActionListener
{ s1="";
s2="";
setBackground(Color.yellow);
setBackground(Color.green);//NOTE
repaint();
repaint();
}
}//class
/*<applet code=context3.class height=500 width=850>
</applet>
*/
22
23
24
25
26
// Demonstrate Buttons
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="ButtonDemo" width=250
height=150>
</applet>
*/
public class ButtonDemo extends Applet implements
ActionListener {
String msg = "";
Button yes, no, maybe;
public void init() {
yes = new Button("Yes");
no = new Button("No");
maybe = new Button("Undecided");
add(yes);
add(no);
add(maybe);
yes.addActionListener(this);
no.addActionListener(this);
maybe.addActionListener(this);
SAFE- Saraswati Academy
For Excellence,Kolkata-131.
www.SafeSuccess.blogspot.in
}
27
28