Java Applets What is Java Applet An applet

  • Slides: 9
Download presentation
Java Applets

Java Applets

What is Java Applet • An applet is a Java program that is referenced

What is Java Applet • An applet is a Java program that is referenced by a Web page and runs inside a Java-enabled Web browser. . • An applet begins execution when an HTML page that "contains" it is loaded. . • Either a Java-enabled Web browser or the appletviewer is required to run an applet. .

Applet Disadvantage • Do not read from the local (client) disk • Do not

Applet Disadvantage • Do not read from the local (client) disk • Do not write to the local (client) disk • Do not open network connections other than to the server from which the applet was loaded • Do not link to client-side C code or call programs installed on the browser machine • Cannot discover private information about the user

Applet Life Cycle 1. 2. public void init() - Called when applet is first

Applet Life Cycle 1. 2. public void init() - Called when applet is first initialized public void start() – – 3. public void paint(Graphics g) – – – 4. Called by the browser after init and start, and again whenever the browser redraws the screen, (typically when part of the screen has been obscured and then re exposed) This method is where user-level drawing is placed Inherited from java. awt. Container public void stop() – – 5. Called immediately after init and again revisited after browser left page containing applet Used to start animation threads Called when the browser leaves the page Used to stop animation threads public void destroy() – Called when applet is killed (rarely used)

Web browser-to-applet interface

Web browser-to-applet interface

Applet Template import java. applet. Applet; import java. awt. *; public class Applet. Template

Applet Template import java. applet. Applet; import java. awt. *; public class Applet. Template extends Applet { // Variable declarations. public void init() { // Variable initializations, image loading, etc. } public void paint(Graphics g) { // Drawing operations. } }

Applet HTML Template <html><head> <title>A Template for Loading Applets</title> </head> <body> <applet code="Applet. Template.

Applet HTML Template <html><head> <title>A Template for Loading Applets</title> </head> <body> <applet code="Applet. Template. class" width=120 height=60> <b>Error! You must use a Java-enabled browser. </b> </applet> </body> </html>

Applet Inheritance Hierarchy • Applet class derives from the Abstract Window Toolkit (AWT) hierarchy.

Applet Inheritance Hierarchy • Applet class derives from the Abstract Window Toolkit (AWT) hierarchy.

AWT Pre-Made UI Components • AWT supplies the following UI components: – Buttons (java.

AWT Pre-Made UI Components • AWT supplies the following UI components: – Buttons (java. awt. Button) – Checkboxes (java. awt. Checkbox) – Single-line text fields (java. awt. Text. Field) – Menus (java. awt. Menu. Item) – Containers (java. awt. Panel) – Lists (java. awt. List)