AhmadLang / Java, How To Program, 2004
.pdf
[Page 975 (continued)]
Terminology
applet
applet container applet HTML element
Applet menu in appletviewer <applet> tag
appletviewer
attribute
demo directory of the JDK height of an applet
.htm file name extension HTML element
.html file name extension HyperText Markup Language (HTML) init method of JApplet
JApplet class
left angle bracket (<) paint method of JApplet
parseDouble method of Double
Quit menu item in appletviewer
Reload menu item in appletviewer right angle bracket (>)
start method of JApplet stop method of JApplet
tag
width of an applet
[Page 975 (continued)]
Self-Review Exercises
20.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 the user of a browser 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.The __________ method is called once when an applet begins execution.
h. The __________ method is invoked to draw on an applet.
i.The __________ 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.
[Page 975 (continued)]
Answers to Self-Review Exercises
20.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>.
[Page 975 (continued)]
Exercises
20.2Write 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. 20.10.
[Page 976]
20.3Write an applet that asks the user to enter two floating-point numbers, obtains the numbers from the user and displays the two numbers first and then the larger number followed by the words "is larger" 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. 20.10.
20.4Write 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. 20.10.
20.5Write 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 p. Use the
techniques shown in Fig. 20.10. [Note: You may also use the predefined constant Math.PI for the value of p. 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):
20.6Write an applet that reads five integers, determines which are the largest and smallest integers in the group and prints them. Use only the programming techniques you learned in this chapter and Chapter 2. Draw the results on the applet.
20.7Write an applet that draws a checkerboard pattern as follows:
20.8Write an applet that draws rectangles of different sizes and locations.
20.9Write 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.
20.10Class Graphics contains method drawOval, which takes as arguments the same four arguments as method drawRect. The arguments for method drawOval specify the "bounding box for the ovalthe 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.
20.11Modify the solution to Exercise 20.10 to output ovals of different shapes and sizes.
20.12Write an applet that allows the user to input the four arguments required by method drawOval, then draws an oval using the four input values.
[Page 977]
Chapter 21. Multimedia: Applets and
Applications
The wheel that squeaks the loudest ... gets the grease.
John Billings (Henry Wheeler Shaw)
We'll use a signal I have tried and found far-reaching and easy to yell. Waa-hoo!
Zane Grey
There is a natural hootchy-kootchy motion to a goldfish.
Walt Disney
Between the motion and the act falls the shadow.
Thomas Stearns Eliot
OBJECTIVES
In this chapter you will learn:
How to get and display images.
To create animations from sequences of images.
To create image maps.
To get, play, loop and stop sounds, using an AudioClip.
To play video using interface Player.
[Page 978]
Outline
21.1 Introduction
21.2 Loading, Displaying and Scaling Images
21.3 Animating a Series of Images
21.4 Image Maps
21.5 Loading and Playing Audio Clips
21.6 Playing Video and Other Media with Java Media Framework
21.7 Wrap-Up
21.8 Internet and Web Resources
Summary
Terminology
Self-Review Exercises
Answers to Self-Review Exercises
Exercises
Special Section: Challenging Multimedia Projects
[Page 978 (continued)]
21.1. Introduction
Welcome to what may be the largest revolution in the history of the computer industry. Those who entered the field decades ago were interested in using computers primarily to perform arithmetic calculations at high speed. As the computer field evolved, we began to realize that the datamanipulation capabilities of computers are equally important. The "sizzle" of Java is multimediathe use of sound, images, graphics and video to make applications "come alive." Although most multimedia in Java applications is two-dimensional, Java programmers already can use the Java 3D API to create substantial 3D graphics applications (Sun provides an online tutorial for the Java 3D API at java.sun.com/developer/onlineTraining/java3d).
Multimedia programming offers many new challenges. The field is already enormous and is growing rapidly. People are rushing to equip their computers for multimedia. Most new computers sold today are "multimedia ready," with CD-RW and DVD drives, audio boards and special video capabilities. Economical desktop and laptop computers are so powerful that they can store and play DVD-quality sound and video, and we expect to see further advances in the kinds of programmable multimedia capabilities available through programming languages. One thing that we have learned is to plan for the "impossible"in the computer and communications fields, the "impossible" has repeatedly become reality.
Among users who want graphics, many now want three-dimensional, high-resolution, color graphics. True three-dimensional imaging may become available within the next decade. Imagine having highresolution, "theater-in-the-round," three-dimensional television. Sporting and entertainment events will seem to take place on your living room floor! Medical students worldwide will see operations being performed thousands of miles away, as if they were occurring in the same room. People will be able to learn how to drive with extremely realistic driving simulators in their homes before they get behind the wheel. The possibilities are exciting and endless.
Multimedia demands extraordinary computing power. Until recently, affordable computers with that kind of power were not available. Today's ultrapowerful processors, like the SPARC Ultra 5 from Sun Microsystems, the Pentium and Itanium 2 from Intel, the PowerPC G5 from IBM and Apple, and the processors from MIPS/Silicon Graphics (among others) make effective multimedia possible. The computer and communications industries will be primary beneficiaries of the multimedia revolution. Users will be willing to pay for the faster processors, larger memories and wider communications bandwidths that support demanding multimedia applications. Ironically, users may not have to pay more, because the fierce competition in these industries has historically driven prices down.
[Page 979]
We need programming languages that make creating multimedia applications easy. Most programming languages do not incorporate such capabilities. However, Java, through its class libraries, provides extensive multimedia facilities that enable you to start developing powerful multimedia applications immediately.
This chapter presents several examples of interesting multimedia features that you will need to build useful applications, including:
1.the basics of manipulating images.
2.creating smooth animations.
3.playing audio files with the AudioClip interface.
4.creating image maps that can sense when the cursor is over them, even without a mouse click.
5.playing video files using the Player interface.
The exercises for this chapter suggest dozens of challenging and interesting projects. When we were creating these exercises, the ideas just kept flowing. Multimedia leverages creativity in ways that we did not experience with "conventional" computer capabilities. [Note: Java's multimedia capabilities go far beyond those presented in this chapter. They include the Java Media Framework (JMF) API (for adding audio and video media to an application), Java Sound API (for playing, recording and modifying audio), Java 3D API (for creating and modifying 3D graphics), Java Advanced Imaging API (for image-processing capabilities, such as cropping and scaling), Java Speech API (for inputting voice
commands from the user or outputting voice commands to the user), Java 2D API (for creating and modifying 2D graphics, covered in Chapter 12) and Java Image I/O API (for reading from and outputting images to files). Section 21.8 provides Web links for each of these APIs.]
[Page 979 (continued)]
21.2. Loading, Displaying and Scaling Images
Java's multimedia capabilities include graphics, images, animations, sounds and video. We begin our discussion with images. We will use a few different images in this chapter. Developers can create such
images with any image software, such as Adobe® Photoshop™, Jasc® Paint Shop Pro™ or Microsoft® Paint.
The applet of Fig. 21.1 demonstrates loading an Image (package java.awt) and loading an ImageIcon (package javax.swing). Both classes are used to load and display images. The applet displays the Image in its original size and scaled to a larger size, using two versions of Graphics method drawImage. The applet also draws the ImageIcon, using the icon's method paintIcon. Class ImageIcon implements interface Serializable, which allows ImageIcon objects to be easily written to a file or sent across the Internet. Class ImageIcon is also easier to use than Image, because its constructor can receive arguments of several different formats, including a byte array containing the bytes of an image, an Image already loaded in memory, and a String or a URL object, which both can be used to represent the location of the image. A URL object represents a Uniform Resource Locator that serves as a pointer to a resource on the World Wide Web, on your computer or on any networked machine. A URL object is more often used when accessing data over the Internet, while a simple string is more often used when accessing data on the current machine. Using an URL object also enables the programmer to access information from the Web, such as searching for information from a database or through a search engine.
[Page 980]
Figure 21.1. Loading and displaying an image in an applet.
(This item is displayed on pages 980 - 981 in the print version)
1 |
// Fig. |
21. |
1: LoadImageAndScale |
.java |
|
|
|
|||
2 |
// |
Load |
an |
image |
and display it |
in |
its |
original size |
and |
twice its |
3 |
// |
original |
size |
. Load and display |
the |
same image as |
an |
ImageIcon. |
||
4import java.awt.Graphics;
5import java.awt.Image;
6import javax.swing.ImageIcon;
7import javax.swing.JApplet;
8
9public class LoadImageAndScale extends JApplet
10{
11private Image image1; // create Image object
12private ImageIcon image2; // create ImageIcon object
14// load image when applet is loaded
15public void init()
16{
17image1 = getImage( getDocumentBase(), "redflowers.png" );
18image2 = new ImageIcon( "yellowflowers.png" );
19} // end method init
20
21// display image
22public void paint( Graphics g )
23{
24super.paint( g );
26g.drawImage( image1, 0, 0, this ); // draw original image
28 |
// draw image |
to fit |
the |
width and the height less 120 |
pixels |
29 |
g.drawImage( |
image1, |
0, |
120, getWidth(), getHeight() - |
120, this ); |
30 |
|
|
|
|
|
31// draw icon using its paintIcon method
32image2.paintIcon( this, g, 180, 0 );
33} // end method paint
34} // end class LoadImageAndScale
