Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Applets. 2 What is an applet? Applet: a Java program that can be inserted into a web page and run by loading that page in a browser brings web pages.

Similar presentations


Presentation on theme: "1 Applets. 2 What is an applet? Applet: a Java program that can be inserted into a web page and run by loading that page in a browser brings web pages."— Presentation transcript:

1 1 Applets

2 2 What is an applet? Applet: a Java program that can be inserted into a web page and run by loading that page in a browser brings web pages to life with interactive content, multimedia, games, and more the feature of Java that is primarily responsible for its initial popularity 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)

3 3 Applet

4 4 APPLET Loaded into a java-enabled program (Netscape, Applet Viewer) Applet is not a stand-alone program (no main method), structured to run inside another program (browser) JVM runs applications, but not applets. An applet can run as an application if it defines a main(). Applets are usually event-driven.

5 5 Does I/O through a GUI, only displays error messages with stdout. Applets subclass java.applet Security - applets barred from reading/writing files on the client, can’t load libraries, can’t make network connections (except to its host), can’t execute programs, etc... APPLET

6 6 Applet life cycle browser visits page containing an applet browser calls init on that applet, once browser calls start on that applet browser goes away from that page browser calls stop on that applet browser comes back to that page browser calls start again on that applet... browser shuts down browser calls destroy on the applet, once init() start() stop() destroy() do some work

7 7 APPLET Applet lifecycle: (methods overriden from Applet class): init(), start(), paint(g),stop(), destroy() Loading an applet an instance of the applet’s controlling class (Applet subclass) created applet initializes itself applet starts running Leaving and returning to applet page when page is left, applet stops upon page return, applet starts Applet reloading/unloading applet performs final cleanup, before reloading Browser quitting - (applet stops, performs final cleanup)

8 8 HTML APPLET Element... Required Attributes CODE Designates the filename of the Java class file to load Filename interpreted with respect to directory of current HTML page (default) unless CODEBASE is supplied WIDTH and HEIGHT Specifies area the applet will occupy Values can be given in pixels or as a percentage of the browser window width

9 9 Applet inside a web page applet programs live inside web pages when the browser reaches the web page: all necessary class files are downloaded from server, loaded onto JVM in web browser, and executed

10 10 HTML (web pages) Web pages are written in the HyperText Markup Language (HTML) Web pages have two main sections: head: header information (not shown on screen) body: actual text of the web page HTML text is placed into tags between (content)

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

12 12 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.TextField) Menus (java.awt.MenuItem) Containers (java.awt.Panel) Lists (java.awt.List )

13 13 Web page with an applet Put a page like this in your project's folder: My Applet Page

14 14 Loading images in applet public Image getImage(URL url) Loads and returns the image file at the given URL. public Image getImage(URL folder, String file) Loads the image in the given folder with the given name. Often used with getCodeBase().

15 15 Passing parameter to applet Parameters can be placed in the HTML code of your web page, which your applet can read:

16 16 Useful Applet Methods getCodeBase, getDocumentBase The URL of the: Applet file - getCodeBase HTML file - getDocumentBase getParameter Retrieves the value from the associated HTML PARAM element getSize Returns the Dimension (width, height) of the applet getGraphics Retrieves the current Graphics object for the applet The Graphics object does not persist across paint invocations

17 17 Useful Applet Methods, (Continued) showDocument (AppletContext method) getAppletContext().showDocument(...) Asks the browser to retrieve and a display a Web page Can direct page to a named FRAME cell showStatus Displays a string in the status line at the bottom of the browser getCursor, setCursor Defines the Cursor for the mouse, for example, CROSSHAIR_CURSOR, HAND_CURSOR, WAIT_CURSOR

18 18 Useful Applet Methods, (Continued) getAudioClip play Retrieves an audio file from a remote location and plays it JDK 1.1 supports.au only. Java 2 also supports MIDI,.aiff and.wav getBackground, setBackground Gets/sets the background color of the applet SystemColor class provides access to desktop colors getForeground, setForeground Gets/sets foreground color of applet (default color of drawing operations)

19 19 Summary Applet operations are restricted Applet cannot read/write local files, call local programs, or connect to any host other than the one from which it was loaded The init method Called only when applet loaded, not each time executed This is where you use getParameter to read PARAM data The paint method Called each time applet is displayed Coordinates in drawing operations are wrt top-left corner Drawing images getImage(getCodeBase(), "imageFile") to “load” drawImage(image, x, y, this) to draw


Download ppt "1 Applets. 2 What is an applet? Applet: a Java program that can be inserted into a web page and run by loading that page in a browser brings web pages."

Similar presentations


Ads by Google