java applets 1. what is an applet? an applet is a small java program that is typically embedded in a...

17
Java Applets 1

Upload: gregory-nelson

Post on 03-Jan-2016

229 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 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

Java Applets

1

Page 2: 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

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

Page 3: 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

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

Page 4: 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

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.

Page 5: 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

The genealogy of Applet

5

java.lang.Object | +----java.awt.Component | +----java.awt.Container | +----java.awt.Panel | +----java.applet.Applet | +----javax.swing.JApplet

Page 6: 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

• 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

Page 7: 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

7

Applet life cycle methods

Page 8: 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

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

Page 9: 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

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

Page 10: 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

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

Page 11: 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

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

Page 12: 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

12

Page 13: 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

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"); }

Page 14: 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

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>*/

Page 15: 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

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

Page 16: 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

HTML file<HTML>

<HEAD>

<TITLE></TITLE>

<BODY>

<APPLET CODE = Param.class WIDTH=400 HEIGHT=200>

<PARAM NAME=“string” VALUE “applet”>

</APPLET>

</BODY>

</HTML>

16

Page 17: 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

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