Beruflich Dokumente
Kultur Dokumente
Java Programming
1
1
TCS Confidential
Applets and Event
Handling
Java Programming
Session 3 - Exception-Handling
Java Programming
2
2
TCS Confidential
Applets
Applet Basics
The Applet Architecture
Applet Initialization & Termination
Applet Restrictions
Writing a Simple Applet
Visualizing How an Applet Works
Session 3 - Exception-Handling
Java Programming
3
3
TCS Confidential
Applets
Simple Applet Display Methods
Overriding update()
Requesting Repainting - Threads
in Applets
Using the Status Window
The HTML APPLET Tag
Passing Parameters to Applets
Session 3 - Exception-Handling
Java Programming
4
4
TCS Confidential
Applet Basics
Applet
Applets are small application that
are accessed on an internet
server, Transported over the
Internet, Automatically installed
and run as part of an web
document.
Session 3 - Exception-Handling
Java Programming
5
5
TCS Confidential
Applet Basics
Component
Container
Panel
Applet
All applets
Session 3 - Exception-Handling
Java Programming
6
6
TCS Confidential
Applet Basics
Run Examples
From appletviewer
From Browser
Session 3 - Exception-Handling
Java Programming
7
7
TCS Confidential
Applet Basics
Provide support for
Applet execution (start and stop)
Load and display images
load and play audio clips.
Applets must import java.applet.*
and java.awt.*
Execution by Web browser or an
applet viewer provided by JDK
Session 3 - Exception-Handling
Java Programming
8
8
TCS Confidential
The Applet Architecture
AWT
Methods
SOP O/P
Only Few All main()
init() main() Begin with
Applet Normal
Class
Execution of
Session 3 - Exception-Handling
Java Programming
9
9
TCS Confidential
The Applet Architecture ...
This is a crucial point.
An applet should not maintain
control for an extended period.
An Additional thread must be started
for repetitive task.
Session 3 - Exception-Handling
Java Programming
10
10
TCS Confidential
The Applet Architecture ...
Second
The user initiates interaction with
an applet not the other way
round.
These interactions are sent to the
applet as events to which the
applet must respond.
Session 3 - Exception-Handling
Java Programming
11
11
TCS Confidential
The Applet Architecture ...
Control applets execution by
overriding set of methods.
init
start
stop
destroy
Session 3 - Exception-Handling
Java Programming
12
12
TCS Confidential
Applet Initialization and
Termination
Begins order
init()
start()
paint()
Termination order
stop()
destroy()
Session 3 - Exception-Handling
Java Programming
13
13
TCS Confidential
Applet Initialization and
Termination
init():
First method
Variables initialization
Called only once
Session 3 - Exception-Handling
Java Programming
14
14
TCS Confidential
Applet Initialization and
Termination
start():
It is called after init();
Called to restart an applet
User comes back to a web page
Session 3 - Exception-Handling
Java Programming
15
15
TCS Confidential
paint():
It is called each time
Restoring the applet window
Window overwritten
When applet begins execution;
paint() has one parameter of type
Graphics.
Applet Initialization and
Termination
Session 3 - Exception-Handling
Java Programming
16
16
TCS Confidential
Applet Initialization and
Termination
stop():
When a web browser leaves the
HTML document containing the
applet and goes to another page
destroy():
When the environment
determines that your applet needs
to be removed completely from
memory.
Session 3 - Exception-Handling
Java Programming
17
17
TCS Confidential
Applet Restrictions
Which are not enforced at compile
time.
Result in run-time exception.
Session 3 - Exception-Handling
Java Programming
18
18
TCS Confidential
Applet Restrictions
Applets may not
Read, write or delete files
Create or list directories
Check for the existence of a
file/properties
Session 3 - Exception-Handling
Java Programming
19
19
TCS Confidential
Applet Restrictions ...
Applets may not invoke a local
program
Create a network connection
Applets can read the file system
on the server it has been
downloaded from.
Session 3 - Exception-Handling
Java Programming
20
20
TCS Confidential
Applet Restrictions ...
These restrictions can be
overwhelming.
Be lifted for applets from a trusted
source.
Signing of an applet
Information is encoded that proves
that the applet can be trusted.
Session 3 - Exception-Handling
Java Programming
21
21
TCS Confidential
Demo of SampleApplet.java
// Writing a Simple Applet
public class SampleApplet extends Applet {
String msg;
// Initial settings.
public void init() {msg = "In init() -- ";
setBackground(Color.cyan);
setForeground(Color.red);
}
public void start(){msg += "In start() -- "; }
public void stop() {msg = ""; }
// Display msg
public void paint(Graphics g) {
msg += "In paint().";
g.drawString(msg, 50, 150); }
}
Session 3 - Exception-Handling
Java Programming
22
22
TCS Confidential
Compiling/Running an Applet
The applet subclass must be
declared as public, because it will
be accessed by code that is outside
the program.
Compile the program in the same
way used for other Java programs:
javac SampleApplet.java
There are three ways to run the
applet.
Session 3 - Exception-Handling
Java Programming
23
23
TCS Confidential
Compiling/Running an Applet
1.Executing within a Java-
compatible Web browser:
Create file apv.html
<applet code = SampleApplet
width = 200 height = 80> </applet>
Session 3 - Exception-Handling
Java Programming
24
24
TCS Confidential
Compiling/Running an Applet ...
2.Above html file can also be
executed from command line as
follows at the DOS prompt:
D:\suryoday> appletviewer
apv.html
Session 3 - Exception-Handling
Java Programming
25
25
TCS Confidential
Compiling/Running an Applet ...
3. Include the contents of the html file
at the head (after includes) of your
Java source code file.
D:\suryoday> appletviewer
MovingBanner.java
Applet Viewer: SampleApplet.class
In init() -- In start() -- In paint()
Applet
Applet started.
Session 3 - Exception-Handling
Java Programming
26
26
TCS Confidential
Visualizing How an Applet Works
Applet Code
Applet Code
Javac compiler
Javac compiler
byte
code
.class file
byte code
.class file
HTML
envelope
Hello.java
Hello.class
Hello.html
Java enabled
Browser
OUTPUT
Java enabled
Browser
OUTPUT:
Hello,World!
Java Run-time
Environment
Classes.zip
Java Run-time
Environment
Classes.zip
Session 3 - Exception-Handling
Java Programming
27
27
TCS Confidential
Applet Display Methods
void drawString(String msg, int
x, int y)
Member of Graphics class.
update() or paint().
Session 3 - Exception-Handling
Java Programming
28
28
TCS Confidential
Applet Display Methods
void setBackground(Color
newColor)
void setForeground(Color newColor)
The colors can be changed during
execution.
Session 3 - Exception-Handling
Java Programming
29
29
TCS Confidential
Applet Display Methods
Current setting of
background/foreground color can
be obtained by
getBackground()/getForeground().
Session 3 - Exception-Handling
Java Programming
30
30
TCS Confidential
Overriding update()
Change default color in the Update
methods instead of Paint method.
public void update(Graphics g) {
// Redisplay the window, here. }
public void paint(Graphics g) {
update(g); }
Session 3 - Exception-Handling
Java Programming
31
31
TCS Confidential
Requesting Repainting
One of architectural constraints
imposed on applet is that
it must quickly return control to the
AWT run-time system.
If the applet is displaying a moving
banner, how can it itself update the
window for its changing contents?
Session 3 - Exception-Handling
Java Programming
32
32
TCS Confidential
Requesting Repainting
It cannot create a loop inside paint()
Well, it can simply call repaint()
whenever it needs to update the
information to be displayed.
In turn, update() is called which then
calls paint() by default.
Since the scrolling of message is a
repetitive task, it should be performed by
a separate thread created by applet when
it is initialized.
Session 3 - Exception-Handling
Java Programming
33
33
TCS Confidential
Demo of MovingBanner.java
Threads in applets: This applet scrolls a
message, from right to left, across the applets
window with the help of a thread.
import java.awt.*;
import java.applet.*;
public class MovingBanner extends Applet implements
Runnable {
String msg = HELLO WORLD;
Thread t = null; boolean stopFlag;
public void init() { // set colors
setBackground(Color.cyan);
setForeground(Color.red);
} // end of init
Session 3 - Exception-Handling
Java Programming
34
34
TCS Confidential
MovingBanner.java ...
public void start( ) { // start thread
t = new Thread(this);
stopFlag = false; t.start();
} // end of start
public void run() { char ch;
for ( ; ; ) {
try {repaint();
Thread.sleep(250);
ch = msg.charAt(0);
msg = msg.substring(1, msg.length());
msg += ch;
if (stopFlag) break;
} catch(InterruptedException e) { }
}
}
Session 3 - Exception-Handling
Java Programming
35
35
TCS Confidential
MovingBanner.java ...
public void stop() {
stopFlag = true; t = null;
} // end of stop
public void paint(Graphics g) {
g.drawString(msg,100,100);
} // end of paint
} // end of class MovingBanner
Applet Viewer: MovingBanner.class
WORLD HELLO
Applet
Applet started.
Sample
Output:
Session 3 - Exception-Handling
Java Programming
36
36
TCS Confidential
Using the Status Window
Also output a message to status window of the
browser or applet viewer.
By calling showStatus().
Lets write paint() again:
public void paint(Graphics g) {
g.drawString(msg,10,10);
showStatus(This is shown in status
window.);
}
Applet Viewer: MovingBanner.class
HELLO WORLD
Applet
This is shown in status window.
Session 3 - Exception-Handling
Java Programming
37
37
TCS Confidential
The HTML APPLET Tag
The APPLET tag is used to start
an applet.
An applet viewer will execute
each APPLET tag in a separate
window
Browsers allow many applets on
a single page.
Syntax of Applet tag
Session 3 - Exception-Handling
Java Programming
38
38
TCS Confidential
The HTML APPLET Tag
<APPLET [CODEBASE = codebaseURL] CODE =
appletFile
[ALT = alternateText] [NAME = appletInstanceName]
WIDTH = pixels HEIGHT = pixels
[ALIGN =alignment] [VSPACE =pixels] {HSPACE
=pixels]
>
[<PARAM NAME = pname VALUE = pvalue >]
[< PARAM NAME = pname VALUE = pvalue >
if (c1!=null) c1.loop();
try { Thread.sleep(1000);
} catch(InterruptedException e) {}
i++;
if (i==pics.length) i=0;
}
}
public void paint(Graphics g) {
if (currimage != null)
g.drawImage(currimage,10,40,this);
}
}
Session 3 - Exception-Handling
Java Programming
69
69
TCS Confidential
Reducing Animation Flickering
While creating animation, a call
to repaint() method is made
The repaint() method in turn
calls update() method before
calling paint() method
The update() method clears the
screen which in turn causes
animation flickering
Session 3 - Exception-Handling
Java Programming
70
70
TCS Confidential
Reducing Animation Flickering
Animation flickering can be
reduced if the update() method s
overridden in your application
public void update(Graphics g) {
paint(g); }
Sometimes animation flickering
persists even after overriding
update() method
Session 3 - Exception-Handling
Java Programming
71
71
TCS Confidential
Reducing Animation Flickering
In such a case double-buffering
technique can be used that
eliminates flickering
In double-buffering, the entire
frame is first painted on a non-
visible area called buffer
Then this frame is printed on the
screen
Session 3 - Exception-Handling
Java Programming
72
72
TCS Confidential
Double Buffering
However, double buffering takes
a lot of memory space and isnt
always the most efficient method
to reduce flickering
The following program shows
the movement of a ball along a
trajectory
Session 3 - Exception-Handling
Java Programming
73
73
TCS Confidential
Double Buffering
import java.awt.*;
public class BallWithBuff extends java.applet.Applet
implements Runnable {
Thread t;
int xPos=5, yPos=5;
int xMove=2, yMove=4;
Session 3 - Exception-Handling
Java Programming
74
74
TCS Confidential
Double Buffering
Image offscreenImage;
Graphics offscreeng;
public void init() {
offscreenImage = createImage(getSize().width,
getSize().height );
offscreeng=offscreenImage.getGraphics(); }
public void start() {
if (t==null) {
t=new Thread(this);
t.start();
}
}
public void stop() {t=null;}
Session 3 - Exception-Handling
Java Programming
75
75
TCS Confidential
Double Buffering
public void run() {
Thread currThread =
Thread.currentThread();
while(t == currThread) {
xPos+=xMove;
yPos+=yMove;
if (yPos>=300) yMove*=-1;
if (yPos<=0) {
yMove*=-1;
xMove*=-1;
}
repaint();
try{ Thread.sleep(100);
} catch (InterruptedException e){}
}
}
Session 3 - Exception-Handling
Java Programming
76
76
TCS Confidential
Double Buffering
public void update(Graphics g) {
paint(g);
}
public void paint(Graphics g) {
offscreeng.setColor(Color.yellow);
offscreeng.fillRect(0,0,360,351);
offscreeng.setColor(Color.red);
offscreeng.fillOval(xPos,yPos,50,50);
g.drawImage(offscreenImage,0,0,this);
}
public void destroy() {
offscreeng.dispose();
}
}
Session 3 - Exception-Handling
Java Programming
77
77
TCS Confidential
Summary
Participants are familiarized with the:
Java threads - the Thread class, thread states,
priorities, synchronization, the Runnable
interface and creating threads.
Applet - architecture, initialization &
termination, restrictions, display methods,
writing simple applets, the HTML APPLET Tag,
and passing parameters to applets.
Event - classes, sources of events, event
listeners and interfaces, handling mouse and
keyboard events.
Creating animation using thread