Summary. 962 Chapter 23 Applets and Java Web Start

Similar documents
Chapter 3 - Introduction to Java Applets

Road Map. Introduction to Java Applets Review applets that ship with JDK Make our own simple applets

An applet is a program written in the Java programming language that can be included in an HTML page, much in the same way an image is included in a

Chapter 7 Applets. Answers


Java Applets. Last Time. Java Applets. Java Applets. First Java Applet. Java Applets. v We created our first Java application

Introduction to Java Applications

TWO-DIMENSIONAL FIGURES

GUI, Events and Applets from Applications, Part III

Lecture Static Methods and Variables. Static Methods

Java WebStart, Applets & RMI

Framework. Set of cooperating classes/interfaces. Example: Swing package is framework for problem domain of GUI programming

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Page 1 of 7. public class EmployeeAryAppletEx extends JApplet

Lecture Static Methods and Variables. Static Methods

9. APPLETS AND APPLICATIONS

Programming: You will have 6 files all need to be located in the dir. named PA4:

Chapter 13. Applets and HTML. HTML Applets. Chapter 13 Java: an Introduction to Computer Science & Programming - Walter Savitch 1

MULTIMEDIA PROGRAMMING IN JAVA. Prof.Asoc. Alda Kika Department of Informatics Faculty of Natural Sciences University of Tirana

Programming In Java Prof. Debasis Samanta Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Introduction to Java Applications

Unit 1 Lesson 4. Introduction to Control Statements

TaskGuide. Users Guide. Version 2.0 Beta. Stottler Henke. Smarter Software Solutions

CS1004: Intro to CS in Java, Spring 2005

Question 2. [5 points] Given the following symbolic constant definition

Java applets & the RealJ IDE

SELF-STUDY. Glossary

CSCI 053. Week 5 Java is like Alice not based on Joel Adams you may want to take notes. Rhys Price Jones. Introduction to Software Development

PROGRAMMING LANGUAGE 2

3 Getting Started with Objects

CS 315 Software Design Homework 1 First Sip of Java Due: Sept. 10, 11:30 PM

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

Data Representation and Applets

Lesson 13: Exploring Factored Form

Java Applet Basics. Life cycle of an applet:

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Chapter 2 Using Objects. Types. Number Literals. A type defines a set of values and the operations that can be carried out on the values Examples:

ADOBE DREAMWEAVER CS4 BASICS

Methods (Deitel chapter 6)

Methods (Deitel chapter 6)

CSC System Development with Java Introduction to Java Applets Budditha Hettige

QClaims Launch Instructions for Windows

Applets and the Graphics class

Sun ONE Integrated Development Environment

Introduction... xv SECTION 1: DEVELOPING DESKTOP APPLICATIONS USING JAVA Chapter 1: Getting Started with Java... 1

Lab 3: Work with data (IV)

Advanced Java Programming

Programming graphics

WebVisit User course

Math-2 Lesson 6-3: Area of: Triangles, rectangles, circles and Surface Area of Pyramids

CS 209 Programming in Java #12 JAR Files: Creation and Use

CHAPTER 7 WEB SERVERS AND WEB BROWSERS

Java Programming Unit 6. Inner Classes. Intro to Apples. Installing Apache Tomcat Server.

FrontPage 2000 Tutorial -- Advanced

Practice Labs User Guide

Graphics Applets. By Mr. Dave Clausen

Netscape Composer Tutorial

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 7, Name:

If you do not have administrator privileges on your computer and are unable to install Java, please reach out to the ITC Help Desk at

CSE 8B Programming Assignments Spring Programming: You will have 5 files all should be located in a dir. named PA3:

Appendix A GLOSSARY SYS-ED/ COMPUTER EDUCATION TECHNIQUES, INC.

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 9: Writing Java Applets. Introduction

Java TM Applets. Rex Jaeschke

G51PRG: Introduction to Programming Second semester Applets and graphics

Installing CWM Client

Chapter 5 - Methods Prentice Hall, Inc. All rights reserved.

ASSIGNMENT NO 14. Objectives: To learn and demonstrated use of applet and swing components

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

CS2 Advanced Programming in Java note 8

