 1992-2007 Pearson Education, Inc. All rights reserved. 1 20 Introduction to Java Applets.

Slides:



Advertisements
Similar presentations
Lecture 2 Internet Computing Using Java Theophano Mitsa UMASS-Dartmouth.
Advertisements

 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing.
The Web Warrior Guide to Web Design Technologies
© by Pearson Education, Inc. All Rights Reserved.
Java Applets A lab course by Dr. Junaid Ahmed Zubairi SUNY Fredonia.
Lecture 24 Applets. Introduction to Applets Applets should NOT have main method but rather init, stop, paint etc They should be run through javac compiler.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 16 Applets.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2.
1 Applets Chapter 1 To understand:  why applets are used to extend the capabilities of Web pages  how an applet is executed and know about the restrictions.
Java Applets. Road Map Introduction to Java Applets Review applets that ship with JDK Make our own simple applets –Introduce inheritance –Introduce the.
1 L44 Introduction to Java Applets. 2 OBJECTIVES  To differentiate between applets and applications.  To observe some of Java's exciting capabilities.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 14 Applets, Images,
 2002 Prentice Hall. All rights reserved. 1 Week 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2 Software.
Chapter 3 - Introduction to Java Applets Outline 3.1Introduction 3.2Thinking About Objects 3.4A Simple Java Applet: Drawing a String 3.5Two More Simple.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Object Oriented Programming (OOP) LAB # 5 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L05 (Chapter 16) Applets.
PHY281Introduction to JavaSlide 1 Introduction to Java In this section we will learn how how to use Java and write our first Java Applet:  The Java Development.
Chapter 2 Introduction to HTML5 Internet & World Wide Web How to Program, 5/e Copyright © Pearson, Inc All Rights Reserved.
Introduction to scripting
2. Introduction to the Visual Studio.NET IDE 2. Introduction to the Visual Studio.NET IDE Ch2 – Deitel’s Book.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 24 - Introduction to Java Applications and Applets.
Computer Science [3] Java Programming II - Laboratory Course Lab 6: Introduction to Java Applets Sample Applets from the Java Simple Java Applet: Drawing.
 2002 Prentice Hall. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2 Software.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
Tutorial 1 Getting Started with Adobe Dreamweaver CS3
Applets Chapter 17.  Java’s big splash onto the scene came in the mid 90’s. The people at Sun Microsystems had managed to work java programs into Web.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 2 Welcome Application Introducing the Visual Basic 2008 Express Edition IDE.
 2008 Pearson Education, Inc. All rights reserved Introduction to XHTML.
