APPLET PROGRAMMING and GRAPHICS
APPLET PROGRAMMING and GRAPHICS
APPLET PROGRAMMING and GRAPHICS
PROGRAMMING
1
Objectives
• Introduction To Applet
• Introduction To AWT
2
Introduction to Applet
What is an Applet?
According to Sun:-
"An applet is a small program that is
intended not to be run on its own,
but rather to be embedded inside
another application....
3
Web Browsers Accessing a Web Page
HTML files,
applets,
images
Internet
Web Server
HTML file Response from the
web server
HTML file
HTML files,
applets,
images
Internet
Web Server
HTML file Response from the
web server
HTML file
HTML files,
applets,
images
Advantages of an Applet
• It is simple to make it work across platform.
• It runs in a sandbox, so the user does not need to trust the code, so
it can work without security approval.
• It is supported by most web browsers.
• It will cache in most web browsers, so will quick to load when
returning to a web page.
• The applet can work on all installed versions of Java rather than just
the latest plug – in version only.
• It can improve with use: after a first applet is run, the JVM is already
running and starts quickly, benefiting regular users of Java.
7
Introduction to Applet (Continued)
Disadvantages of an Applet
• It requires the default plug-in, which isn’t available by default on all
web browsers.
• It can’t startup until the JVM is running, and this may have
significant startup time the first time it is used.
• It is considered to difficult to build and design a good user interface
with than with HTML-based technologies.
8
Introduction To AWT
9
Life Cycle of an Applet
• init
To initialize the applet each time it's loaded (or reloaded).
• start
To start the applet's execution, such as when the applet's loaded or
when the user revisits a page that contains the applet.
• stop
To stop the applet's execution, such as when the user leaves the
applet's page or quits the browser.
• destroy
To perform a final cleanup in preparation for unloading.
10
Life Cycle of an Applet (Continued)
Initialization an When the applet is opened for the first time.
Applet using init()
Stopping an Applet When the applet lost focus or when the user leaves the
using stop() page.
Destroying an
Applet using When an applet is destroyed.
destroy()
11
Security Issues in Context to Applet
12
Creating Applet in Java
13
Creating Applet in Java (Continued)
import java.awt.*;
import java.applet.Applet; IsIsa an
method
objectof
that
Graphics
holds
public class MyApplet extends Applet
class,
information
use to
{ about
draw letters
painting
in
public void paint(Graphics g) an applet
It remembers
{ window
what color,
g.drawString(“Welcome To IBM”,50,50); font you are
} using.
}
14
Creating Applet in Java
15
Creating Applet in Java
16
Creating Applet in Java
<html>
<body>
<applet code=“MyApplet.class” width=200 height=200></applet>
</body>
</html>
17
Creating Applet in Java
18
Output in Appletviewer window
19
Output in Web Browser window
20
Attributes of Java Applet Tag
<HTML> <HEAD> </HEAD>
<BODY>
<APPLET
CODE
CODEBASE
HEIGHT
WIDTH
VSPACE
HSPACE
ALIGN
ALT
<PARAM NAME=“parameter_name” VALUE=“value of the parameter”>
</APPLET>
</BODY>
</HTML>
21
Attributes of Java Applet Tag
The CODE attribute is used to indicate the name of the class file
that holds the current Java applet.
The CODE attribute is used when both the .class file and the
.html file are located in the same directory.
The CODEBASE attribute indicates the pathname where the
.class file is stored.
The CODEBASE attribute is used, if you store a java file in a
directory different from an HTML file.
22
Attributes of Java Applet Tag
<HTML> <HEAD> </HEAD>
<BODY>
<APPLET
CODE
CODEBASE
HEIGHT
WIDTH
VSPACE
HSPACE
ALIGN
ALT
<PARAM NAME=“parameter_name” VALUE=“value of the parameter”>
</APPLET>
</BODY>
</HTML>
23
Attributes of Java Applet Tag
• HEIGHT
Use to specify the height of the applet display area.
The value may be given in pixels or as a percentage of the
parent element's height.
• WIDTH
Use to specify the width of the applet display area.
The value may be given in pixels or as a percentage of the
parent element's width.
24
Attributes of Java Applet Tag
<HTML> <HEAD> </HEAD>
<BODY>
<APPLET
CODE
CODEBASE
HEIGHT
WIDTH
VSPACE
HSPACE
ALIGN
ALT
<PARAM NAME=“parameter_name” VALUE=“value of the parameter”>
</APPLET>
</BODY>
</HTML>
25
Attributes of Java Applet Tag
• HSPACE
The HSPACE attribute specify the horizontal space between an
applet and the text.
The HSPACE attribute controls the space to the left and right
side of an applet.
• VSPACE
The VSPACE attribute specify the vertical space between an
applet and the text.
The VSPACE attribute controls the space above and below an
applet.
26
Attributes of Java Applet Tag
<HTML> <HEAD> </HEAD>
<BODY>
<APPLET
CODE
CODEBASE
HEIGHT
WIDTH
VSPACE
HSPACE
ALIGN
ALT
<PARAM NAME=“parameter_name” VALUE=“value of the parameter”>
</APPLET>
</BODY>
</HTML>
27
Attributes of Java Applet Tag
• ALIGN
The ALIGN attribute specifies the alignment of an applet.
You can specify various values to the ALIGN attribute, such as
LEFT, RIGHT, BOTTOM, TOP, BASELINE, MIDDLE, TEXTTOP,
ABSBOTTOM, and ABSMIDDLE.
28
Attributes of Java Applet Tag
<HTML> <HEAD> </HEAD>
<BODY>
<APPLET
CODE
CODEBASE
HEIGHT
WIDTH
VSPACE
HSPACE
ALIGN
ALT
<PARAM NAME=“parameter_name” VALUE=“value of the parameter”>
</APPLET>
</BODY>
</HTML>
29
Attributes of Java Applet Tag
30
Attributes of Java Applet Tag
<HTML> <HEAD> </HEAD>
<BODY>
<APPLET
CODE
CODEBASE
HEIGHT
WIDTH
VSPACE
HSPACE
ALIGN
ALT
<PARAM NAME=“parameter_name” VALUE=“value of the parameter”>
</APPLET>
</BODY>
</HTML>
31
Attributes of Java Applet Tag
• Passing Parameters to Applets
32
Applet Retrieving Data From A HTML File
33
Java’s coordinate system
Java uses (x,y) coordinate system
(0,0) is the top left corner (50, 0)
34
More on java.awt.Graphics class
1.
drawLine(x1,y1,x2,y2)
Used to draw lines.
(x1,y1) (x2,y2)
35
More on java.awt.Graphics class
2. drawOval(x,y,width,height)
(starting x
coordinate, starting y height
coordinate)
width
36
More on java.awt.Graphics class
3. fillOval(x,y,width,height)
(starting x
coordinate, starting y height
coordinate)
width
37
More on java.awt.Graphics class
4. drawRect(x,y,width,height)
(starting x
oordinate, starting y
coordinate) height
width
38
More on java.awt.Graphics class
5. fillRect(x,y,width,height)
Used to draw filled rectangle or square shapes with the current color.
(starting x
coordinate, starting y
coordinate)
height
width
39
More on java.awt.Graphics class
Setting a Color
Setting a Font
41
More on java.awt.Graphics class
Sample
program
using
import java.applet.Applet;
setFont()
import java.awt.*; method
public class MyApplet extends Applet {
public void paint(Graphics g) {
g.setFont(new Font(“Arial”,Font.BOLD,15);
g.drawString(“Welcome To IBM”,50,50);
}
}
42
Exercise
Write a program in Applet to draw the shapes as given below?
43
Summary
44
Summary (Continued)
• To run an applet in appletviewer, include an HTML tag as a comment.
Compile the applet using javac command and run the applet giving the
following command:
appletviewer className.java
• The parameters of an applet HTML tag are:
CODE, CODEBASE
WIDTH, HEIGHT
HSPACE, VSPACE
ALT
<PARAM> tag
• The various drawing methods in Java are:
drawString()
drawLine()
drawRect(
drawOval()
fillOval()
45