INTRODUCTION... 3 INSTALLATION GUIDE FOR ECLIPSE 3.1 AND INSTALLATION GUIDE FOR ECLIPSE 3.3 TO

Block I Unit 2. Basic Constructs in Java. AOU Beirut Computer Science M301 Block I, unit 2 1

BARR/Web Interface Installation Instructions

CHAPTER 8: INTEGRALS 8.1 REVIEW: APPROXIMATING INTEGRALS WITH RIEMANN SUMS IN 2-D

Javadocing in Netbeans (rev )

Secure MaxMarkup

New Cycle Manager operation

Training Quick Steps Internet Explorer (v7) Settings. Adding Your URL as a Trusted Site

download instant at Introduction to C++

Chapter 4 Introduction to Control Statements

Contents 8-1. Copyright (c) N. Afshartous

Introduction to Eclipse

Adobe Dreamweaver CC 17 Tutorial

Computer Science 210: Data Structures. Intro to Java Graphics

Chapter 12 Advanced GUIs and Graphics

Lesson 1 Key-Terms Meanings: Web Connectivity of Devices and Devices Network

Java 1.8 Programming

CW Middle School. Math RtI 7 A. 4 Pro cient I can add and subtract positive fractions with unlike denominators and simplify the result.

Graphics Applets. By Mr. Dave Clausen

Chapter 14: Applets and More

8/23/2014. Chapter Topics. Introduction to Applets. Introduction to Applets. Introduction to Applets. Applet Limitations. Chapter 14: Applets and More

CSE 212 : JAVA PROGRAMMING LAB. IV Sem BE (CS&E) (2013) DEPT OF COMPUTER SCIENCE & ENGG. M. I. T., MANIPAL. Prepared By : Approved by :

Area rectangles & parallelograms

Higher National Diploma in Information Technology First Year, Second Semester Examination 2015

Computer Applications Final Exam Study Guide

Python Programming Exercises 1

8. Polymorphism and Inheritance

Basic and Intermediate Math Vocabulary Spring 2017 Semester

There are four (4) skills every Drupal editor needs to master:

Dynamic code downloading using Java TM (Using the java.rmi.server.codebase Property)

Overview. Applets. A Java GUI inside your browser! Important methods Drawing images Playing audio Getting input parameters Double buffering

Transcription:

962 Chapter 23 Applets and Java Web Start Summary Section 23.1 Introduction Applets (p. 942) are Java programs that are typically embedded in HTML (Extensible Hyper- Text Markup Language) documents also called web pages. 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 (p. 942). It s the applet container s responsibility toload the applet s class(es), create an instance of the applet and manage its life cycle. The JDK includes an applet container called the appletviewer (p. 942) for testing applets as you develop them and before you embed them in web pages. Web browsers execute Java applets via the Java Plug-In (p. 942). Section 23.2 Sample Applets Provided with the JDK To re-execute an applet in the appletviewer, click the appletviewer s Applet menu and select the Reload menu item. To terminate the appletviewer,selectquit from the appletviewer s Applet menu. Section 23.3 Simple Java Applet: Drawing a String Every Java applet is a graphical user interface on which you can place GUI components or draw. Class JApplet (p. 948) from package javax.swing is used to create applets. An appletcontainer can create only objects of classesthat are public andextendjapplet (or the Applet class from early versions of Java). An applet container expects every Java applet to have methods named init, start, paint, stop anddestroy (p. 949), each of which is declared in classjapplet. Each new applet class you create inherits default implementations of these methods from class JApplet. When an applet container loads an applet, it creates an object of the applet s type, then calls the applet s init, start and paint methods. Toenableanapplet todraw, overrideitspaint method (p. 949). The applet container calls paint to tell the applet when to draw. The first statement in method paint should be a call to the superclass method paint. Before you can execute an applet, you must create an HTML (Extensible HyperText Markup Language) document that specifies which applet to execute in the applet container. Typically, an HTML document ends with an.html or.htm file-name extension. Anapplet 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. Normally, an applet and its corresponding HTML document are stored in the same directory. When an applet container encounters an HTML document that contains an applet, the container automatically loads the applet s.class file(s) from the same location as the HTML document. Theappletviewer understands only the <applet> and </applet> HTML tags (p. 950) and ignores all other tags in the document. Section 23.4 Applet Life-Cycle Methods Methodinit (p. 949) is called once by the applet container to initialize an applet when it s loaded. Method start (p. 949) is called by the applet container after method init completes execution. In addition, if the user browses to another website and later returns to the applet s HTML page, method start is called again. Method paint is called by the applet container after methods init and start.

