Applet Programming
Applet Programming
Applet programming
1
Lecture Summary
• In this lecture, I will introduce you to the
techniques necessary to develop applet.
– Learning Applet basic concepts: Local and remote
applets
– How applet differ from application;
– Creating your own applet, applet tags, parameters,…
– Graphical methods, drawString, drawRect…
– Containers and components, like textbox, Labels,
CkeckBoxes used to develop applet….
Browser
Local Applet
6
Remote Applet
7
Remote applet
Local remote
computer(client) computer(server)
8
Remote applet…
• In order to locate and load a remote applet, we
must know the applet’s address on the web.
• This address is known as Uniform Resource
Locator(URL) .
• It must be specified in the applet’s HTML document
as the value of codebase attribute.
9
How applet differ from application
• Although both applet and standalone application
are java programs, there are significant difference
between them.
– Applets as previously described, are the small programs
while applications are larger programs.
– Applets don’t have the main() method while in an
application execution starts with the main method.
10
How applet differ from application…
12
Applet vs. Program Attributes
13
Applet HTML
• An applet is embedded in a standard HTML file using the
applet tag.
<html>
<head>
<title>page-title </title>
</head>
<body bgcolor=black text=white link=blue>
<applet code = "sa.class" height = 0
width = 0>
</applet>
</body>
</html>
– Applet tags should appear in the body of an HTML
document. 14
–
Applet Tag Attributes
• There are twelve attributes specific to the applet
tag.
• The three important ones are:
– code: a URL to an applet class.
– height and width: the applet real-estate height and
width in pixels (required).
15
LAB: Creating applet
• To create an applet, we have to use a class called
Applet, which is available inside the package
java.applet.
19
LAB: Creating applet…
• This assumes that the file AppletExample.class is located
in the same directory with the HTML document.
• If this is not the case, you can use another modifier,
CODEBASE, to give the URL of the directory that contains
the class file.
• The value of CODE itself is always just a file name, not a
URL.
• The code attribute of this tag indicates the class name
AppletExample and
23
Applet tag…
• An applet can obtain the parameters using the following
applet methods;
public String getParameter(String paramname)
Returns the value of the given parameter.
• If the specified paramName does not occur in any param
tag, then getParameter returns the value null.
• Parameter allow the writer of an applet to customize the
applet based on input from the writer of the HTML page.
28
Graphics methods …
• To draw hollow Rectangle of the specified width and height
starting from a particular x and y position.
method: drawRect()
syntax: drawRect(int x,int y,int width,int height)
29
Graphics methods cont’d…
• Method: fillOval()
• Syntax: fillOval(int x,int y,int width,int
height)
30
LAB: Illustrating Graphics methods
import java.applet.*;
import java.awt.*;
public class GraphicsDemo extends Applet{
public void paint(Graphics g)
{
g.setColor(Color.red);
g.drawString("Graphics demonstration",50,50);
g.setColor(Color.blue);
g.drawRect(100,100,100,100);
g.fillRect(150,200,200,200);
g.setColor(Color.yellow);
g.drawOval(80,100,90,90);
g.fillOval(200,300,300,200);
}
} 31
GUI (Containers and Components)
• There are two elements of GUI: container and
component.
• Container is for displaying components.
• Components must be displayed with in container.
• A button is an example of a component.
• Whereas a frame is an example of a container.
– To display a Button, you place it within a frame and display
the frame.
• Component is an abstract class that is the parent class of
the various GUI components of AWT:
– Button, CheckBox, Choise, Label, List…
• Containers is an abstract class that is the parent class of
the containers of AWT:
– Window, Panel.
32
Adding Components to a Container
34
Adding Components to a Container…
• Checkbox Group:
it creates a set of mutually execlusive check boxes in
which one and only one check box can be checked at
any one time.
• Constructors:
– Public checkboxGroup();
• Methods:
– Public String getSelectedCheckbox();
– Public void setSelectedCheckbox(Checkbox box);
35
Adding Components to a Container…
• Choice:
is a pop-up list of strings from which a single string can
be selected.
• Constructors:
– Public choice();
• Methods:
– Public void addItem(String item);
– Public String getItem(int index);
– Public int getSelectedIndex();
– Public String getSelectedItem();
– Public int getItemCount();
– Public void select(String str);
36
Adding Components to a Container…
• Label:
is a string of text that displayed on the container.
• Label cannot generate any event.
• Constructors:
– Public Label();
– Public Label(String label);
– Public Label(String label,int
alignment);
• Methods:
– Public void setText(String text);
– Public String getText();
37
Adding Components to a Container…
• TextField:
it creates a single-line text entry area, where user
can enter strings, modifies an existing string etc…
• Constructors:
– Public TextField();
– Public TextField(String text);
• Methods:
– Public String getText();
– Public void setText();
– Public void setEditable(boolean t);
38
LAB: Illustrating GUI in Applet
import java.applet.Applet;
import java.awt.*;
public class Controls extends Applet{
public void init()
{
Button b=new Button("Ok");
add(b);
CheckboxGroup cbg=new CheckboxGroup();
Checkbox cb1=new Checkbox("Java",cbg,true);
Checkbox cb2=new Checkbox("C++",cbg,false);
add(cb1);
add(cb2);
39
List l=new List();
l.add("CS");
l.add("IT");
l.add("EE");
l.add("CE");
add(l);
Label lb=new Label("Controls test");
add(lb);
TextField tf=new TextField(" Here we go");
add(tf);
}
}
//HTML CODE
<applet code=“Controls” width=300 height=400>
<applet>
40
Capturing an event
• You’ll notice that if you compile and run the applet above,
nothing happens when you press the buttons.
• This is where you must step in and write some code to
determine what will happen.
• For example,
– it could be a mouse click,
– a normal keyboard press or release,
– a special key press or release, the fact that the component got or
lost the focus,
– mouse movements, or drags, etc.
41
Capturing an event…
• The second argument is usually the target of the event,
which you’ll often ignore.
• The second argument is also encapsulated in the Event
object so it is redundant as an argument.
42
Capturing an event…
• Action method
public boolean action(Event event, Object o)
{
repaint();
return true;
}
43
LAB: Calculator programming using Applet
import java.applet.Applet;
import java.awt.*;
a1.setBounds(100,100,100,30);
a2.setBounds(100,160,100,30);
a3.setBounds(100,220,100,30);
t1.setBounds(210,100,100,30);
t2.setBounds(210,160,100,30);
t3.setBounds(210,220,100,30);
add(cb1);
add(cb2);
add(cb3);
add(cb4);
add(a1);
add(a2);
add(a3);
add(t1);
add(t2);
add(t3); } 46
LAB: Calculator programming using Applet
48
Applet Life cycle
• Applet runs in the browser and its lifecycle method
are called by JVM at its birth, its death and when it is
momentarily away.
49
Life cycle of an applet
Init()
New
born
state
start() stop()
Run Idle
state state
start()
destroy()
paint()
Dead
state
50
Applet Life cycle…
• When a user views a web page that contains an applet,
the following sequence of events occurs regarding the
life cycle of the applet:
1. The web browser downloads the necessary byte code from
the web server where the code is located.(this web server is
referred to as the code base).
2. The browser creates an instance of the Applet class, invoking
the default constructor.
3. The applet is displayed in the web page, with the location
and size of the applet determined by the HTML.
4. The browser invokes the init() method on the applet.
5. The browser invokes the start() method on the applet.
51
Applet Life cycle…
7. The applet is now live and running within the web page.
52
New Born state
• The life cycle of an applet is begin on that time when the
applet is first loaded into the browser and called the init()
method.
• The init() method is called only one time in the lifecycle of
an applet.
• The init() method is basically called to read the PARAM
tag in the html file.
• The init() method retrieve the passed parameter thru the
PARAM tag of html file using getParameter() method.
• All the initialization such as initialization of variables and
the objects like image, sound file are loaded in the init()
method.
• After the initialization of the init() method user can
interact with the applet.
53
New Born state…
54
Running state
• After initialization , this state will automatically occur by
invoking the start() method of applet class which again
calls the paint() method.
• The running state also occurs from idle state when the
applet is reloaded.
55
Running state…
56
Idle state
• The idle state will make the execution of the applet to be
halted temporarily.
• Applet moves to this state:
– when the currently executed applet is minimized or
– when the user switches over to another page.
• For example;
– The stop() method is called by the web browser on that time when
the user leaves one applet to go to another applet.
57
Idle state…
58
Dead state
• When the applet programs terminate, the destroy()
method is invoked which makes an applet to be in
dead state.
• The destroy() method is called only one time in the
life cycle of applet like init() method.
Syntax
public void destroy()
{
// statements
}
59
Display state
• The applet is said to be in display state when the
paint() method is called.
• This method can be used when we want to display
output in the screen.
• This method can be called any number of times.
• paint() method is must in all applets when we want to
draw something on the applet window.
• paint() method takes Graphics object as argument.
Syntax
public void paint(Graphics g) {
//statements
}
60
LAB: Program to illustrate the sequence of method call when an applet is
loaded.
import java.applet.*;
import java.awt.*;
public class AppLife extends Applet{
String msg=“the currently executing method”;
public void init()
{
msg+=“init()”;
}
public void start()
{
msg+=“start()”;
}
61
Program to illustrate the sequence of method call when an applet is loaded
cont’d…
62
Summary
• An applet is a Java program that runs in a web
browser.
• An applet is written by extending the
java.applet.Applet class.