1 / 23

Applets – Lecture 1, 2 & 3

Applets – Lecture 1, 2 & 3. Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI. 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

lara-kemp
Télécharger la présentation

Applets – Lecture 1, 2 & 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Applets – Lecture 1, 2 & 3 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI

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

  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. APPLET… • 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...

  6. init() start() do some work stop() destroy() 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

  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. Applet Skeleton import java.awt.*; import java.applet.*; /* <applet code="AppletSkel" width=300 height=100> </applet> */ public class AppletSkel extends Applet { // Called first. public void init() { // initialization } /* Called second, after init(). Also called whenever the applet is restarted. */ public void start() { // start or resume execution } // Called when the applet is stopped. public void stop() { // suspends execution } /* Called when applet is terminated. This is the last method executed. */ public void destroy() { // perform shutdown activities } // Called when an applet's window must be restored. public void paint(Graphics g) { // redisplay contents of window } }

  9. Simple Applet Display Methods • To output a string to an applet, use drawString( ), which is a member of the Graphics class. Typically, it is called from within paint( ). It has the following general form:void drawString(String message, int x, int y)Here, message is the string to be output beginning at x,y. • In a Java window, the upper-left corner is location 0,0.

  10. Simple Applet Display Methods… • To set the background color of an applet’s window, use setBackground( ). • To set the foreground color (the color in which text is shown, for example), use setForeground( ). • These methods are defined by Component, and they have the following general forms:void setBackground(Color newColor)void setForeground(Color newColor) • A good place to set the foreground and background colors is in the init( ) method. • Of-course, you can change these colors as often as necessary during the execution of your applet.

  11. Simple Applet Program • Here is a very simple applet that sets the background color to cyan, the foreground color to red, and displays a message that illustrates the order in which the init( ), start( ), and paint( ) methods are called when an applet starts up. import java.awt.*; import java.applet.*; public class Sample extends Applet{ String msg; public void init() { setBackground(Color.cyan); setForeground(Color.red); msg = "Inside init( ) --"; } public void start() { msg += " Inside start( ) --"; } public void paint(Graphics g) { msg += " Inside paint( )."; g.drawString(msg, 10, 30); } }

  12. Simple Applet Program… • The applet generates the window shown here:

  13. HTML APPLET TAG (Element) <applet code="..." width=xxx height=xxx ...> ... </applet> • 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

  14. Web page with an applet • Put a page like this in your project's folder: <HTML> <HEAD> <TITLE>My Applet Page</TITLE> </HEAD> <BODY> <APPLET code="mypackage/MyApplet.class" width=400 height=300> </APPLET> </BODY> </HTML>

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

  16. Useful Applet Methods… • 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 • 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)

  17. Useful Applet Methods… • getAudioClip(URL url, String fileName) • 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

  18. Sample Playing Audio Clip Program import java.applet.Applet; import java.applet.AudioClip; public class NewApplet extends Applet { public void start(){ AudioClip acl; acl = this.getAudioClip(getDocumentBase(),"M1F1-Alaw-AFsp.au"); acl.play(); } }

  19. Useful Applet Methods… 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().

  20. Requesting Repainting • repaint() method is defined by AWT. • Causes AWT runtime system to execute paint() method. • repaint() method has four forms: • void repaint()this method causes the entire window to be repainted. • void repaint(int left, int top, int width, int height)this method causes a specific region in a window to be repainted. • void repaint(long maxDelay)Here maxDelay specifies the maximum number of milliseconds that can elapse before calling repaint(). • void repaint(long maxDelay, int x, int y, int width, int height)

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

  22. Exercises • What is an applet? How does an applet differ from applications? • Explain with an example how we implement an applet into a webpage? • Explain how to set background area in applet? • What are the methods that controls an applet’s life cycle?

  23. Assignment • Write a program to load an image to your applet. • Write a program to draw a home in to your applet using Graphics methods.

More Related