Sie sind auf Seite 1von 39

Applet Programming

Applets are small java programs that are primarily used in


internet computing
They can be transported over the internet from one
computer to another and run using Applet Viewer or any
web browser that supports java

When to use applets??


When we need something dynamic to be
included in the display of a web page
When we require some flash outputs
When we want to create a program and make it
available on internet for use by others on their
computers

Types
Local applet
Remote applet

Local Applet

Local Computer

Internet

Local Computer
(client)

Remote applet

Remote Computer
(server)

We can embed applets into web pages in 2 ways:

We can write our own applets and embed them


into web pages
We can download an applet from a remote
computer system and then embed it into a web
page

Applets do not use main() method for initiating the


execution of code
Applets can not be run independently
Applets can not communicate with other servers on the
network
Applets can not run any program from local computer
Applets can not read from or write to the files in local
computer

Applet Security
For security reasons the applets that are loaded
over the network have several restrictions:
An applet cannot ordinarily read or write files
on the computer that its executing on
An applet cannot make network connections
except to the host that it came from

Java.lang.Object

Java.awt.Component

Java.awt.Container

Java.awt.Panel

Chain of classes inherited by


Applet class

Java.applet.Applet

The Applet CLASS:


Every applet is an extension of the java.applet.Applet class.
The base Applet class provides methods that a derived Applet
class may call to obtain information and services from the
browser context. These include methods that do the
following:
Get applet parameters
Get the network location of the HTML file that contains the
applet
Get the network location of the applet class directory
Print a status message in the browser
Fetch an image
Fetch an audio clip
Play an audio clip
Resize the applet

Additionally, the Applet class provides an interface by which the


viewer or browser obtains information about the applet and
controls the applet's execution.
The viewer may:
request information about the author, version and copyright of
the applet
request a description of the parameters the applet recognizes
initialize the applet
destroy the applet
start the applet's execution
stop the applet's execution

Useful Applet Methods


getCodeBase, getDocumentBase
The URL of the:
Applet file - getCodeBase
HTML file - getDocumentBase

getParameter
Retrieves the value from the associated HTML PARAM element

getSize
Returns the Dimension (width, height) of the applet

showStatus
Displays a string in the status line at the bottom of the browser

12

Useful Applet Methods,


(Continued)
getBackground, setBackground
Gets/sets the background color of the applet
SystemColor class provides access to desktop colors

getForeground, setForeground
Gets/sets foreground color of applet (default color of drawing
operations)

13

Useful Graphics Methods


drawString(string, left, bottom)
Draws a string in the current font and color with the bottom left corner
of the string at the specified location
One of the few methods where the y coordinate refers to the bottom
of shape, not the top. But y values are still with respect to the top left
corner of the applet window

drawRect(left, top, width, height)


Draws the outline of a rectangle (1-pixel border) in the current color

fillRect(left, top, width, height)


Draws a solid rectangle in the current color

drawLine(x1, y1, x2, y2)


Draws a 1-pixel-thick line from (x1, y1) to (x2, y2)

14

Useful Graphics Methods,


continued
drawOval, fillOval
Draws an outlined and solid oval, where the arguments describe a
rectangle that bounds the oval

drawPolygon, fillPolygon
Draws an outlined and solid polygon whose points are defined by
arrays or a Polygon (a class that stores a series of points)
By default, polygon is closed; to make an open polygon use the
drawPolyline method

drawImage
Draws an image
Images can be in JPEG or GIF (including GIF89A) format

15

Graphics Color
setColor, getColor
Specifies the foreground color prior to drawing operation
By default, the graphics object receives the foreground
color of the window
AWT has 16 predefined colors (Color.red,
Color.blue, etc.) or create your own color,
new Color(r, g, b)
Changing the color of the Graphics object affects only
the drawing that explicitly uses that Graphics object
To make permanent changes, call the applets setForeground
method.

16

Graphics Font
setFont, getFont
Specifies the font to be used for drawing text
Set the font of the window (I.e., call the applets setFont method)
for permanent changes to the Graphics object
In JDK 1.1, only 5 fonts are available: Serif (aka TimesRoman),
SansSerif (aka Helvetica), Monospaced (aka Courier),
Dialog, and DialogInput

17

Applet life cycle


Begin
(Load Applet)

Initialization
Born

start()
stop()
Running

Idle

stopped

Display
paint()

start()
destroy()

destroyed

Dead

Exit of browser

End

Initialization state
Applet enters this state when it is first loaded.
We may do following at this stage:
create objects needed by applet
set up initial values
load images or fonts
set up colors

public void init()


{

(action)
}

