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.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 10 Applets.
Advertisements

Learn about Java applets. Know the differences between Java applets and applications. Designing and using Java applets Running Java applets. Security.
Java Applets:. How Applets differ from application?: They do not use main method but init(), start() and paint() methods of the applet class They can.
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
1 L45 Multimedia: Applets and Applications. 2 OBJECTIVES  How to get and display images.  To create animations from sequences of images.  To create.
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.
Applets. The Applet Class public class MyApplet extends java.applet.Applet {... /** The no-arg constructor is called by the browser when the Web page.
Review of Java Applets Vijayan Sugumaran Decision and Information Sciences Oakland University.
1 Introduction to Applets Overview l What is an Applet? l Steps for creating an applet l What is HTML? l Basic HTML tags l Drawing Simple Graphical shapes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 16 Applets.
18-Jun-15 Applets. 2 An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page.
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.
Chapter 14 Applets. 2 Knowledge Goals Understand the differing roles of applications and applets Understand how a browser operates Understand the role.
Java Applets. Lecture Objectives Learn about Java applets. Know the differences between Java applets and applications. Designing and using Java applets.
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,
 Pearson Education, Inc. All rights reserved Multimedia: Applets and Applications.
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.
28-Jun-15 Applets. 2 An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page.
Applets. What is an Applet?  According to Sun “An applet is a small program that is intended not to be run on its own, but rather to be embedded inside.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L05 (Chapter 16) Applets.
Applets, HTML and GUI’s Recitation – 04/11/2008 CS 180 Department of Computer Science, Purdue University.
Computer Science [3] Java Programming II - Laboratory Course Lab 7: Multimedia: Applets and Applications Faculty of Engineering & IT Software Engineering.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
Applets  The Applet Class  The HTML Tag F Passing Parameters to Applets.
Chapter 9: Applets Jim Burns Fall Outline Learn about applets Learn about applets Write an HTML doc to host an applet Write an HTML doc to host.
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.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
 2005 Pearson Education, Inc. All rights reserved Multimedia: Applets and Applications.
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.
JAPPLET.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
1 INF160 IS Development Environments AUBG, COS dept Lecture 04 Title: Computer Applications (Extract from Syllabus)
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.
Java Applets. An applet is a Panel that allows interaction with a Java program. A applet is typically embedded in a Web page and can be run from a browser.
Applets. What is an applet? Why create applets instead of applications? – Applets are Java programs that can be embedded in an HTML document – In contrast,
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Java Applets 1. What is an applet? An applet is a small Java program that is typically embedded in a Web page and can be run using the applet viewer or.
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
Program that runs in appletviewer (test utility for applets) Web browser (IE, Communicator) Executes when HTML (Hypertext Markup Language) document containing.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 16 Applets and Multimedia.
APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
Applets Yong Choi School of Business CSU, Bakersfield.
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
Java Programming Working with Sound and Images. Topics Learn about the paint() and repaint() methods Learn about paintComponent() method Use the drawString()
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.
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.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 2 – Welcome Application Introduction to Graphical.
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.
CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Applet An applet is a Java program that runs in a Web browser. An applet can be.
CHAPTER Agenda Applets Servelets Browsers HelloWorld.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Will not cover Section 3.7 Thinking About Objects: Identifying.
1 Applets Programming. Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program.
Java Applets.
Applets.
Lecture 09 Applets.
UNIT-5.
Java Applets.
11.1 Applets & graphics.
JAVA APPLET PREPARED BY Mr. Jahanzaib Ahmed
APPLET PROGRAMMING.
Presentation transcript:

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 components Understand the JApplet life cycle Understand multimedia and use images Add sound to JApplet s 2Java Programming, Seventh Edition

Introducing Applets Applets – Java programs called from within another application – Frequently are run from a Web page – Can contain any number of components – Can respond to user-initiated events – Behaviors come from a Java class named JApplet 3Java Programming, Seventh Edition

Introducing Applets (cont’d.) Similar to a Java application – Use a.java file extension – Compile into bytecode using the javac command – Saved with the.class extension – Can contain: Methods you define Variables and constants Decisions, loops, and arrays GUI elements 4Java Programming, Seventh Edition

Introducing Applets (cont’d.) Different from a Java application – Descend from the JApplet class – Run from another application Do not use the java command to execute an applet – Do not contain a main() method – Do not set a default close operation – Cannot delete, read, or create files on the user’s system – Cannot run any other program on the user’s system 5Java Programming, Seventh Edition

Understanding the JApplet Class Import JApplet – import javax.swing.JApplet; JApplet – A Swing class from which you can inherit 6Java Programming, Seventh Edition

Running an Applet You run an applet from a document, usually in HTML HTML, or Hypertext Markup Language – A simple language used to create Web pages for the Internet – Contains many commands Two ways to run an applet: – In a Web browser – Using the Applet Viewer appletviewer command 7Java Programming, Seventh Edition

Writing an HTML Document to Host an Applet When you create an applet: – Write the applet in Java Save with a.java file extension – Compile the applet into bytecode using the javac command – Write an HTML document that includes a statement to call your compiled Java class – Load the HTML document into a Web browser or run the Applet Viewer program 8Java Programming, Seventh Edition

Writing an HTML Document to Host an Applet (cont’d.) Web browser – A program that allows the display of HTML documents – Often contains Java applets Untrusted code carries the possibility of doing harm – Applet code is not trusted Sandbox – A safe area in which applet code can run 9Java Programming, Seventh Edition

Writing an HTML Document to Host an Applet (cont’d.) Run an applet from within an HTML document: Three object tag attributes: – code The name of the compiled applet – width – height 10Java Programming, Seventh Edition

Writing an HTML Document to Host an Applet (cont’d.) Pixels – Set with width and height and tag set – Can be used instead of the tag – It’s better to use the tag 11Java Programming, Seventh Edition

Using the init() Method JApplet class methods – Invoked by the Web browser when the browser runs an applet public void init() public void start() public void paint() public void stop() public void destroy() 12Java Programming, Seventh Edition

Using the init() Method (cont’d.) 13Java Programming, Seventh Edition

Working with JApplet Components All the techniques that are used with JFrame s can also be used with JApplet s: – Change the font and color of labels – Use layout managers – Add multiple GUI components – Change the background color – Add listeners for user events – Add images and sounds 14Java Programming, Seventh Edition

Understanding the JApplet Life Cycle Applets are popular – Easy to use in Web pages JApplet class – Contains methods automatically called by the browser: init() start() stop() destroy() 15Java Programming, Seventh Edition

The init() Method init() method – Executes when a Web page containing a JApplet is loaded or when the appletviewer command is run 16Java Programming, Seventh Edition

The start() Method start() method – Executes after the init() method – Executes again every time the applet becomes active after it has been inactive 17Java Programming, Seventh Edition

The stop() Method stop() method – Invoked when a user leaves a Web page 18Java Programming, Seventh Edition

The destroy() Method destroy() method – Called when the user closes the browser or the Applet Viewer – Releases any resources JApplet might have allocated Every JApplet has the same life cycle outline 19Java Programming, Seventh Edition

The destroy() Method (cont’d.) 20 Figure The JApplet life cycle Java Programming, Seventh Edition

Understanding Multimedia and Using Images Multimedia – The use of sound, images, graphics, and video in computer programs Java provides extensive multimedia tools: – Java 2D or Java 3D Application Programming Interface (API) – Java Media Framework (JMF) API – Java Sound – Java Advanced Imaging API – Java Speech API 21Java Programming, Seventh Edition

Adding Images to JApplet s Image – A likeness of a person or thing Image formats supported by Java: – Graphics Interchange Format (GIF) – Joint Photographic Experts Group (JPEG) – Portable Network Graphics (PNG) Image class – Provides many of Java’s image capabilities 22Java Programming, Seventh Edition

Adding Images to JApplet s (cont’d.) Declare an Image – companyLogo = getImage(getCodeBase(), "logo.gif"); Use the applet paint() method to display an Image drawImage() method – Is a Graphics method ImageObserver – Can be any object that implements the ImageObserver interface – All Component s inherit this implementation 23Java Programming, Seventh Edition

24 Figure The JCompanyImage JApplet Java Programming, Seventh Edition

25 Figure Output of the JCompanyImage JApplet Java Programming, Seventh Edition Adding Images to JApplet s (cont’d.)

Using ImageIcon s ImageIcon class – Creates images in applications and applets – Is simpler than working with Image You can place an ImageIcon on a Component ImageIcon arrowPicture = new ImageIcon("arrow.gif"); JButton arrowButton = new JButton(arrowPicture); paintIcon() method – Displays ImageIcon images 26Java Programming, Seventh Edition

Adding Sound to JApplet s Java supports sound using methods from the Applet class play() method of the Applet class – The simplest way to retrieve and play a sound – Two forms – Codebase attribute Indicates the filename of the applet’s main class file – getCodeBase() method: AudioClip aClip = new AudioClip(getCodeBase(), "tune.au"); 27Java Programming, Seventh Edition

You Do It Creating an HTML Document to Host an Applet Creating and Running a JApplet Running a JApplet in Your Web Browser Creating a More Complicated JApplet Making the JApplet ’s Button Respond to Events Understanding the Applet Life Cycle Creating an HTML Document to Host the JApplet Displaying Images Playing Sounds 28Java Programming, Seventh Edition

Don’t Do It Don’t forget a matching closing tag for every opening tag in an HTML document Don’t forget to use the.class extension with the name of a JApplet Don’t add a main() method to a JApplet Don’t try to execute an applet using the java command 29Java Programming, Seventh Edition

Summary Applets – Programs called from within another application – Run within a Web page or within another program called Applet Viewer Applet life cycle – init() – start() – stop() – destroy() 30Java Programming, Seventh Edition

Summary (cont’d.) 31Java Programming, Seventh Edition Images – Image – ImageIcon Sounds – Applet