Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java How to Program, 9/e © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

Similar presentations


Presentation on theme: "Java How to Program, 9/e © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved."— Presentation transcript:

1 Java How to Program, 9/e © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

2

3

4  This chapter introduces applets—Java programs that are typically embedded in XHTML (Extensible HyperText Markup Language) documents.  When a Java-enabled web browser loads a web page containing an applet, the applet downloads into the browser and executes.  The application in which an applet executes is known as the applet container.  The applet container loads the applet’s class(es), creates an instance of the applet and manages its life cycle. ◦ The Java Development Kit (JDK) includes one called the appletviewer for testing applets as you develop them and before you embed them in web pages. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

5  Some browsers don’t come with the plug-in by default.  You can visit java.com to determine whether your browser is ready to execute Java applets.  If not, you can click the Free Java Download button to install Java for your browser.  We demonstrate applets using both the appletviewer and web browsers, which execute Java applets via the Java Plug-In.  Java Web Start and the Java Network Launch Protocol (JNLP) enable you to package your applets and applications so that they can be installed onto the user’s desktop. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

6  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. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

7

8

9

10  This TicTacToe demonstration applet allows you to play Tic- Tac-Toe against the computer.  Change directories to subdirectory TicTacToe, where you’ll find the file example1.html that loads the applet.  Execute the applet with the command appletviewer example1.html ◦ Loads the XHTML document example1.html specified as its command-line argument.  Can also open the XHTML document in your browser.  To play again, click the appletviewer ’s Applet menu and select the Reload menu item (Fig. 23.3), or click the applet again when the game is over.  To terminate the appletviewer, click the appletviewer ’s Applet menu and select the Quit menu item. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

11

12

13  The DrawTest applet allows you to draw lines and points in different colors.  In the command window, change directories to directory applets, then to subdirectory DrawTest.  In the command window, type the command ◦ appletviewer example1.html © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

14

15  The Java2D applet demonstrates many features of the Java 2D API (which we introduced in Chapter 14).  Change directories to the jfc directory in the JDK’s demo directory, then change to the Java2D directory.  In the command window, type the command ◦ appletviewer Java2Demo.html © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

16

17  Every Java applet is a graphical user interface on which you can place GUI components using the techniques introduced in Chapter 14 or draw using the techniques demonstrated in Chapter 14.  In this chapter, we’ll demonstrate drawing on an applet.  Examples in Chapters 24 and 27 demonstrate building an applet’s graphical user interface. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

18

19

20  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. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

21  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. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

22  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. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

23  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. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

24

25

26

27

28

29

30  To execute an applet in Firefox, perform the following steps: ◦ Select Open File… from the File menu. ◦ In the dialog box that appears, locate the directory containing the XHTML document for the applet you wish to execute. ◦ Select the XHTML document. ◦ Click the Open button.  The steps for executing applets in other web browsers are similar.  In most browsers, you can simply type Ctrl + O to open a dialog that enables you to select an XHTML document from your local hard disk. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

31

32  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. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

33

34

35

36

37  Our next applet (Fig. 23.9) computes the sum of two values entered into input dialogs by the user and displays the result by drawing a String inside a rectangle on the applet.  The sum is stored in an instance variable of the AdditionApplet class, so it can be used in both the init method and the paint method.  The XHTML document that you can use to load this applet into an applet container (i.e., the appletviewer or a web browser) is shown in Fig. 23.10. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

38

39

40

41

42

43  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. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

44  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. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

45  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. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

46  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. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

47  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. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

48  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. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

49  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. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

50  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. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

51  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. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

52

53  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. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

54

55

56  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. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

57  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/technotes/ guides/javaws/developersguide/setup.h tml. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

58  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. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

59  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. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

60  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. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

61

62

63

64  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. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

65


Download ppt "Java How to Program, 9/e © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved."

Similar presentations


Ads by Google