Running state
Applet enters this state when the system calls start()
method of Applet class
This method is automatically called after the browser
calls the init method. It is also called whenever the user
returns to the page containing the applet after having
gone off to other pages

public void start()


{ ..
..
.. (action)
}

Idle or stopped state


This method is automatically called when the user
moves off the page on which the applet sits. It can,
therefore, be called repeatedly in the same applet.

public void stop()


{

(action)
}

Dead state
An applet is said to be dead when it is removed from
memory.
This method is only called when the browser shuts down
normally. Because applets are meant to live on an HTML
page, you should not normally leave resources behind
after a user leaves the page that contains the applet.
public void destroy()
{

(action)
}

Display state
Applet moves to this state whenever it has to perform
some output operations on the screen
Invoked immediately after the start() method, and also
any time the applet needs to repaint itself in the browser.
The paint() method is actually inherited from the
java.awt.

public void paint (Graphics g)


{
..
..
.. (display statements)
}

Graphics Behavior
Browser calls repaint method to request redrawing of
applet
Called when applet first drawn or applet is hidden by another
window and then re-exposed

repaint()
sets flag
update(Graphics g)
Clears screen, Calls paint
paint(Graphics g)
24

Steps involved in developing and


testing an applet

Building an applet code (.java file)


Creating an executable applet (. Class file)
Designing a web page using HTML tags
Preparing <APPLET> tag
Incorporating <APPLET> tag into the web page
Creating HTML file
Testing the applet code

1. Building applet code


Import java.awt.*;
Import java.applet.*;
.
public class appletclassname extends Applet
{
..
..
public void paint(Graphics g)
{
..
..
}

Example
import java.awt.*;
import java.applet.*;
public class HelloJava extends Applet
{
public void paint(Graphics g)
{
g.drawString(hello world, 10,100);
}
}

2. Creating an executable applet


Move to the directory containing the source code
and type following command
javac HelloJava.java
Compiled output file called HelloJava.class is placed
in same directory as source
If any error msg is received, then we must check for
errors, correct them and compile the applet again

3. Designing a web page


<HTML>
<!

Comment
section

>

<HEAD>

Title Tag
</HEAD>

Head
section

<BODY>
Applet Tag
</BODY>
</HTML>

Body
section

<body>
<center>
<h1> welcome to world of applets </h1>
</center>
<br>
<APPLET.>
</APPLET>
</body>

4. Applet tag
<APPLET>
CODE=hellojava.class
WIDTH=400
HEIGHT=200>
</APPLET>

5. Adding applet to HTML file


<html>
<head>
<title>
welcome to java applets
</title>
</head>
<body>
<center>
<h1> welcome to world of applet </h1>
</center>
<br>
<center>
<APPLET>
CODE=hellojav.class
WIDTH=400
HEIGHT=200>
</APPLET>
</center>
</body>
</html>

More about Applet tag

Code= AppletFileName.class
Codebase=codebase_url
Width=pixels
Height=pixels
Name= applet_instance_name
Align=Alignment
Hspace=pixels
Vspace=pixels

Passing parameters to applets


<applet.>
<param name= color value=red>
</applet>
Eg.
<param name=text value=I like java

import java.awt.*;
import java.applet.*;
//<applet code="HelloJavaParam.class" height="100" width="100"> <param
name="string" value="applet!"></applet>

public class HelloJavaParam extends Applet


{
String str;
public void init()
{
str=getParameter(string); // receiving parameter value
if(str==null)
str=java;
str=hello + str;
// using the value
}
public void paint(Graphics g)
{
g.drawString(str,10,100);
}
}

Displaying numerical values


import java.awt.*;
import java.applet.*;
public class NumValues extends Applet
{
public void paint(Graphics g)
{
int value1=10;
int value2=20;
int sum=value1+value2;
String s=sum: + String.valueOf(sum);
g.drawString(s,10,100);
}
}

Getting input from user


import java.awt.*;
import java.applet.*;
public class UserIn extends Applet
{
TextField text1,text2;
public void init()
{
text1=new TextField(8);
text2=new TextField(8);
add(text1);
add(text2);
text1.setText(0);
text2.setText(0);
}

Contd

public void paint(Graphics g)


{
int x=0,y=0,z=0;
String s1,s2,s;
g.drawString ( input a no in each box , 10,50);
try
{
s1=text1.getText();
x=Integer.parseInt(s1);
s2=text2.getText();
y=Integer.parseInt(s2);
}
catch(Exception e) { }
z=x+y;
s=String.valueOf(z);
g.drawString(the sum is: , 10,75);
g.drawString(s,100,75);
}
}

Das könnte Ihnen auch gefallen