Summary 963 Methodstop (p. 949) is called by the applet container when the user leaves the applet s web page by browsing to another web page. Methoddestroy (p. 949) is called by the applet container when the applet is being removed from memory. This occurs when the user exits the browsing session by closing all the browser windows and may also occur at the browser s discretion when the user has browsed to other web pages. Section 23.5 Initialization with Method init Graphics method drawstring draws a String at a specified location. Graphics method drawrect draws a rectangle at the specified upper-left corner, width and height. Section 23.6 Sandbox Security Model A browser downloads an applet 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 sandbox security model (p. 955) that provides a mechanism for executing downloaded code safely. Downloaded code cannot access local system resources, and an applet can interact only with the server from which it was downloaded. You can digitally sign an applet that requires access to local system resources. If the user indicates that he/she trusts the applet s source, only then will the applet be able to access the local computer s resources. Section 23.7 Java Web Start and the Java Network Launch Protocol (JNLP) Java Web Start (p. 956) is a framework for running downloaded programs outside the browser. 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. Java Web Start 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. When you execute a program via Java Web Start, the program is 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 since it was last lauched, Java Web Start can automatically download the new version, so a user always has the most up-to-date version. A Java Network Launch Protocol (JNLP; p. 956) document provides the information that Java Web Start needs to download and run a program. Programs launched via Java Web Start execute using the sandbox security model. The user can permit access to the local file system, the clipboard and other services via the JNLP APIs. Section 23.7.1 Packaging the DrawTest Applet for Use with Java Web Start Thejar command (p. 957) is used to create JAR files. Option c indicates that the command should create a new JAR file (p. 957). Optionvindicates that the command should produce verbose output (p. 957). Option f indicates that the next argument in the command line is the new JAR file s name (p. 957). Section 23.7.2 JNLP Document for the DrawTest Applet A JNLP document describes the contents of the JAR file and specifies which file in the JAR is the so-called main-class (p. 957) that begins the program s execution. JNLP documents are written in Extensible Markup Language (XML) a widely supported standard for describing data (p. 957).

964 Chapter 23 Applets and Java Web Start JNLP is a so-called XML vocabulary that describes the information Java Web Start needs to launch a program. The jnlp element s (p. 958) codebase attribute specifies the path where the JNLP document and the JAR file are stored. The href attribute specifies the JNLP file that launches the program. Typically, the codebase references a directory on a web server with an http:// URL. Theinformation element (p. 959) provides details about the program. Thetitle element (p. 959) specifies a title for the program. Thevendor element (p. 959) specifies who created the program. Thedesktop element nested in the shortcut element (p. 959) tells Java Web Start to ask users whether they wish to install a desktop shortcut. Theoffline-allowed (p. 959) element indicates that a program can be launched via Java Web Start even when the computer is not connected to the Internet. The resources element (p. 959) contains a java element (p. 959) that lists theminimum version of Java required to execute the program and a jar element (p. 959) that specifies the location of the JAR file. Theapplet-desc (p. 959) element s name attribute specifies the applet s name. The main-class attribute specifies the main applet class. The width and height (p. 950) attributes specify the width and height in pixels, respectively, of the window in which the applet will execute. To launch the applet via Java Web Start you can use thejavaws command (p. 959). You can also use your operating system s file manager to locate the JNLP on your computer and double click its file name. Normally, a JNLP file is referenced from a web page via a hyperlink. WhenyourunanappletviaJavaWebStartthefirsttimeandtheJNLPdocumentspecifiesthat a desktop icon should be installed, you ll be presented with a dialog that enables you to decide whether to install the desktop icon. If you click OK, a new icon labeled with the title specified in the JNLP document appears on the desktop. You can view the installed Java Web Start programs in the Java Cache Viewer by typing the command javaws -viewer in a command window. The Java Cache Viewer enables you to manage the installed Java Web Start programs. You can run a selected program, create a desktop shortcut, delete installed programs, and more. Self-Review Exercise 23.1 Fill in the blanks in each of the following: a) Java applets begin execution with a series of three method calls:, and. b) The method is invoked for an applet each time a browser s user leaves an HTML page on which the applet resides. c) Every applet should extend class. d) The or a browser can be used to execute a Java applet. e) The method is called each time the user of a browser revisits the HTML page on which an applet resides. f) To load an applet into a browser, you must first define a(n) file. g) Method is called once when an applet begins execution. h) Method is invoked to draw on an applet. i) Method is invoked for an applet when the browser removes it from memory. j) The and HTML tags specify that an applet should be loaded into an applet container and executed.

