Download presentation
Presentation is loading. Please wait.
Published byGiles Ward Modified over 9 years ago
1
Applets Life Cycle Drawing and Event Handling Methods for UI Components Applet Capabilities Example
2
Life Cycle Loading the applet –instance of applet subclass is created –applet initializes itself [ init ()] (like constructor) –applet starts running [ start ()] (execution, except user input) Leave and Come Back –stopping [ stop ()] –restarting [ start ()] –iconification [minimize]
3
Reloading the Applet Quitting the Browser –stop () –final cleanup [ destroy ()] init, start, stop, destroy methods
5
Drawing and Event Handling paint (): Basic display method. update (): method used with paint () to improve performance Objects notified of registered events (listen for them) Register event by implementing appropriate interface ( mouseListener )
7
Buttons (java.awt.Button) Checkboxes (java.awt.Checkbox) Single-line text fields (java.awt.TextField) Larger text display and editing areas (java.awt.TextArea) Labels (java.awt.Label) Lists (java.awt.List) Pop-up lists of choices (java.awt.Choice) Sliders and scrollbars (java.awt.Scrollbar) Drawing areas (java.awt.Canvas) Menus (java.awt.Menu, java.awt.MenuItem, java.awt.CheckboxMenuItem) Containers (java.awt.Panel, java.awt.Window and its subclasses) Pre-Made AWT UI Components
8
Adding UI Components add: adds the component to the applet remove: removes the component setLayout: sets the applet’s layout manager which controls the position and size of the applet components
10
Working with Applets Cannot load libraries or define native methods Cannot read or write files on local host Cannot make network connections Cannot start a program on local host Cannot read system properties on local host Implemented with SecurityManager which throws SecurityExceptions
11
Applets Can... Display html If local, restrictions off make network connections back to host they came from Don’t have to stop when you leave
12
<APPLET CODE = AppletSubclass.class WIDTH=anInt HEIGHT=anInt>
13
Finding and Loading Data Files
14
Applet.getCodeBase() returns URL of directory from which applet classes load Applet.getDocumentBase() returns URL of directory from which applet’s html came Image image=getImage(getCodeBase(),"imgDir/a.gif");
15
Showing Status Applet.showStatus(String s); showStatus("MyApplet: Loading image file " + file);
16
Displaying Documents AppletContext class allows applet to interact with context (i.e. browser) in which it runs. Display in browser (won’t work with appletviewer) public void showDocument(java.net.URL url) public void showDocument(java.net.URL url, String targetWindow)
17
"_blank" Display the document in a new, nameless window. "windowName" Display the document in a window named windowName. This window is created if necessary. "_self" Display the document in the window and frame that contain the applet. "_parent" Display the document in the applet's window but in the parent frame of the applet's frame. If the applet frame has no parent frame, this acts the same as "_self". "_top" Display the document in the applet's window but in the top-level frame. If the applet's frame is the top-level frame, this acts the same as "_self".
18
...//In an Applet subclass: urlWindow = new URLWindow(getAppletContext());... class URLWindow extends Frame {... public URLWindow(AppletContext appletContext) {... this.appletContext = appletContext;... }... public boolean action(Event event, Object o) {... String urlString = /* user-entered string */; URL url = null; try { url = new URL(urlString); } catch (MalformedURLException e) {...//Inform the user and return... } if (url != null) { if (/* user doesn't want to specify the window */) { appletContext.showDocument(url); } else { appletContext.showDocument(url, /* user-specified window */); }...
19
Sending Messages to Other Applets Same host, same directory, same page Find by name on same page Two ways to name applet
20
1) By specifying a NAME attribute within the applet's tag. For example: <APPLET CODEBASE=example/ CODE=Sender.class WIDTH=450 HEIGHT=200 NAME="buddy">... 2) By specifying a NAME parameter with a tag. For example: <APPLET CODEBASE=example/ CODE=Receiver.class WIDTH=450 HEIGHT=35>...
21
Applet receiver = null; String receiverName = nameField.getText(); //Get name to search for. receiver = getAppletContext().getApplet(receiverName); if (receiver != null) { //Use the instanceof operator to make sure the applet //we found is a Receiver object. if (!(receiver instanceof Receiver)) { status.appendText("Found applet named " + receiverName + ", " + "but it's not a Receiver object.\n"); } else { status.appendText("Found applet named " + receiverName + ".\n" + " Sending message to it.\n"); //Cast the receiver to be a Receiver object //(instead of just an Applet object) so that the //compiler will let us call a Receiver method. ((Receiver)receiver).processRequestFrom(myName); } }...
22
Playing Sound AudioClip onceClip, loopClip; onceClip = applet.getAudioClip(getCodeBase(), "bark.au"); loopClip = applet.getAudioClip(getCodeBase(), "train.au"); onceClip.play(); //Play it once. loopClip.loop(); //Start the sound loop. loopClip.stop(); //Stop the sound loop.
23
Using the Applet Tag Like command line arguments for applets Customize applet operation User Configures Names Provide Default Values Parameter values are all strings Let applet interpret string
24
<APPLET CODE=AppletButton.class CODEBASE=example WIDTH=350 HEIGHT=60> <PARAM NAME=buttonText VALUE="Click here to see a BorderLayout in action">
25
int requestedWidth = 0;... String windowWidthString = getParameter("WINDOWWIDTH"); if (windowWidthString != null) { try { requestedWidth = Integer.parseInt(windowWidthString); } catch (NumberFormatException e) { //Use default width. }
26
String windowClass; String buttonText; String windowTitle; int requestedWidth = 0; int requestedHeight = 0;... public void init() { windowClass = getParameter("WINDOWCLASS"); if (windowClass == null) { windowClass = "TestWindow"; } buttonText = getParameter("BUTTONTEXT"); if (buttonText == null) { buttonText = "Click here to bring up a " + windowClass; } windowTitle = getParameter("WINDOWTITLE"); if (windowTitle == null) { windowTitle = windowClass; } String windowWidthString = getParameter("WINDOWWIDTH"); if (windowWidthString != null) { try { requestedWidth = Integer.parseInt(windowWidthString); } catch (NumberFormatException e) { //Use default width. }...
27
Combining Applet Files If applet has more than one file, combine into zip archive jar tool and JAR format from java jar cvf file.zip com/mycompany/myproject/*.class *.gif (Solaris) jar cvf file.zip com\mycompany\myproject\*.class *.gif (Windows 95/NT)
28
<APPLET CODE="AppletSubclass.class" ARCHIVE="file1, file2" WIDTH=anInt HEIGHT=anInt>
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.