JAPPLET.
1 Introduction to Applets. 2 Applications Programs that execute using the java interpreter Executes in command windows.
Chapter 17: Applets, Images, and Sound. Objectives Learn about applets Write an HTML document to host an applet Use the init() method Work with JApplet.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 5 Working with Images Starting Out with Games & Graphics in.
Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter.
Chapter 8 Introduction to HTML and Applets Fundamentals of Java.
XP New Perspectives on Microsoft Office FrontPage 2003 Tutorial 1 1 Microsoft Office FrontPage 2003 Tutorial 1 – Creating a Web Site.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved CheckWriter Application Introducing Graphics and Printing.
Introduction to Java Applets Sangeetha Parthasarathy 05/21/2001.
1 CSC Computer Education (P) Ltd. DESIGNED BY K PRAKASH,
Java Applets. 2 Introduction to Java Applet Programs Applications are ___________________ programs –executed with Java interpreter Applet is a small program.
Chapter 2: Color and Applets Coming up: Introduction to Graphics.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1Introduction 3.2Thinking About Objects 3.3Sample.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 14 Applets and Advanced GUI  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets F.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 2002 Prentice Hall. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2 Software.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
1 Applets are small applications that are accessed on an Internet server, transported over the internet, automatically installed and run as a part of web.
Applets, Images, and Audio Chapter 14 CSCI CSCI 1302 – Applets, Images, and Audio2 Outline Introduction The Applet Class –The init Method –The start.
Creating a Java Application and Applet
1 Applets. 2 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.
1 Introduction to Graphics b The last one or two sections of each chapter of the textbook focus on graphical issues b Most computer programs have graphical.
HTML-I Basic HTML Elements. HTML (Hyper Text Markup Language) HTML is a document layout and hyperlink- specification language. i.e. a language used to.
Please open JCreator and follow these steps: 1)Configure  Options 2)JDK Tools 3)Choose Run Applet from drop-down 4)Click Default, then Edit 5)Click the.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Will not cover Section 3.7 Thinking About Objects: Identifying.
What Is Firefox? __________ is a Web ___________ that you use to search for and view Web pages, save pages for use in the future, and maintain a list.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2.
Chapter 23 Applets and Java Web Start
Lecture 09 Applets.
Introduction to Java Applets
Chapter 3 - Introduction to Java Applets
WEB PROGRAMMING JavaScript.
Java Applets.
Chapter 3 - Introduction to Java Applets
Java Programming COMP-417 Applet
Presentation transcript:

 Pearson Education, Inc. All rights reserved Introduction to Java Applets

 Pearson Education Inc. All rights reserved. 2 Observe due measure, for right timing is in all things the most important factor. — Georg Wilhelm Friedrich Hegel Painting is only a bridge linking the painter’s mind with that of the viewer. — Eugene Delacroix The direction in which education starts a man will determine his future in life. — Plato

 Pearson Education, Inc. All rights reserved. 3 OBJECTIVES In this chapter you will learn:  To differentiate between applets and applications.  To observe some of Java's exciting capabilities through the JDK's demonstration applets.  To write simple applets.  To write a simple HyperText Markup Language (HTML) document to load an applet into an applet container and execute the applet.  Five methods that are called automatically by an applet container during an applet's life cycle.

 Pearson Education, Inc. All rights reserved Introduction 20.2 Sample Applets Provided with the JDK 20.3 Simple Java Applet: Drawing a String Executing an Applet in the appletviewer Executing an Applet in a Web Browser 20.4 Applet Life-Cycle Methods 20.5 Initializing an Instance Variable with Method init 20.6 Sandbox Security Model 20.7 Internet and Web Resources 20.8 Wrap-Up

 Pearson Education, Inc. All rights reserved Introduction Applets – Java programs that can be embedded in HyperText Markup Language (HTML) documents – The browser that executes an applet is generically known as the applet container

 Pearson Education, Inc. All rights reserved Sample Applets Provided with the JDK Demonstration applets provided with the JDK – Demonstration programs are located in directory demo Default location in Windows: C:\Program Files\Java\jdk1.5.0\demo Default location in UNIX/Linux/Mac OS X: the directory in which you install the JDK followed by jdk1.5.0/demo –JDK and the demos can be downloaded from the Sun Microsystems Java Web site java.sun.com/j2se/5.0/

 Pearson Education, Inc. All rights reserved. 7 Fig | The examples from the applets directory. (Part 1 of 3.)

 Pearson Education, Inc. All rights reserved. 8 Fig | The examples from the applets directory. (Part 2 of 3.)

 Pearson Education, Inc. All rights reserved. 9 Fig | The examples from the applets directory. (Part 3 of 3.)

 Pearson Education, Inc. All rights reserved Sample Applets Provided with the JDK (Cont.) TicTacToe applet – Allows you to play Tic-Tac-Toe against the computer – Run the applet with the appletviewer command Change directories to subdirectory TicTacToe Type command appletviewer example1.html Point the mouse at the square where you want to place an X – To play again Click the Applet menu Select the Reload menu item – To terminate the appletviewer Click the Applet menu Select the Quit menu item

 Pearson Education, Inc. All rights reserved. 11 Fig | TicTacToe applet sample execution.

 Pearson Education, Inc. All rights reserved. 12 Fig | Applet menu in the appletviewer. Reload the applet to execute it again. Select Quit to terminate the appletviewer.

 Pearson Education, Inc. All rights reserved Sample Applets Provided with the JDK (Cont.) DrawTest applet – Allows you to draw lines and points in different colors – Run the applet with the appletviewer command Change directories to subdirectory drawTest Type command appletviewer example1.html Drag the mouse across the applet to draw lines Select a color by clicking one of the radio buttons at the bottom of the applet – Select from red, green, blue, pink, orange and black Change the shape to draw from Lines to Points by selecting Points from the combo box Select Reload from the Applet menu to start a new drawing

 Pearson Education, Inc. All rights reserved. 14 Fig | DrawTest applet sample execution. Drag the mouse in the white area to draw. Select the drawing color by clicking one of the radio buttons. Select Lines or Points from the combo box to specify what will be drawn when you drag the mouse.

 Pearson Education, Inc. All rights reserved Sample Applets Provided with the JDK (Cont.) Java2D applet – Demonstrates many features of the Java 2D API – Run the applet with the appletviewer command Change directories to the jfc directory in the JDK’s demo directory, then change to the Java2D directory Type command appletviewer Java2Demo.html To change to a different part of the demo, click a different tab at the top of the applet Change the options in the upper-right corner – Example: click the checkbox to the left of the word Anti- Aliasing A graphical technique for producing smoother graphics in which edges of the graphic are blurred

 Pearson Education, Inc. All rights reserved. 16 Fig | Java2D applet sample execution. Click a tab to select a two-dimensional graphics demo. Try changing the options to see their effect on the demonstration.

 Pearson Education, Inc. All rights reserved Simple Java Applet: Drawing a String Creating the applet class – An applet container can create only objects of classes that are public and extend JApplet – An applet container expects every Java applet class to have methods named init, start, paint, stop and destroy These methods are inherited from class JApplet and can be overridden When an applet container loads an applet class, the container creates an object of the class then calls methods init, start and paint

 Pearson Education, Inc. All rights reserved. 18 Import Graphics and JApplet Class WelcomeApplet extends class JApplet Call the superclass version of method paint Use Graphics method drawString to draw Welcome to Java Programming!

 Pearson Education, Inc. All rights reserved. 19 Fig | Sample outputs of the WelcomeApplet in Fig x-axis y-axis Upper-left corner of drawing area is location (0, 0). Drawing area extends from below the Applet menu to above the status bar. x- coordinates increase from left to right. y-coordinates increase from top to bottom. Pixel coordinates (25, 25) at which the string is displayed Applet menu Status bar mimics what would be displayed in the browser’s status bar as the applet loads and begins executing. WelcomeApplet executing in Microsoft Internet Explorer Upper-left corner of drawing area Status bar Pixel coordinate (25, 25) WelcomeApplet executing in the appletviewer

 Pearson Education, Inc. All rights reserved Simple Java Applet: Drawing a String (Cont.) Overriding method paint for drawing – The applet container calls method paint with a Graphics object as an argument to tell the applet when to draw

 Pearson Education, Inc. All rights reserved Executing an Applet in the appletviewer Applets are embedded in Web pages for execution in an applet container – Before executing the applet, you must create an HTML document that specifies which applet to execute HTML documents typically end with an “.html ” or “.htm ” file-name extension Most HTML elements are delimited by pairs of tags – All HTML tags begin with a left angle bracket, – Execute WelcomeApplet in the appletviewer In the directory containing your applet and HTML document, type appletviewer WelcomeApplet.html The appletviewer understands only the and HTML tags and ignores all other tags

 Pearson Education, Inc. All rights reserved. 22 Fig | WelcomeApplet.html loads WelcomeApplet (Fig. 20.6) into an applet container. Specify an applet element Applet element attributes

 Pearson Education, Inc. All rights reserved. 23 Look-and-Feel Observation 20.1 To ensure that it can be viewed properly on most computer screens, an applet should generally be less than 1024 pixels wide and 768 pixels tall—dimensions supported by most computer screens.

 Pearson Education, Inc. All rights reserved. 24 Common Programming Error 20.1 Forgetting the ending tag prevents the applet from executing in some applet containers. The appletviewer terminates without indicating an error. Some Web browsers simply ignore the incomplete applet element.

 Pearson Education, Inc. All rights reserved. 25 Error-Prevention Tip 20.1 If you receive a MissingResourceException error message when loading an applet into the appletviewer or a browser, check the tag in the HTML document carefully for syntax errors, such as commas (, ) between the attributes.

 Pearson Education, Inc. All rights reserved. 26 Error-Prevention Tip 20.2 Test your applets in the appletviewer applet container before executing them in a Web browser. Browsers often save a copy of an applet in memory until all the browser’s windows are closed. If you change an applet, recompile it, then reload it in your browser, the browser may still execute the original version of the applet. Close all your browser windows to remove the old applet from memory. Open a new browser window and load the applet to see your changes.

 Pearson Education, Inc. All rights reserved. 27 Error-Prevention Tip 20.3 Test your applets in every Web browser in which they will execute to ensure that they operate correctly.

 Pearson Education, Inc. All rights reserved Executing an Applet in a Web Browser To execute an applet in Internet Explorer: – Select Open… from the File menu – Click the Browse… button – Locate the directory containing the HTML document for the applet you wish to execute – Select the HTML document – Click the Open button – Click the OK button

 Pearson Education, Inc. All rights reserved Executing an Applet in a Web Browser (Cont.) If your applet executes in the appletviewer but not in your Web browser – Java may not be installed and configured for your browser Visit the Web site java.com and click the Get It Now button to install Java for your browser java.com You may need to manually configure Internet Explorer to use J2SE 5.0 – Click the Tools menu – Select Internet Options… – Click the Advanced tab – Check the “ Use JRE v1.5.0 for (requires restart) ” option – Click OK – Close all browser windows before attempting to execute another applet in the browser

 Pearson Education, Inc. All rights reserved. 30 Fig | JApplet life cycle methods that are called by an applet container during an applet’s execution. (Part 1 of 3.)

 Pearson Education, Inc. All rights reserved. 31 Fig | JApplet life cycle methods that are called by an applet container during an applet’s execution. (Part 2 of 3.)

 Pearson Education, Inc. All rights reserved. 32 Fig | JApplet life cycle methods that are called by an applet container during an applet’s execution. (Part 3 of 3.)

 Pearson Education, Inc. All rights reserved. 33 Common Programming Error 20.2 Declaring methods init, start, paint, stop or destroy with method headers that differ from those shown in Figure 20.9 results in methods that will not be called by the applet container. The code specified in your versions of the methods will not execute.

 Pearson Education, Inc. All rights reserved Initializing an Instance Variable with Method init Applet AdditionApplet – computes the sum of two values input 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 class AdditionApplet – So it can be used in both method init and method paint

 Pearson Education, Inc. All rights reserved. 35 Declare instance variable sum of type double init method called once when the container loads this applet

 Pearson Education, Inc. All rights reserved. 36 Sum the values and assign the result to instance variable sum Call drawString to display sum

 Pearson Education, Inc. All rights reserved. 37

 Pearson Education, Inc. All rights reserved. 38 Fig | AdditionApplet.html loads class AdditionApplet of Fig into an applet container. Load AdditionApplet

 Pearson Education, Inc. All rights reserved. 39 Software Engineering Observation 20.1 The only statements that should be placed in an applet’s init method are those that should execute only once when the applet is initialized.

 Pearson Education, Inc. All rights reserved Sandbox Security Model Sandbox security model – Code executing in the “sandbox” is not allowed to “play outside the sandbox” – Used by the Java platform to prevent code that is downloaded to your local computer from accessing local system resources, such as files – For information on security and applets developer.java.sun.com/developer/technicalA rticles/Security/Signeddeveloper.java.sun.com/developer/technicalA rticles/Security/Signed – For information on the Java 2 Platform security model java.sun.com/j2se/5.0/docs/guide/security/s pec/security-spec.doc1.htmljava.sun.com/j2se/5.0/docs/guide/security/s pec/security-spec.doc1.html

 Pearson Education, Inc. All rights reserved Internet and Web Resources Sun Microsystems Java Web site – java.sun.com java.sun.com – Java applet resources java.sun.com/applets – Free online tutorials java.sun.com/learning To install and configure Java for your browser – Visit java.com java.com – Click the Get It Now button

 Pearson Education, Inc. All rights reserved Internet and Web Resources JARS – Originally called the Java Applet Rating Service Rated every applet registered at the site Allowed users to view the best applets on the Web – Now an all-around resource for Java programmers