Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Apple II graphics wikipedia , lookup
Color vision wikipedia , lookup
Stereo display wikipedia , lookup
Anaglyph 3D wikipedia , lookup
List of 8-bit computer hardware palettes wikipedia , lookup
Framebuffer wikipedia , lookup
Hold-And-Modify wikipedia , lookup
Color Graphics Adapter wikipedia , lookup
Applets – Lecture 1, 2 & 3 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI 1 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) 2 Applet 3 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. 4 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... 5 Applet life cycle browser visits page containing an applet browser goes away from that page browser calls init on that applet, once browser calls start on that applet browser calls start again on that applet ... browser shuts down start() browser calls stop on that applet browser comes back to that page init() browser calls destroy on the applet, once do some work stop() destroy() 6 APPLET Applet lifecycle: (methods overriden from Applet class): init(), start(), paint(g),stop(), destroy() Loading an applet Leaving and returning to applet page when page is left, applet stops upon page return, applet starts Applet reloading/unloading an instance of the applet’s controlling class (Applet subclass) created applet initializes itself applet starts running applet performs final cleanup, before reloading Browser quitting - (applet stops, performs final cleanup) 7 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 } } 8 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. 9 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. 10 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); } } 11 Simple Applet Program… The applet generates the window shown here: 12 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 13 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> 14 Useful Applet Methods getCodeBase, getDocumentBase The URL of the: Applet file - getCodeBase HTML file - getDocumentBase getParameter getSize Retrieves the value from the associated HTML PARAM element 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 15 Useful Applet Methods… showStatus getCursor, setCursor Defines the Cursor for the mouse, for example, CROSSHAIR_CURSOR, HAND_CURSOR, WAIT_CURSOR getBackground, setBackground Displays a string in the status line at the bottom of the browser 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) 16 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 17 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(),"M1F1Alaw-AFsp.au"); acl.play(); } } 18 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(). 19 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) 20 Summary Applet operations are restricted 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 Applet cannot read/write local files, call local programs, or connect to any host other than the one from which it was loaded 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 21 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? 22 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. 23