Download presentation
Presentation is loading. Please wait.
Published byJody Manning Modified over 9 years ago
1
1 Introduction to Applets
2
2 Applications Programs that execute using the java interpreter Executes in command windows
3
3 Applets Program that runs in – appletviewer (test utility for applets) – Web browser (IE, Communicator) Executes when HTML (Hypertext Markup Language) document containing applet is opened
4
(C) 2010 Pearson Education, Inc. All rights reserved.
5
5 A First Java Applet To compile: javac WelcomeApplet.java – creates WelcomeApplet.class To execute: – create Welcome.html - In Command Prompt >appletviewer Welcome.html
6
(C) 2010 Pearson Education, Inc. All rights reserved.
7
Class JApplet (package javax.swing ) is used to create applets. Every applet contains at least one public class declaration. An applet container can create only objects of classes that are public and extend JApplet (or its superclass Applet ). An applet container expects every applet to have methods named init, start, paint, stop and destroy, each of which is declared in class JApplet. – These methods can be overridden (redefined) to perform tasks that are specific to your applet. When an applet container loads class WelcomeApplet, the container creates a WelcomeApplet object, then calls its methods init, start and paint in sequence. Inheriting the “default” versions of these methods guarantees that the applet container can execute each applet uniformly.
8
(C) 2010 Pearson Education, Inc. All rights reserved. Override method paint to draw on an applet. Method paint receives a parameter of type Graphics, which is used to draw graphics on the applet. The applet container calls paint to tell the applet when to draw, and the applet container is responsible for passing a Graphics object as an argument.
9
(C) 2010 Pearson Education, Inc. All rights reserved. Applets are embedded in web pages for execution in an applet container ( appletviewer or a browser). You must create an XHTML document that specifies which applet to execute in the applet container. An applet element tells the applet container to load a specific applet and defines the size of its display area (its width and height in pixels) in the applet container. The applet and its corresponding XHTML document are normally stored in the same directory on disk.
10
(C) 2010 Pearson Education, Inc. All rights reserved. When an applet container encounters an applet element in an XHTML document, it loads the applet’s.class file (or files) from the same location that contains the XHTML document. The applet element has several attributes. Attribute code indicates the applet’s.class file. Attributes width and height specify the dimensions of the applet.
11
(C) 2010 Pearson Education, Inc. All rights reserved.
12
Five applet methods are called by the applet container from the time the applet is loaded into the browser to the time it’s terminated by the browser. These methods correspond to various aspects of an applet’s life cycle. Figure 23.8 lists these methods, which are inherited into your applet classes from class JApplet.
13
(C) 2010 Pearson Education, Inc. All rights reserved.
20
For security reasons, it’s generally considered dangerous to allow applets or any other program that you execute from a web browser to access your local computer. So, you must decide whether you trust the source. Most of what you do with your web browsers requires you to trust the sites you visit and to trust the organizations that maintain those sites.
21
(C) 2010 Pearson Education, Inc. All rights reserved. Applets are typically downloaded from the Internet. What would happen if you downloaded a malicious applet? A browser downloads and executes a Java applet automatically—the user is not asked for approval. In fact, an applet typically downloads without the user’s knowledge— it’s just another element of the web page the user happens to be visiting. To combat malicious code, the Java platform uses a so-called sandbox security model that provides a mechanism for executing downloaded code safely. Such code executes in the “sandbox” and is not allowed to “play outside the sandbox.” By default, downloaded code cannot access local system resources, and an applet can interact only with the server from which the applet was downloaded.
22
(C) 2010 Pearson Education, Inc. All rights reserved. The sandbox makes it difficult for applets to perform useful tasks. It’s possible, however, for an organization that wishes to create applets with access to the local system to obtain a security certificate (also called a digital certificate) from one of several certificate authorities. Can then use tools provided with the JDK to “digitally sign” an applet that requires access to local system resources. When a user downloads a digitally signed applet, a dialog prompts the user asking whether he or she trusts the applet’s source. If so, only then will the applet be able to access to the local computer’s resources.
23
(C) 2010 Pearson Education, Inc. All rights reserved. Java Web Start is a framework for running downloaded applets and applications outside the browser. Typically, stored on a web server for access via the Internet, but can also be stored on an organization’s network for internal distribution, or even on CDs, DVDs or other media. Java Web Start enables you to ask the user if a downloaded program can have access to the resources of the user’s computer.
24
Applet Parameter
25
import java.awt.*; import javax.Swing.*; public class ParameterExample extends JApplet { String parameter1; // the first HTM parameter as a String int parameter2; // the second one we will use as an integer int parameter3; // third one too int result; // we'll add param2 and param3 public void init() { // This method will get the specified parameter's value // out of the HTML code that is calling the applet. parameter1 = getParameter("param1"); // Since those are read as text we need to transform them // to integers parameter2 = Integer.parseInt(getParameter("param2")); parameter3 = Integer.parseInt(getParameter("param3")); result = parameter2 + parameter3; } public void paint(Graphics g) { // Shows what was in the HTML param code. g.drawString("Parameter 1 is: " + parameter1,20,20); g.drawString("Parameter 2 is: " + parameter2,20,40); g.drawString("Parameter 3 is: " + parameter3,20,60); g.drawString("Parameter 2 + parameter 3 is: " + result,20,80); }
26
Draw a sin(x) Function import java.awt.*; import java.applet.*; import javax.swing.*; public class FuncApplet extends JApplet { Color dc; //the drawing color //initialize the drawing color public void init(){ dc = Color.BLACK; String colorParameter; if((colorParameter = getParameter("drawcolor")) != null) //get parameter from html code dc = parseColor(colorParameter); } public Color parseColor(String c){ //simply accept blue and red, otherwise use default color if (c.equals("blue")) return Color.BLUE; else if (c.equals("red")) return Color.RED; return Color.BLACK; } //the function to be drawn double f(double x) { return (Math.sin(x))*(getSize().height/2)+getSize().height/2; } public void paint(Graphics g) { g.setColor(dc); for (int x = 0 ; x < getSize().width; x++) { //draw the function g.drawLine(x, (int)f(x*1.0/getSize().width*2*3.14), (x + 1), (int)f((x + 1)*1.0/getSize().width*2*3.14)); }
28
(C) 2010 Pearson Education, Inc. All rights reserved. Desktop integration: Users can launch robust applets and applications by clicking a hyperlink in a web page, and can quickly and easily install the programs on their computers. – Can be configured to ask the user if a desktop icon should be created so the user can launch the program directly from the desktop. – Downloaded programs can also have an “offline mode” for execution when the computer is not connected to the Internet. Automatic updating: Java Web Start programs are downloaded and cached (stored) on the user’s computer. The next time the user executes that program, Java Web Start launches it from the cache. If the program has been updated, Java Web Start can automatically download the updates, so a user always has the most up-to-date version. – This makes installation and updating software simple and seamless to the user.
29
(C) 2010 Pearson Education, Inc. All rights reserved. Draggable applets: New in Java SE 6 Update 10. With a small change to the applet element that invokes an applet from an XHTML document, you can allow users to execute an applet in its own window by holding the Alt key and dragging the applet out of the web browser. The applet continues to execute even after the web browser closes.
30
(C) 2010 Pearson Education, Inc. All rights reserved. A Java Network Launch Protocol (JNLP) document provides the information that Java Web Start needs in order to download and run a program. Must package your program in one or more Java archive (JAR) files that contain the program’s code and resources. By default, programs launched via Java Web Start execute using the sandbox security model. If the user gives permission, such programs can access the local file system, the clipboard and other services via the JNLP APIs of package javax.jnlp.
31
(C) 2010 Pearson Education, Inc. All rights reserved. To package the JDK’s DrawTest demonstration applet so that you can execute it via Java Web Start, you must first wrap the applet’s.class files and the resources it uses (if any) into a Java archive (JAR) file. In a command window, change to the DrawTest directory, as you did in Section 23.2. Once in that folder, execute the following command: – jar cvf DrawTest.jar *.class creates a JAR file in the current directory named DrawTest.jar containing the applet’s.class files. If the program had other resources, you’d simply add the file names or the folder names in which those resources are stored to the end of the preceding command.
32
(C) 2010 Pearson Education, Inc. All rights reserved. The letters cvf are command-line options to the jar command. The c option indicates that the command should create a new JAR file. The v option indicates that the command should produce verbose output so you can see the list of files and directories being included in the JAR file. The f option indicates that the next argument in the command line ( DrawTest.jar ) is the new JAR file’s name.
33
(C) 2010 Pearson Education, Inc. All rights reserved.
34
Next, create a JNLP document that describes the contents of the JAR file and specifies which file in the JAR is the so-called main- class that begins the program’s execution. For an applet, the main-class is the one that extends JApplet. For an application, the main-class is the one that contains the main method.
35
(C) 2010 Pearson Education, Inc. All rights reserved.
37
JNLP documents are written in Extensible Markup Language (XML). JNLP is a so-called XML vocabulary that describes the information Java Web Start needs to launch a program. In Fig. 23.12, the jnlp element (lines 2–26) is the root element-. The jnlp element’s start tag (lines 2–4) has two attributes— codebase and href. – The codebase attribute’s value is a URL that specifies the path where the JNLP document and the JAR file are stored. – The href attribute specifies the JNLP file that launches the program.
38
(C) 2010 Pearson Education, Inc. All rights reserved. Typically, the codebase references a directory on a web server with an http:// URL. If you’d like to serve your applet or application from a web server so users can access it online, you’ll need to configure your web server correctly, as described at java.sun.com/javase/6/docs/tech notes/guides/javaws/developersg uide/setup.html.
39
(C) 2010 Pearson Education, Inc. All rights reserved. The information element provides details about the program. The title element specifies a title for the program. The vendor element specifies who created the program. – The values of these elements appear in Java Web Start’s security warnings and errors that are presented to the user. – The title ’s value also appears in the title bar of the window in which the program executes. The desktop element that is nested in the shortcut element tells Java Web Start to ask whether the user wishes to install a desktop shortcut. – If the user accepts, an icon will appear on the desktop. The offline-allowed element indicates that once the program is installed on the user’s computer, it can be launched via Java Web Start—even when the computer is not connected to the Internet.
40
(C) 2010 Pearson Education, Inc. All rights reserved. The resources element contains two nested elements. – The java element lists the minimum version of Java required to execute the program – The jar element specifies the location of the JAR file that contains the program and whether that JAR file contains the class that launches the program The applet-desc element is similar to the applet element in XHTML. – The name attribute specifies the applet’s name. – The main-class attribute specifies the main applet class (the one that extends JApplet ). – The width and height attributes specify the width and height in pixels, respectively, of the window in which the applet will execute.
41
(C) 2010 Pearson Education, Inc. All rights reserved. There are several ways to to launch the applet via Java Web Start. The javaws command in a command window from the folder that contains the JNLP document, as in javaws DrawTest.jnlp Can also use your operating system’s file manager to locate the JNLP on your computer and double click its file name. The JNLP file can be referenced from a web page via a hyperlink. Clicking the hyperlink in the web page downloads the JNLP file and executes the corresponding applet.
42
(C) 2010 Pearson Education, Inc. All rights reserved.
45
You can view the installed Java Web Start programs in the Java Cache Viewer by typing the following command in a command window: javaws -viewer The Java Cache Viewer enables you to manage the Java Web Start programs on your system. You can run a selected program, create a desktop shortcut for a program (if there is not one already), delete installed programs, and more.
46
(C) 2010 Pearson Education, Inc. All rights reserved.
47
Demonstration applets provided with the JDK. – Each sample applet comes with its source code. The demo directory contains several subdirectories. The applets directory contains demonstration applets. The jfc (Java Foundation Classes) directory contains applets and applications that demonstrate Java’s powerful graphics and GUI capabilities. Change to the applets directory and list its contents to see the directory names for the demonstration applets. Figure 23.1 provides a brief description of each demo applet.
48
(C) 2010 Pearson Education, Inc. All rights reserved.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.