Chapter 1 : Java Applet
Objectives
To define an applets To describe the support for applets by Java To see how applet differ from other application To understand the lifecycle of applet To implement applet
Applet is
Embedded java class in HTML and is downloaded and executed by web browser. Applets are not stand-alone programs, can run either by web browser or applet viewer How does it differ from an application? Executed by web browser Does not using main() method
Applet Security Restriction
Most browser prevent the following
Runtime execution of another program File I/O Call to any native methods Attempts to open a socket to any system except the host that provide the applet
How Java guarantee applet is safe?
Applet Secure Environment
Java provides means of secure environment to run applet at clients site. A remote system checks 2 things
The originating URL and signature of applet it downloads
It checks against a local file containing entries mapping special applets to special protection domain.
This enables special applet coming from particular places to run with special privileges
5
Applet Types Applet Types
1. Based directly on the Applet class ( using Abstract Window Toolkit - AWT) - It provides the graphic user interface (or use no GUI at all) - This style of applet has been available since Java was first created 2. Based on Swing class (JApplet)
Applet Class Hierarchy
java.lang.Object
java.awt.Component java.awt.Container
java.awt.Window
java.awt.Panel
java.awt.Frame
java.applet.Applet
javax.swing.JApplet
7
Writing An Applet
import javax.swing.JApplet; Extends to Applet class
public class HelloWorld extends JApplet
Applet class must be public Its name must match the file name
Example : HelloWorld.java.
Embed Java class into .html file using applet tag
<applet code=className width=pixels height=pixels> </applet>
Key Applet Methods
init()
Called when the applet is created or recreated
start()
Called when the applet become visible
paint(Graphics g)
Called when the component is exposed
stop()
Called when the applet become invisible
Browser is minimized or link follows to another URL
destroy()
Called when the browser is closed or exited
Loading An Applet
http://someLocation/file.html 1. The browser loads the URL
<html> <applet code = > </applet>
2. The browser loads the html document
Applet Classes Browser Location:
3. The browser loads the applet classes
http://someLocation/file.html
4. The browser runs the applet
Loading
10
Applet Life Cycle
Enter web page Create applet
init() after init start() exits destroy() return to page stop() Go to another page
11
Applet Display
Applets are graphical in nature. The browser environment calls the paint() method.
import java.awt.Graphics; import javax.swing.JApplet; public class HelloWorld extends JApplet { private int paintCount; public void init(){ paintCount = 0; } public void paint (Graphics g) { g.drawString("Hello World", 25,25); ++paintCount; g.drawString("Number of times paint called : " + paintCount, 25, 50); } } 12
Applet Display
HelloWorld.html <HTML> <applet code = "HelloWorld" width = 300 height = 100> </applet> </HTML>
C:\src\Lecture 8> javac HelloWorld.java C:\src\Lecture 8> appletviewer HelloWorld.html
13
Viewing Applet
Viewing using appletviewer
appletviewer HelloWorld.html
Viewing using web browser
Address : C:\src\Lecture 8\HelloWorld.html
14
<applet> Tag Structure
<applet [archive = archiveList] code = appletFile.class width = pixels height = pixels [codebase = codeBaseURL] [alt = alternateText] [name = appletInstanceName] [align = alignment] [vspace = pixels] [hspace = pixels] > [<param name = appletAttribute value = value>] </applet>
15
Passing Parameter to Applet
To passed parameter to an applet
Declare the parameter in the HTML file
Using <param>
<param name = appletAttribute value = value>
Read the parameter during applet initialization
Using getParameter()
16
DisplayMessage.java Example Code
import javax.swing.JApplet; import java.awt.Graphics; public class DisplayMessage extends JApplet { String text; int x,y; public void init() { text = getParameter("Message"); x = Integer.parseInt(getParameter("x")); y = Integer.parseInt(getParameter("y")); }
public void paint(Graphics g){ g.drawString(text, x,y); }
}
17
DisplayMessage.html Example Code
<html> <applet code="DisplayMessage.class" width=200 height=50> <param name = Message value = "Java is fun!"> <param name = x value = 20> <param name = y value = 30> </applet> </html>
18
Running Applet as Application
Applet can be converted to an application without loss of functionality. Implement main() method to enable the applet run as application.
19
Example Code
import import import import java.awt.Graphics; java.awt.BorderLayout; javax.swing.JApplet; javax.swing.JFrame; public class HelloWorldApp extends JApplet { private int paintCount; public void init(){ paintCount = 0; }
public void paint (Graphics g) { g.drawString("Hello World", 25,25); ++paintCount; g.drawString("Number of times paint called : + paintCount, 25, 50); }
public static void main (String args[]) { JFrame frame = new JFrame("HelloWorldApp Demo"); HelloWorldApp app = new HelloWorldApp(); frame.add(app,BorderLayout.CENTER); app.init(); app.start(); frame.setSize(300,300); frame.setVisible(true); }
}
20
The End