Answers to Self-Review Exercise 965 k) is a framework for running downloaded programs outside the browser. l) A(n) document provides the information that Java Web Start needs to download and run a program. m) The enables you to manage the Java Web Start programs on your system. Answers to Self-Review Exercise 23.1 a) init, start, paint. b)stop. c)japplet (or Applet). d) appletviewer. e)start. f) HTML. g) init. h)paint. i)destroy. j)<applet>, </applet>. k) Java Web Start. l) Java Network Launch Protocol (JNLP). m) Java Cache Viewer. Exercises 23.2 (Arithmetic) Write an applet that asks the user to enter two floating-point numbers, obtains the two numbers from the user and draws their sum, product (multiplication), difference and quotient (division). Use the techniques shown in Fig. 23.9. 23.3 (Comparing Numbers) Write an applet that asks the user to enter two floating-point numbers, obtains the numbers from the user and displays the two numbers, then displays the larger number followed by the words"islarger" as a string on the applet. If the numbers are equal, the applet should print the message "These numbers are equal." Use the techniques shown in Fig. 23.9. 23.4 (Arithmetic and Numeric Comparisons) Write an applet that inputs three floating-point numbers from the user and displays the sum, average, product, smallest and largest of these numbers as strings on the applet. Use the techniques shown in Fig. 23.9. 23.5 (Diameter, Circumference and Area of a Circle) Write an applet that asks the user to input the radius of a circle as a floating-point number and draws the circle s diameter, circumference and area. Use the value 3.14159 for π. Use the techniques shown in Fig. 23.9. [Note: You may also use the predefined constant Math.PI for the value of π. This constant is more precise than the value 3.14159. Class Math is defined in the java.lang package, so you do not need to import it.] Use the following formulas (r is the radius): diameter =2r circumference =2πr area = πr 2 23.6 (Largest and Smallest) Write an applet that reads five integers, determines which are the largest and smallest integers in the group and prints them. Draw the results on the applet. 23.7 (Draw a Checkerboard Pattern) Write an applet that draws a checkerboard pattern as follows: 23.8 (Drawing Rectangles) Write an applet that draws rectangles of different sizes and locations. 23.9 (Drawing Rectangles Based on User Input) Write an applet that allows the user to input values for the arguments required by method drawrect, then draws a rectangle using the four input values.

966 Chapter 23 Applets and Java Web Start 23.10 (Drawing Ovals and Rectangles) Class Graphics contains methoddrawoval,whichtakesas arguments the same four arguments as method drawrect. TheargumentsformethoddrawOval specify the bounding box for the oval the sides of the bounding box are the boundaries of the oval. Write a Java applet that draws an oval and a rectangle with the same four arguments. The oval will touch the rectangle at the center of each side. 23.11 (Drawing Ovals and Rectangles) Modify the solution to Exercise 23.10 to output ovals of different shapes and sizes. 23.12 (Drawing Ovals Based on User Input) Write an applet that allows the user to input the four arguments required by method drawoval, then draws an oval using the four input values. 23.13 (TicTacToe Demonstration Applet with Java Web Start) Package the TicTacToe demonstration applet from the JDK (discussed in Section 23.2) for use with Java Web Start, then copy the JNLP document in Fig. 23.12 and modify it so that it launches the TicTacToe applet.