0% found this document useful (0 votes)
14 views29 pages

Understanding Java Applets and Development

Java applets are small Java programs embedded in web pages that enhance interactivity and deliver applications over the Internet. They have specific restrictions for security, such as not using the main() method and being unable to access local files. The applet lifecycle includes states like initialization, running, display, idle, and destroyed, with specific methods invoked at each stage.

Uploaded by

technologyn387
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views29 pages

Understanding Java Applets and Development

Java applets are small Java programs embedded in web pages that enhance interactivity and deliver applications over the Internet. They have specific restrictions for security, such as not using the main() method and being unable to access local files. The applet lifecycle includes states like initialization, running, display, idle, and destroyed, with specific methods invoked at each stage.

Uploaded by

technologyn387
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Java Applets

Chapter 7
Introduction
Applets are small Java programs that are embedded in
Web pages.
They can be transported over the Internet from one
computer (web server) to another (client computers).
They transform web into rich media and support the
delivery of applications via the Internet.
Applet: Making Web Interactive and
Application
1 2 Delivery
3 4
Media 5
APPLET [Link] Create Accessing The browser
Development AT SUN’S Applet from creates
“[Link]” WEB tag in Your Organisation a new
AT SERVER HTML window and
[Link] document a new thread
and
then runs the
code

Hello Java
<app=
“Hello”> The Internet
Hello

3
Applet vs Application
Although both the Applets and stand-alone applications
are Java programs, there are certain restrictions imposed
on Applets due to security concerns:
 Applets don’t use the main() method, but when they are loaded,
automatically call certain methods (init, start, paint, stop,
destroy).
 They are embedded inside a web page and executed in browsers.
 They cannot read from or write to the files on local computer.
…cont’d…
 They cannot communicate with other servers on the network.
 They cannot run any programs from the local computer.
 They are restricted from using libraries from other languages.
The above restrictions ensures that an Applet cannot do
any damage to the local system.
Syntax of an Applet
import [Link];
import [Link].*;
public class AppletName extends Applet {
// Variable declarations.
public void init() {
// Variable initializations, image loading, etc.
}
public void paint(Graphics g) {
// Drawing operations.
}
}
Creating Applets
//[Link]
import [Link];
import [Link].*;

public class HelloWorldApplet extends Applet {


public void paint(Graphics g) {
[Link] ("Hello World of Java!",25, 25);
}
}
Embedding to HTML
<html>
<head><title>
Hello World Applet
</title></head>
<body>
<h1>Hi, This is My First Java Applet on the Web!</h1>
<applet code="[Link]" width=500 height=400>
</applet>
</body>
</html>
Accessing the webpage(runs
applet)
Applet Life cycle
Every applet inherits a set of default behaviours from the
Applet class. As a result, when an applet is loaded, it undergoes
a series of changes in its state. The applet states include:
 Initialisation – invokes init()
 Running – invokes start()
 Display – invokes paint()
 Idle – invokes stop()
 Dead/Destroyed State – invokes destroy()
…cont’d…
Begin Born init()
start() stop()

paint() Running Idle

start()
destroy()

Dead End

11
The States
 Initialization – invokes init() – only once
 Invoked when applet is first loaded.
 Running – invokes start() – more than once
 For the first time, it is called automatically by the
system after init() method execution.
 It is also invoked when applet moves from idle/stop
state to active state. For example, when we return
back to the Web page after temporarly visiting other
pages.
…cont’d…
 Display – invokes paint() - more than once
 It happens immediately after the applet enters into the
running state. It is responsible for displaying output.
 Idle – invokes stop() - more than once
 It is invoked when the applet is stopped from running. For
example, it occurs when we leave a web page.
 Dead/Destroyed State – invokes destroy() - only once
 This occurs automatically by invoking destroy() method
when we quit the browser.
Passing parameters to applet
<html>
<head><title>
Hello World Applet
</title></head><body>
<h1>Hi, This is My First Communicating Applet on the Web!</h1>
<applet code="[Link]" width=500 height=400>
<param name="Greetings" value="Hello Friend, How are you?">
</applet>
</body>
</html>
Applet code accessing
parameter
import [Link]; if( msg == null)
import [Link].*; msg = "Hello";
public class HelloAppletMsg
extends Applet { }
String msg; public void paint(Graphics g)
public void init() {
{ [Link]
msg = (msg,10, 100);
getParameter("Greetings"); }
} of parameter specified in PARAM tag;
This is name
This method returns the value of paramter.
Displaying Numeric Values
import [Link]; int sum = num1 + num2;
import [Link].*; String str = "Sum:
public class SumNums extends "+[Link](sum);
Applet {
[Link]
public void paint(Graphics g) { (str,100, 125);
int num1 = 10;
}
int num2 = 20;
}
The HTML file
<html> <applet code="[Link]" width=500
<head><title> height=400>
Hello world applet </applet>
</title></head> </body>
<body> </html>
<h1>
Sum of Numbers
</h1>
Interactive Applet
import [Link]; public void paint(Graphics g) {
import [Link].*; int num1 = 0;
public class SumNumsInteractive extends int num2 = 0;
Applet {
int sum;
TextField text1, text2;
public void init() String s1, s2, s3;
{ [Link]("Input a number in each
box ", 10, 50);
text1 = new TextField(10);
text2 = new TextField(10); try {
[Link]("0"); s1 = [Link]();
[Link]("0"); num1 = [Link](s1);
add(text1); s2 = [Link]();
add(text2); num2 = [Link](s2);
} }
catch(Exception e1){}
…cont’d…
sum = num1 + num2;
String str = "THE SUM IS: "+[Link](sum);
[Link] (str,100, 125);
}
public boolean action(Event ev, Object obj)
{
repaint();
return true;
}
}
Useful applet methods
getAudioClip,play
 Retrieves an audio file from a remote location and plays it
 JDK 1.1 supports .au only. Java 2 also supports MIDI, .aiff and .wav
getBackground, setBackground
 Gets/sets the background color of the applet
getForeground, setForeground
 Gets/sets foreground color of applet (default color of drawing
operations)
…cont’d…
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
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)
…cont’d…
 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
…cont’d…
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 ([Link], [Link], 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 applet’s setForeground method.
…cont’d…
setFont, getFont
 Specifies the font to be used for drawing text
 Determine the size of a character through FontMetrics (in Java 2 use
LineMetrics)
 Setting the font for the Graphics object does not persist to subsequent
invocations of paint
 Set the font of the window (I.e., call the applet’s 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
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
Drawing Image
Register the Image (from Draw the image (from paint)
init) [Link](image, x, y,
Image image window);
[Link](image, x, y, w, h,
=getImage(getCodeBase(),"file");
window);
Image image = getImage (url);
 May draw partial image or
 Loading is done in a separate nothing at all
thread
 Use the applet (this) for the
 If URL is absolute, then window argument
try/catch block is required
Example program
import [Link]; public void paint(Graphics g) {
import [Link].*; [Link](javaMan, 0, 0, this);
/** An applet that loads an image }
from a relative URL. */ }
public class JavaMan1 extends Applet {
private Image javaMan;
public void init() {
javaMan = getImage(getCodeBase(), "images/[Link]");

}
The End
Thanks!

You might also like