applets applets · java applet applet is a special type of program that is embedded in the webpage...
TRANSCRIPT
APPLETS
Applets:
Concepts of Applets
Differences between applets and applications
Life cycle of an applet
Types of applets
Creating applets
Passing parameters to applets
Graphics class.
Reference: Herbert Schildt, "Java the complete reference," TMH, Seventh Edition, 2007. 1
Java Applet
Applet is a special type of program that is embedded in the
webpage to generate the dynamic content. It runs inside the
browser and works at client side.
Advantage of Applet
There are many advantages of applet. They are as follows:
• It works at client side so less response time.
• Secured
• It can be executed by browsers running under many
platforms, including Linux, Windows, Mac Os etc.
Drawback of Applet
• Plugin is required at client browser to execute applet.
2
3 Fig. 1 Hierarchy of Applet
Applet class extends Panel. Panel class extends Container which is the subclass of Component.
Cont..
4
Built using one of general definitions of applets
Applet class
JAapplet class
Java applets are usually graphical
• Draw graphics in a defined screen area
• Enable user interaction with GUI elements
Applets are Java programs that can be embedded in HTML
documents
• To run an applet you must create a .html file which
references the applet
• Ready to Program also will run an applet
When browser loads Web page containing applet
• Applet downloads into Web browser
• begins execution
Can be tested using appletviewer program
Cont..
5
Java Applet Classes
Abstract Windowing Toolkit AWT
Earlier versions of Java
Applet class is one of the AWT components
Java Foundation Classes JFC
Extension to Java in 1997
Has a collection of Swing components for enhanced GUIs
Swing component classes begin with J
Cont..
6
Differences between applets and applications
Application Applet
Applications are stand alone programs:
executed with Java interpreter
Applet is a small program: can be placed on a web page. will be executed by the web browser. give web pages “dynamic content”.
Object class extended JApplet class extended
Class not declared public class declared to be public
Has a main() init() instead of main()
static keyword used init() not declared with static keyword
7
Life cycle of an applet
• Applet is initialized.
• Applet is started.
• Applet is painted.
• Applet is stopped.
• Applet is destroyed.
8
Lifecycle methods for Applet:
The java.applet.Applet class 4 life cycle methods and
java.awt.Component class provides 1 life cycle methods for
an applet.
java.applet.Applet class
1. public void init(): is used to initialized the Applet. It is
invoked only once.
2. public void start(): is invoked after the init() method or
browser is maximized. It is used to start the Applet.
3. public void stop(): is used to stop the Applet. It is invoked
when Applet is stop or browser is minimized.
4. public void destroy(): is used to destroy the Applet. It is
invoked only once.
Cont..
9
java.awt.Component class
1. public void paint(Graphics g): is used to paint the Applet.
It provides Graphics class object that can be used for drawing
oval, rectangle, arc etc.
2. void drawString(String message, int x, int y)
void setBackground(Color newColor)
void setForeground(Color newColor)
Color getBackground( )
Color getForeground( )
Cont..
10
11
Creating applets
Applets embedded in a web page
Executed when web page loaded by browser
Web pages structured with HTML codes
HyperText Mark-up Language
Syntax
<command>
. . .
</command>
Turns format on
Turns the format off
12
• Embedding Java applets
– Insert applet tags
<APPLET>
</APPLET>
• Call the specific applet by its file name
<APPLET CODE = "Whatever.class"
WIDTH = nnn HEIGHT = mmmm>
<\APPLET>
Where nnn and mmm are specific pixel sizes
Cont..
13
• Create the web page code
using a text editor
• Save it with an .html suffix
• Open this file with
appletviewer or with a web
browser that supports Java
• Java Plug-in must be installed
(part of J2SDK 1.4.1 from
Sun)
<HTML>
<HEAD>
</HEAD>
<BODY>
<APPLET CODE = . . . >
</APPLET>
</BODY>
</HTML>
Cont..
14
//First.java import java.applet.Applet; import java.awt.Graphics; public class First extends Applet{ public void paint(Graphics g){ g.drawString("welcome",150,150); } }
A Simple Java Applet
//myapplet.html <html> <body> <applet code="First.class" width="300" height="300"> </applet> </body> </html>
Cont..
>javac First.java >Open any browser run the myapplet.html
15
Output Cont..
16
//First.java import java.applet.Applet; import java.awt.Graphics; public class First extends Applet{ public void paint(Graphics g){ g.drawString("welcome to applet",150,150); } } /* <applet code="First.class" width="300" height="300"> </applet> */ To execute the applet by appletviewer tool, write in command prompt: c:\>javac First.java c:\>appletviewer First.java
Simple example of Applet by appletviewer tool Cont..
17
Output Cont..
18
Passing parameters to applets
<APPLET
CODEBASE = codebaseURL
ARCHIVE = archiveList
CODE = appletFile ...or... OBJECT = serializedApplet
ALT = alternateText
NAME = appletInstanceName
WIDTH = pixels
HEIGHT = pixels
ALIGN = alignment VSPACE = pixels HSPACE = pixels >
<PARAM NAME = appletAttribute1 VALUE = value>
<PARAM NAME = appletAttribute2 VALUE = value> . . .
alternateHTML
</APPLET>
19
public String getParameter(String parameterName)
UseParam.java import java.applet.Applet; import java.awt.Graphics; public class UseParam extends Applet { public void paint(Graphics g) { String str=getParameter("msg"); g.drawString(str,50, 50); } }
myapplet.html <html> <body> <applet code="UseParam.class" width="300" height="300"> <param name="msg" value="Welcome to applet"> </applet> </body> </html>
Cont..
20 AppletContext Interface
21
Displaying Graphics in Applet java.awt.Graphics class provides many methods for graphics programming. Commonly used methods of Graphics class:
public abstract void drawString(String str, int x, int y): is used to draw the specified string. public void drawRect(int x, int y, int width, int height): draws a rectangle with the specified width and height.
public abstract void fillRect(int x, int y, int width, int height): is used to fill rectangle with the default color and specified width and height.
public abstract void drawOval(int x, int y, int width, int height): is used to draw oval with the specified width and height.
public abstract void fillOval(int x, int y, int width, int height): is used to fill oval with the default color and specified width and height.
22
public abstract void drawLine(int x1, int y1, int x2, int y2): is used to draw line between the points(x1, y1) and (x2, y2). public abstract boolean drawImage(Image img, int x, int y, ImageObserver observer): is used draw the specified image. public abstract void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle): is used draw a circular or elliptical arc. public abstract void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle): is used to fill a circular or elliptical arc. public abstract void setColor(Color c): is used to set the graphics current color to the specified color. public abstract void setFont(Font font): is used to set the graphics current font to the specified font.
Cont..
23
import java.applet.Applet;
import java.awt.*;
public class GraphicsDemo extends Applet{
public void paint(Graphics g){
g.setColor(Color.red);
g.drawString("Welcome",50, 50);
g.drawLine(20,30,20,300);
g.drawRect(70,100,30,30);
g.fillRect(170,100,30,30);
g.drawOval(70,200,30,30);
g.setColor(Color.pink);
g.fillOval(170,200,30,30);
g.drawArc(90,150,30,30,30,270);
g.fillArc(270,150,30,30,0,180);
}
}
GraphicsDemo.java
24
myapplet.html
<html>
<body>
<applet code="GraphicsDemo.class" width="300" height="300">
</applet>
</body>
</html>