Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Java Applets 1

2 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 by visiting a web page that contains an applet program (if they have the Java runtime environment installed on their computer) For security reasons, applets run in a sandbox: they have no access to the client’s file system 2

3 3 Applet: Making Web Interactive Hello Hello Java <app= “Hello”> 4 Applet Development “hello.java” AT SUN.COM The Internet hello.class AT SUN’S WEB SERVER 2 3 1 5 Create Applet tag in HTML document Accessing from Your Organisation The browser creates a new window and a new thread and then runs the code

4 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.

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

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

8 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

9 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

10 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

11 The simplest possible applet 11 import java.applet.Applet; public class TestApplet extends Applet { } TestApplet.java TestApplet.html

12 12

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

14 Applet Life Cycle-2/2 14 public void paint(Graphics g) { System.out.println("paint called"); g.drawString("This is basic Applet", 10, 20); } } /* */

15 Passing Parameters to Applets Param.java public 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

16 HTML file 16

17 Using Images DrawImage.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);} } /* “drawImage”, width=280 height=280> */ 17


Download ppt "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."

Similar presentations


Ads by Google