Sunteți pe pagina 1din 52

APPLETS

What is an Applet ?

An applet is a dynamic and interact


Program that can run inside a Web
Displayed by a java-capable browser
Hot Java or Netscape.

APPLETS Vs APPLICATIONS
Java applications are simple and
alone programs that can run usi
Java interpreter from the comma
Java applets are run from inside
browser that supports applets.

A reference to an Applet is embe


a Web page using a special HTM
Applets have an added adv. As th
run inside java browser that pro
frame,event-handling facility,gra

Web browser Applets Interface

STRUCTURE OF A SIMPLE
APPLET
By inheriting from
java.applet.Applet the necessary
structure is defined to
coordinate with a Web browser

public class MyApplet extends


java.applet.Applet {
public MyApplet( ) { //create
applet
System.out.println("In
ctor");
}
public void init( ) { //
initialize
System.out.println("In
init");
}

public void stop( ) { // leave


page
System.out.println("In
stop");
}
public void destroy( ) {// clean
up
System.out.println("In
destroy");
}
}

My First Applet
Program
Create a java file called
firstapp.java
public class firstapp extends
java.applet.Applet { public void
init() { System.out.println("In
Panorama.init"); } }
Create a html file called
firstapp.html
<applet
code=firstapp
width=600 height=130>
</applet>

GRAPHICS,COLOR AND FONT C


In Java all graphics operations ca
performed using the java.awt pa
This package contains various cla
Methods for painting graphics an
GRAPHICS CLASS

This class contains methods for


Strings,lines,rectangles and oth
Defined in the Graphics class,wh
Part of java.awt packge.

FONT CLASS
The font class represents fonts. T
Written inside an applet using diff
fonts.
An object of Font class is created
COLOR CLASS
By using some of the methods fro
class we can further enhance the
diagram and text inside the apple
using different colors.
The foreground and background c
also be set for an applet.

LOADING AND DISPLAYING


IMAGES

A package is a named group of c


for a common domain: java.lang,
java.awt, java.util, java.io Packag
imported by other source files m
names available:
import java.awt.*; // All classes
import java.awt.Image; // One cl

The most visible interface for image


is
in Applet
Image getImage(URL) // Abs URL
Image getImage(URL, String) // Rel
URL
For Example
Image
img=getImage(getDocumentBase(),
"x.gif");
It returns a reference to an Image
object
that is being asynchronously loaded.
The most common thing to do with a

LOADING AND DISPLAYING IMAGE


STEPS TO FOLLOW
1.Above and outside the class,
add an import of java.awt
package at the start of the
file.

2. In the class at the same level a


init method, make an instance va
img, for the image to be loaded a
Its type is Image

3.In init after the output stateme


this instance variable by callingA
getImage method. Pass it the
getDocumentBase() and the strin
"Panorama.gif"

4.In the class beneath the init me


add a "paint" method. This metho
receives a Graphics object, g, as
argument.
5.In paint, output the tracing me
"In Panorama.paint".

6.In paint send the drawImage m


to g object passing the image (im
x (0), y (0), and the Applet (this)

7.Copy Panorama.gif from the lin


provided below, to the directory
you are storing your source. Com
and run. Did the image get disp
Does the paint method get calle
multiple times? It should as the
is asyncronously loaded.

EXCEPTIONS AND MEDIA TRAC


MEDIA TRACKER
* The getImage method loads
images asynchronously
* This means that users can
start interacting with a
program before it's ready or
that early phases of animation
don't look right because some
images

The MediaTracker class


keeps track of the loading of
images.
Three key methods are:
addImage: Adds image to list
of objects
being tracked.
checkAll: Checks if all images
have
finished loading.

1.In the class, make an


instance variable,
mt, that will reference a
media tracker
object. It's type is
MediaTracker.
2.In init after getting the
image, create
a new media tracker object
passing in
the Applet(this).
3.In init after creating the

4.In init after adding the


image, send to
media
tracker the message to wait for
all images (in our case only
one) to finish loading.
5.Compile. Any errors? Was it
from a missing exception, if so
continue.
Otherwise, fix your syntax
errors.
Don't forget to add a try-catch

7. In the catch, print out a trace


of the execution stack using
the exception's
printStackTrace( ) method.
6. Compile and run. Did the
image appear all at once
(not incrementally)? Does the
paint method now get called
only once? It should, because
you are synchronously loading

Example Program..
import java.awt.*;
public class Panorama extends
java.applet.Applet { Image img;
MediaTracker mt;
public void init() {
System.out.println("In Panorama.init");
img=getImage(getDocumentBase(),"Panoram
a.gif");
mt = new MediaTracker(this);
mt.addImage(img,0);
try {
mt.waitForAll();
} catch(InterruptedException e)
{ e.printStackTrace();

public void paint(Graphics g)


{ System.out.println("In
Panorama.paint");
g.drawImage(img,0,0,this);
}
}
* Create a HTML file.
<applet code =Panoroma
width=600
height = 130 </applet>

IMAGE ROTATION USING COPY


AREA
Step 1: Create buffer with
extension of
N columns.
Step 2(a): Shift image to the
right N columns filling in the
extension.
Step 2(b): Wrap extension (N
columns) back to the beginning
of the image.
Step 3: Display buffer

GRAPHICS COPYING OF PIXEL IM


* A rectangle of image's pixels
can be copied from one
location to another using the
Graphics class' copyArea
method.
* Image i = createImage(iw, ih);
Graphics gc = i.getGraphics();
gc.copyArea(x, y, w, h, dx, dy

IMAGE ROTATION
ALGORITHM
SPECIFICATION
*Create a buffer with an
extension of dx columns
i = createImage(iw + dx, ih);
*Shift the image to the right dx
pixels
gc.copyArea(0, 0, iw, ih, dx, 0);
*Wrap the extension (of dx
pixels) back to the beginning
gc.copyArea(iw, 0, dx, ih, -iw,

Add an advance method which


will later be called to animate
the image. In it, output a trace
message then rotate the image
by copying the image twice
using
copyArea
Test the advance method by
calling it in the init method
with the previously defined
shift amount of 100. Verify that
the image has been shifted by
comparing
it to output from
Example followed.

import java.awt.*; public class


copyarea extends
java.applet.Applet {
Image img;
MediaTracker mt;
int iw, ih;
Image buf;
int shift = 100;
Graphics bufg;
public void init()
{ System.out.println("In
Panorama.init"); img =
getImage(getDocumentBase(),

mt = new MediaTracker(this);
mt.addImage(img,0);
try { mt.waitForAll(); }
catch(InterruptedException e)
{ e.printStackTrace(); }
iw = img.getWidth(null);
ih = img.getHeight(null);
buf =
createImage(iw+shift,ih);
bufg = buf.getGraphics();
bufg.drawImage(img,0,0,null);
advance();

public void paint(Graphics g)


{ System.out.println("In
Panorama.paint");
g.drawImage(buf,0,0,null); }
public void advance()
{ System.out.println("In
Panorama.advance");
bufg.copyArea(0,0, iw,ih,
shift,0); bufg.copyArea(iw,0,
shift,ih, -iw,0); }
I}nsert .class file in .html file and
Program. See the result!

1.In the class, add an "advance


method to the Applet.
2.In advance, add the standard
tracing message.
3.In advance, copy the pixels in
the secondary graphics
context, bufg, and rotate the
image. Call Graphic's
copyArea twice to accomplish
this.
4.In init, call advance (to test
the shift algorithm). This will

RESULT

THREADING AND
ANIMATION
PROCESSES,
THREADS AND

ACTIVITIES

On most computer processors,


multiple processes execute
multiple
programs
A process
encapsulates many
things including:
Address space
Current state of the
computation
(machine registers, call
stack, ...)

A process can have many threads


A thread embodies only the
state(magmt.) of an activity.
An activity is a sequence of
operations (thread of control or
flow of communication)

The java.lang.Thread class is


the base for all objects that can
behave as threads.
A Thread object is
conceptually just an activity.
Such an activity needs a focal
point (i.e., what code it needs
to run)
Each Thread needs a
reference to a

To facilitate this protocol,


Java supplies a simple
interface java.lang.Runnable
public interface Runnable

AN APPLET AS RUNNABLE
Many times an Applet will
have one additional (usually
continuous) activity, e.g.,
animation loop
In such cases it is quite likely
the class will both extend
Applet and implement
Runnable class ImgJump
extends Applet

It's start method will create &


start a thread
t = new Thread(this);
t.start( );
It's stop method will stop &
nullify the thread
if ( t != null ) {
t.stop( );
t = null;
}

BASIC ANIMATION
Animation is the display of a
series of pictures over time to
give the illusion of motion.
A basic computer animation
loop consists of
Advance to the next picture
(in an offscreen buffer)
Update the screen by
repainting it
Delay for some period of time

For example,
public void run( );
while (true) {
advance( ); // advance to
the next frame
repaint( ); // repaint the
screen
try
{ Thread.sleep(33); } // delay

catch(InterupptedException e)

Example Program
import java.awt.*;
public class move extends
java.applet.Applet implements
Runnable {
Image img;
MediaTracker mt;
int iw, ih;
Image buf;
int shift = 1;
Graphics bufg;
Thread t;

public void init()


{ System.out.println("In
Panorama.init"); img =
getImage(getDocumentBase(),
"Panorama.gif");
mt = new MediaTracker(this);
mt.addImage(img,0);
try { mt.waitForAll(); }
catch(InterruptedException e)
{ e.printStackTrace(); }
iw = img.getWidth(null);
ih = img.getHeight(null);

bufg = buf.getGraphics();
bufg.drawImage(img,0,0,null);
}
public void paint(Graphics g)
{ System.out.println("InPanora
ma.paint");
g.drawImage(buf,0,0,null);
}
public void advance() {
System.out.println
("In Panorama.advance");
bufg.copyArea(0,0, iw,ih,

public void start()


{ System.out.println
("In Panorama.start");
t = new Thread(this);
t.start(); }
public void run()
{ System.out.println("In
Panorama.run"); while(true) {
advance();
repaint();
try { Thread.sleep(200); }
catch(InterruptedException e)

PASSING PARAMETERS
TO AN APPLET

Parameters can be specified in th


File using the PARAM tag to dis
your Applet.
Example
<APPLET CODE=ImgJump
WIDTH=300 HEIGHT=200>
<PARAM NAME=image
VALUE="Test.gif">
<APPLET>

An applet (in the init method


can access each PARAM tag by
using the getParameter
method.
Specify the NAME string returns the VALUE STRING.
If the NAME string is not
found, null is
returned.
Example..
String imgName=

Parameter passing Example


import java.awt.*;
public class Panorama extends
java.applet.Applet implements
Runnable {
Image img;
MediaTracker mt;
int iw, ih;
Image buf;
int shift = 1;
Graphics bufg;
Thread t;

public void init()


{ System.out.println("In
Panorama.init"); String
imgName =
getParameter("image");
if (imgName==null)
imgName = "Panorama.gif";
img =
getImage(getDocumentBase(),i
mgName); mt = new
MediaTracker(this);
mt.addImage(img,0);

e.printStackTrace(); }
iw = img.getWidth(null);
ih = img.getHeight(null);
buf = createImage(iw+shift,ih);
bufg = buf.getGraphics();
bufg.drawImage(img,0,0,null);
}
public void paint(Graphics g)
{ System.out.println("InPanora
ma.paint");
g.drawImage(buf,0,0,null); }
public void advance()

System.out.println
(InPanorama.advance);
bufg.copyArea(0,0, iw,ih,
shift,0); bufg.copyArea(iw,0,
shift,ih, -iw,0); } public void
start() { System.out.println("In
Panorama.start"); t = new
Thread(this); t.start(); }
public void run()
{ System.out.println("In
Panorama.run"); while(true)
{ advance();

try { Thread.sleep(1);
} catch(InterruptedException
e)
{ e.printStackTrace(); return; }
}}
public void update(Graphics g)
{ paint(g);
In
} HTML file insert the tag param
different
properties.
}
<applet code=Panorama
width=600
height=130><param

S-ar putea să vă placă și