java applets 1. what is an applet? an applet is a small java program that is typically embedded in a...
TRANSCRIPT
Java Applets
1
What is an applet?• An applet is a small Java program that is typically embedded in
a Web page and can be run using the applet viewer or a browser often as a plug-in.
brings web pages to life with interactive content, multimedia, games, and more
users can run applets simply byvisiting a web page thatcontains an applet program(if they have the Java runtimeenvironment installed on theircomputer)
• For security reasons, applets run in a sandbox: they have no access to the client’s file system
2
3
Applet: Making Web Interactive
HelloHello
Hello Java<app=“Hello”>
4Applet
Development “hello.java”
AT SUN.COM
The Internet
hello.class AT
SUN’SWEB SERVER
2 31 5Create Applettag in
HTMLdocument
Accessing fromYour
Organisation
The browser creates a new window and a new thread
and then runs the code
4
How Applets Differ from Applications• Although both the Applets and stand-alone applications are
Java programs, there are certain restrictions are imposed on Applets due to security concerns:– Applets don’t use the main() method, but when they are load,
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.– 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.
• Note: Applets that are not signed using a security certificate are considered to be untrusted and referred to as unsigned applets. When running on a client, unsigned applets operate within a security sandbox that allows only a set of safe operations.
The genealogy of Applet
5
java.lang.Object | +----java.awt.Component | +----java.awt.Container | +----java.awt.Panel | +----java.applet.Applet | +----javax.swing.JApplet
• The java.applet.Applet class defines following call-back methods for managing the life-cycle of the applets:– Init(): called back (only once) when the applet is first loaded to
initialize variables, resize the applet, setting up GUI components, and etc.
– start(): called back by the browser after the init() to start the applet running (similar to main() in Java application).
– stop(): called back when the user leaves the page on which the applet is running, reloads the page, or minimizes the browser.
– destroy(): called back when the applet is about to be purged from memory.
– paint(): called back when the applet drawing area must be refreshed.
6
Applet life cycle methods
7
Applet life cycle methods
Applet methods
public void init ()public void start ()public void stop ()public void destroy ()public void paint (Graphics)
Also:public void repaint()public void update (Graphics)public String getParameter(String)
8
repaint( )
• Call repaint( ) when you have changed something and want your changes to show up on the screen– You do need to call repaint() after drawing commands
(drawRect(...), fillRect(...), drawString(...), etc.)
• When you call repaint( ), Java schedules a call to update(Graphics g) which in turn calls paint()
9
update( )
• When you call repaint( ), Java schedules a call to update(Graphics g) which in turn calls paint()
• Here's what update does: public void update(Graphics g) {
// Fills applet with background color, then paint(g);}
• The update(Graphics g) clears the drawing area (i.e. fills the area with the background color) and calls paint.
10
The simplest possible applet
11
import java.applet.Applet;
public class TestApplet extends Applet { }
<applet code="TestApplet.class" width="150"height="100"></applet>
TestApplet.java
TestApplet.html
12
Applet Life Cycle-1/2
13
import java.applet.Applet;import java.awt.Graphics; public class BasicApplet extends Applet {
public void init() { System.out.println("init called");
} public void start() {
System.out.println("start called"); } public void stop(){
System.out.println("stop called");} public void destroy(){
System.out.println("destroy called"); }
Applet Life Cycle-2/2
14
public void paint(Graphics g) {System.out.println("paint called"); g.drawString("This is basic Applet", 10, 20);
} } /*<applet code="BasicApplet" width="300" height="200"> </applet>*/
Passing Parameters to Applets
Param.javapublic class Param extends Applet
{ String str; public void init()
{ str = getParameter(“string”);
str=“hello” + str;}
public void paint (Graphics g){
g.drawString(str, 10,100);}
}
15
HTML file<HTML>
<HEAD>
<TITLE></TITLE>
<BODY>
<APPLET CODE = Param.class WIDTH=400 HEIGHT=200>
<PARAM NAME=“string” VALUE “applet”>
</APPLET>
</BODY>
</HTML>
16
Using ImagesDrawImage.java
public class DrawImage extends Applet{
Image image;
public void init(){
image=getImage(getDocumentBase(), getParameter(“file”));
}
public void paint(Graphics g){
g.drawImage(image,0,0,this);}
}
/*<applet code> “drawImage”, width=280 height=280>
< param name=“file” value= “.jpg” > </applet> */
17