Você está na página 1de 41

Chapter 19 – Multimedia: Images, 1

Animation and Audio


Outline
19.1   Introduction
19.2   Loading, Displaying and Scaling Images
19.3   Animating a Series of Images
19.4   Image Maps
19.5   Loading and Playing Audio Clips
19.6   Internet and World Wide Web Resources
19.7   (Optional Case Study) Thinking About Objects: Animation
and Sound in the View

 2003 Prentice Hall, Inc. All rights reserved.


2

19.1 Introduction

• Multimedia
– Use of sound, image, graphics and video
– Makes applications “come alive”

 2003 Prentice Hall, Inc. All rights reserved.


3

19.1 Introduction (cont.)

• This chapter focuses on


– Image-manipulation basics
– Creating smooth animations
– Playing audio files (via AudioClip interface)
– Creating image maps

 2003 Prentice Hall, Inc. All rights reserved.


4

19.2 Loading, Displaying and Scaling Images

• Demonstrate some Java multimedia capabilities


– java.awt.Image
• abstract class (cannot be instantiated)
• Can represent several image file formats
– e.g., GIF, JPEG and PNG
– javax.swing.ImageIcon
• Concrete class

 2003 Prentice Hall, Inc. All rights reserved.


1 // Fig. 19.1: LoadImageAndScale.java Outline
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.
4 import java.applet.Applet; LoadImageAndSca
5 import java.awt.*;
6 import javax.swing.*;
le.java
7
8 public class LoadImageAndScale extends JApplet { Line 15
9 private Image logo1;
10 private ImageIcon logo2;
Objects of class Image Line 16
11
12 // load image when applet is loaded
must be created via
13 public void init() method getImage Line 22
14 {
15 logo1 = getImage( getDocumentBase(), "logo.gif" ); Line 25
16 logo2 = new ImageIcon( "logo.gif" );
17 }
18
Objects of class ImageIcon may be
19 // display image createddrawImage
Method via ImageIcon constructor
displays
20 public void paint( Graphics g ) Image object on screen
21 {
22 Overloaded
g.drawImage( logo1, 0, 0, this ); // drawmethod drawImage
original image
23 displays scaled Image object on screen
24 // draw image to fit the width and the height less 120 pixels
25 g.drawImage( logo1, 0, 120, getWidth(), getHeight() - 120, this );

 2003 Prentice Hall, Inc.


All rights reserved.
26 Outline
27 // draw icon using its paintIcon method
28 logo2.paintIcon( this, g, 180, 0 );
29 } Method paintIcon displays LoadImageAndSca
30 ImageIcon object on screen le.java
31 } // end class LoadImageAndScale
Line 28

Program Output

 2003 Prentice Hall, Inc.


All rights reserved.
7

19.3 Animating a Series of Images

• Demonstrate animating series of images


– Images are stored in array

 2003 Prentice Hall, Inc. All rights reserved.


1 // Fig. 19.2: LogoAnimator.java Outline
2 // Animation of a series of images.
3 import java.awt.*;
4 import java.awt.event.*; LogoAnimator.ja
5 import javax.swing.*;
6
va
7 public class LogoAnimator extends JPanel implements ActionListener {
8 Line 23
9 private final static String IMAGE_NAME = "deitel"; // base image name
10 protected ImageIcon images[]; // array of images
Lines 26 and 28
11
12 private int totalImages = 30; // number of images
13 private int currentImage = 0; // current image index
14 private int animationDelay = 50; // millisecond delay
15 private int width; // image width
16 private int height; // image height
17
18 private Timer animationTimer; // Timer drives animation
19
20 // initialize LogoAnimator by loading images
21 public LogoAnimator() Create array that stores series
22 { of ImageIcon objects
23 images = new ImageIcon[ totalImages ];
24
25 // load images
26 for ( int count = 0; count < images.length; ++count )
27 images[ count ] = new ImageIcon( getClass().getResource( Load ImageIcon
28 "images/" + IMAGE_NAME + count + ".gif" ) );
objects into array

 2003 Prentice Hall, Inc.


All rights reserved.
29 Outline
30 // this example assumes all images have the same width and height
31 width = images[ 0 ].getIconWidth(); // get icon width
32 height = images[ 0 ].getIconHeight(); // get icon height LogoAnimator.ja
33 }
34
va
35 // display current image
36 public void paintComponent( Graphics g ) Override Lines
method36-45
37 { paintComponent of class
38 super.paintComponent( g ); JPanel to display ImageIcons
Lines 48-51
39
40 images[ currentImage ].paintIcon( this, g, 0, 0 );
41
42 // move to next image only if timer is running
43 if ( animationTimer.isRunning() )
44 currentImage = ( currentImage + 1 ) % totalImages;
45 }
46
47 // respond to Timer's event
48 public void actionPerformed( ActionEvent actionEvent )
49 {
Timer invokes method
50 repaint(); // repaint animator actionPerformed every
51 } animationDelay (50)
52 milliseconds
53 // start or restart animation
54 public void startAnimation()
55 {

 2003 Prentice Hall, Inc.


All rights reserved.
56 if ( animationTimer == null ) { Outline
57 currentImage = 0;
58 animationTimer = new Timer( animationDelay, this );
59 animationTimer.start(); LogoAnimator.ja
60 }
61 else // continue from last image displayed
va
62 if ( ! animationTimer.isRunning() )
63 animationTimer.restart(); Line 69
64 }
65
66 // stop animation timer
67 public void stopAnimation()
68 { Method stop indicates
69 animationTimer.stop(); that Timer should stop
70 }
generating events
71
72 // return minimum size of animation
73 public Dimension getMinimumSize()
74 {
75 return getPreferredSize();
76 }
77
78 // return preferred size of animation
79 public Dimension getPreferredSize()
80 {
81 return new Dimension( width, height );
82 }
83

 2003 Prentice Hall, Inc.


All rights reserved.
84 // execute animation in a JFrame Outline
85 public static void main( String args[] )
86 {
87 LogoAnimator animation = new LogoAnimator(); // create LogoAnimator LogoAnimator.ja
88
89 JFrame window = new JFrame( "Animator test" ); // set up window
va
90 window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
91
92 Container container = window.getContentPane();
93 container.add( animation );
94
95 window.pack(); // make window just large enough for its GUI
96 window.setVisible( true ); // display window
97 animation.startAnimation(); // begin animation
98
99 } // end method main
100
101 } // end class LogoAnimator

 2003 Prentice Hall, Inc.


All rights reserved.
12

19.4 Image Maps

• Image map
– Contains hot areas
• Message appears when user moves cursor over these areas

 2003 Prentice Hall, Inc. All rights reserved.


1 // Fig. 19.3: ImageMap.java Outline
2 // Demonstrating an image map.
3 import java.awt.*;
4 import java.awt.event.*; ImageMap.java
5 import javax.swing.*;
6
7 public class ImageMap extends JApplet { Lines 18-30
8 private ImageIcon mapImage;
9
10 private static final String captions[] = { "Common Programming Error",
11 "Good Programming Practice", "Graphical User Interface Tip",
12 "Performance Tip", "Portability Tip",
13 "Software Engineering Observation", "Error-Prevention Tip" };
14
15 // set up mouse listeners
16 public void init()
17 { Add MouseListener for when
18 addMouseListener( mouse pointer exits applet area
19
20 new MouseAdapter() { // anonymous inner class
21
22 // indicate when mouse pointer exits applet area
23 public void mouseExited( MouseEvent event )
24 {
25 showStatus( "Pointer outside applet" );
26 }
27
28 } // end anonymous inner class
29
30 ); // end call to addMouseListener
 2003 Prentice Hall, Inc.
All rights reserved.
31
Add MouseMotionListener
Outline
32 addMouseMotionListener(
33 for hot areas
34 new MouseMotionAdapter() { // anonymous inner class ImageMap.java
35
36 // determine icon over which mouse appears Lines 32-45
37 public void mouseMoved( MouseEvent event )
38 {
Lines 63-76
39 showStatus( translateLocation(
40 event.getX(), event.getY() ) );
41 }
42
43 } // end anonymous inner class
44
45 ); // end call to addMouseMotionListener
46
47 mapImage = new ImageIcon( "icons.png" ); // get image
48
49 } // end method init
50
51 // display mapImage
52 public void paint( Graphics g )
53 {
54 super.paint( g );
55 mapImage.paintIcon( this, g, 0, 0 );
56 }
57

 2003 Prentice Hall, Inc.


All rights reserved.
58 // return tip caption based on mouse coordinates Test coordinates to determine
Outline
59 public String translateLocation( int x, int y ) the icon over which the mouse
60 {
was positioned
61 // if coordinates outside image, return immediately ImageMap.java
62 if ( x >= mapImage.getIconWidth() || y >= mapImage.getIconHeight() )
63 return "";
64 Line 62
65 // determine icon number (0 - 6)
66 int iconWidth = mapImage.getIconWidth() / 7;
67 int iconNumber = x / iconWidth;
68
69 return captions[ iconNumber ]; // return appropriate icon caption
70 }
71
72 } // end class ImageMap

 2003 Prentice Hall, Inc.


All rights reserved.
Outline

ImageMap.java

Program Output

 2003 Prentice Hall, Inc.


All rights reserved.
Outline

ImageMap.java

Program Output

 2003 Prentice Hall, Inc.


All rights reserved.
18

19.5 Loading and Playing Audio Clips

• Playing audio clips


– Method play of class Applet
– Method play of class AudioClip
– Java’s sound engine
• Supports several audio file formats
– Sun Audio (.au)
– Windows Wave (.wav)
– Macintosh AIFF (.aif or .aiff)
– Musical Instrument Digital Interface (MIDI) (.mid)

 2003 Prentice Hall, Inc. All rights reserved.


1 // Fig. 19.4: LoadAudioAndPlay.java Outline
2 // Load an audio clip and play it.
3
4 import java.applet.*; LoadAudioAndPla
5 import java.awt.*; y.java
6 import java.awt.event.*;
7 import javax.swing.*; Line 10
8
9 public class LoadAudioAndPlay extends JApplet { Declare three
10 private AudioClip sound1, sound2, currentSound; AudioClip objects
11 private JButton playSound, loopSound, stopSound;
12 private JComboBox chooseSound;
13
14 // load the image when the applet begins executing
15 public void init()
16 {
17 Container container = getContentPane();
18 container.setLayout( new FlowLayout() );
19
20 String choices[] = { "Welcome", "Hi" };
21 chooseSound = new JComboBox( choices );
22
23 chooseSound.addItemListener(
24
25 new ItemListener() {
26

 2003 Prentice Hall, Inc.


All rights reserved.
27 // stop sound and change to sound to user's selection Outline
28 public void itemStateChanged( ItemEvent e )
29 {
30 currentSound.stop(); LoadAudioAndPla
31
32 currentSound =
y.java
33 chooseSound.getSelectedIndex() == 0 ? sound1 : sound2;
34 } Lines 62-63
35
36 } // end anonymous inner class
37
38 ); // end addItemListener method call
39
40 container.add( chooseSound );
41
42 // set up button event handler and buttons
43 ButtonHandler handler = new ButtonHandler();
44
45 playSound = new JButton( "Play" );
46 playSound.addActionListener( handler );
47 container.add( playSound );
48
49 loopSound = new JButton( "Loop" );
50 loopSound.addActionListener( handler );
51 container.add( loopSound );

 2003 Prentice Hall, Inc.


All rights reserved.
52 Outline
53 stopSound = new JButton( "Stop" );
54 stopSound.addActionListener( handler ); Method getAudioClip loads
55 container.add( stopSound ); audio file into AudioClip object
LoadAudioAndPla
56
57 // load sounds and set currentSound
y.java
58 sound1 = getAudioClip( getDocumentBase(), "welcome.wav" );
59 sound2 = getAudioClip( getDocumentBase(), "hi.au" ); Lines 68-59
60 currentSound = sound1;
61
62 } // end method init
63
64 // stop the sound when the user switches Web pages
65 public void stop()
66 {
67 currentSound.stop();
68 }
69
70 // private inner class to handle button events
71 private class ButtonHandler implements ActionListener {
72

 2003 Prentice Hall, Inc.


All rights reserved.
73 // process play, loop and stop button events Outline
74 public void actionPerformed( ActionEvent actionEvent )
75 {
76 if ( actionEvent.getSource() == playSound ) LoadAudioAndPla
77 currentSound.play(); Method play starts
y.java
78 playing the audio clip
79 else if ( actionEvent.getSource() == loopSound ) Line 77
80 currentSound.loop(); Method loops plays the
81 audio clip
Linecontinually
80
82 else if ( actionEvent.getSource() == stopSound ) Method stop stops
83 currentSound.stop(); playing the audio clip
84 } Line 83
85
86 } // end class ButtonHandler
87
88 } // end class LoadAudioAndPlay

 2003 Prentice Hall, Inc.


All rights reserved.
23
19.7 (Optional Case Study) Thinking About
Objects: Animation and Sound in the View
• ImagePanel
– Used for objects that are stationary in model
• e.g., Floor, ElevatorShaft
• MovingPanel
– Used for objects that “move” in model
• e.g., Elevator
• AnimatedPanel
– Used for objects that “animate” in model
• e.g., Person, Door, Button, Bell, Light

 2003 Prentice Hall, Inc. All rights reserved.


24

19.7 (Optional Case Study) Thinking About


Objects: Animation and Sound in the View (cont.)

javax.swing.JPanel
ImagePanel 1..*

MovingPanel ElevatorView audio


1..* 1
1 1

1..* 1
AnimatedPanel SoundEffects

graphics view

Fig 19.5 Class diagram of elevator simulation view.

 2003 Prentice Hall, Inc. All rights reserved.


1 // ImagePanel.java Outline
2 // JPanel subclass for positioning and displaying ImageIcon
3 package com.deitel.jhtp5.elevator.view;
4 ImagePanel.java
5 // Java core packages
6 import java.awt.*; Line 13
ImagePanel extends
7 import java.awt.geom.*;
JPanel, so ImagePanel
8 import java.util.*;
can be displayed on screen Line 16
9
10 // Java extension packages
11 import javax.swing.*;
Line 19
12
13 public class ImagePanel extends JPanel { Each ImagePanel Line 25
14 has a unique identifier
15 // identifier
16 private int ID;
17
18 // on-screen position Point2D.Double
19 private Point2D.Double position; offers precision for x-y
20 position coordinate
21 // imageIcon to paint on screen
22 private ImageIcon imageIcon;
23
24 // stores all ImagePanel children
25 private Set panelChildren; Set of ImagePanel children
26

 2003 Prentice Hall, Inc.


All rights reserved.
27 // constructor initializes position and image Outline
28 public ImagePanel( int identifier, String imageName )
29 {
30 super( null ); // specify null layout ImagePanel.java
31 setOpaque( false ); // make transparent
32 Lines 41-42
33 // set unique identifier
34 ID = identifier;
35
36 // set location
37 position = new Point2D.Double( 0, 0 );
38 setLocation( 0, 0 );
Use imageName argument to
39 instantiate ImageIcon that
40 // create ImageIcon with given imageName will be displayed on screen
41 imageIcon = new ImageIcon(
42 getClass().getResource( imageName ) );
43
44 Image image = imageIcon.getImage();
45 setSize(
46 image.getWidth( this ), image.getHeight( this ) );
47
48 // create Set to store Panel children
49 panelChildren = new HashSet();
50
51 } // end ImagePanel constructor
52

 2003 Prentice Hall, Inc.


All rights reserved.
53 // paint Panel to screen Display ImagePanelOutline
(and
54 public void paintComponent( Graphics g ) its ImageIcon) to screen
55 {
56 super.paintComponent( g ); ImagePanel.java
57
58 // if image is ready, paint it to screen Lines 54-60
59 imageIcon.paintIcon( this, g, 0, 0 );
60 }
Lines 63-67
61
62 // add ImagePanel child to ImagePanel
63 public void add( ImagePanel panel )
Override method
Lines to
add70-74
64 { add ImagePanel child
65 panelChildren.add( panel );
66 super.add( panel );
67 }
68
69 // add ImagePanel child to ImagePanel at given index
Overload method add to
70 public void add( ImagePanel panel, int index )
71 {
add ImagePanel child
72 panelChildren.add( panel );
73 super.add( panel, index );
74 }
75

 2003 Prentice Hall, Inc.


All rights reserved.
76 // remove ImagePanel child from ImagePanel Outline
Override method remove to
77 public void remove( ImagePanel panel )
78 { remove ImagePanel child
79 panelChildren.remove( panel ); ImagePanel.java
80 super.remove( panel );
81 } Lines 77-81
82
83 // sets current ImageIcon to be displayed
84 public void setIcon( ImageIcon icon )
85 {
86 imageIcon = icon;
87 }
88
89 // set on-screen position
90 public void setPosition( double x, double y )
91 {
92 position.setLocation( x, y );
93 setLocation( ( int ) x, ( int ) y );
94 }
95
96 // return ImagePanel identifier
97 public int getID()
98 {
99 return ID;
100 }
101

 2003 Prentice Hall, Inc.


All rights reserved.
102 // get position of ImagePanel Outline
103 public Point2D.Double getPosition()
104 {
105 return position; ImagePanel.java
106 }
107 Lines 103-118
108 // get imageIcon
109 public ImageIcon getImageIcon()
110 {
111 return imageIcon; Accessor methods
112 }
113
114 // get Set of ImagePanel children
115 public Set getChildren()
116 {
117 return panelChildren;
118 }
119 }

 2003 Prentice Hall, Inc.


All rights reserved.
1 // MovingPanel.java Outline
2 // JPanel subclass with on-screen moving capabilities
3 package com.deitel.jhtp5.elevator.view;
4 MovingPanel.jav
5 // Java core packages a
6 import java.awt.*;
7 import java.awt.geom.*; Line 13
8 import java.util.*;
9 MovingPanel represents Lines 20-21
10 // Java extension packages moving object in model
11 import javax.swing.*;
12
13 public class MovingPanel extends ImagePanel {
14
15 // should MovingPanel change position?
16 private boolean moving;
17
18 // number of pixels MovingPanel moves in both x and y values
19 // per animationDelay milliseconds Use double to represent
20 private double xVelocity; velocity with high-precision
21 private double yVelocity;
22
23 // constructor initializes position, velocity and image
24 public MovingPanel( int identifier, String imageName )
25 {
26 super( identifier, imageName );
27

 2003 Prentice Hall, Inc.


All rights reserved.
28 // set MovingPanel velocity
29 xVelocity = 0; Outline
30 yVelocity = 0;
31
32 } // end MovingPanel constructor MovingPanel.jav
33 a
34 // update MovingPanel position and animation frame
35 public void animate()
36 { Lines 38-52
37 // update position according to MovingPanel velocity
If MovingPanel is moving,
38 if ( isMoving() ) { update MovingPanel
39 double oldXPosition = getPosition().getX(); position, as well as position of
40 double oldYPosition = getPosition().getY(); MovingPanel’s children
41
42 setPosition( oldXPosition + xVelocity,
43 oldYPosition + yVelocity );
44 }
45
46 // update all children of MovingPanel
47 Iterator iterator = getChildren().iterator();
48
49 while ( iterator.hasNext() ) {
50 MovingPanel panel = ( MovingPanel ) iterator.next();
51 panel.animate();
52 }
53 } // end method animate
54
55 // is MovingPanel moving on screen?
56 public boolean isMoving()
57 {
58 return moving;
59 }  2003 Prentice Hall, Inc.
60 All rights reserved.
61 // set MovingPanel to move on screen Outline
62 public void setMoving( boolean move )
63 {
64 moving = move; MovingPanel.jav
65 } a
66
67 // set MovingPanel x and y velocity Lines 68-84
68 public void setVelocity( double x, double y )
69 {
70 xVelocity = x;
71 yVelocity = y;
72 }
73
74 // return MovingPanel x velocity
75 public double getXVelocity()
76 {
77 return xVelocity;
78 }
79
80 // return MovingPanel y velocity
81 public double getYVelocity()
82 {
83 return yVelocity;
84 }
85 }

 2003 Prentice Hall, Inc.


All rights reserved.
1 // AnimatedPanel.java Outline
2 // MovingPanel subclass with animation capabilities
3 package com.deitel.jhtp5.elevator.view;
4 AnimatedPanel.j
5 // Java core packages ava
6 import java.awt.*;
AnimatedPanel
7 import java.util.*; Line 12
represents moving object
8
9 // Java extension packages
in model and has several
frames of animation Lines 19-20
10 import javax.swing.*;
11
12 public class AnimatedPanel extends MovingPanel { Line 23
13
14 // should ImageIcon cycle frames Lines 26-27
15 private boolean animating;
16
17 // frame cycle rate (i.e., rate advancing to next frame) ImageIcon array that
Variables to control stores images in an
18 private int animationRate;
19 private int animationRateCounter;
animation rate animation sequence
20 private boolean cycleForward = true;
21
22 // individual ImageIcons used for animation frames
23 private ImageIcon imageIcons[];
24
25 // storage for all frame sequences
26 private java.util.List frameSequences; Variables to determine which
27 private int currentAnimation; frame of animation to display

 2003 Prentice Hall, Inc.


All rights reserved.
28 Outline
29 // should loop (continue) animation at end of cycle?
30 private boolean loop;
31 AnimatedPanel.j
32 // should animation display last frame at end of animation?
33 private boolean displayLastFrame;
ava
34
35 // helps determine next displayed frame Lines 44-49
36 private int currentFrameCounter;
37
Lines 56-70
38 // constructor takes array of filenames and screen position
39 public AnimatedPanel( int identifier, String imageName[] )
40 {
41 super( identifier, imageName[ 0 ] );
42
43 // creates ImageIcon objects from imageName string array
44 imageIcons = new ImageIcon[ imageName.length ];
45 AnimatedPanel constructor
46 for ( int i = 0; i < imageIcons.length; i++ ) {
creates ImageIcon array from
47 imageIcons[ i ] = new ImageIcon(
48 getClass().getResource( imageName[ i ] ) ); String array argument, which
49 } contains names of image files
50
51 frameSequences = new ArrayList();
52
53 } // end AnimatedPanel constructor
54 Override method animate of
55 // update icon position and animation frame
56 public void animate()
class MovingPanel to update
57 { AnimatedPanel position and
58 super.animate(); current frame of animation
 2003 Prentice Hall, Inc.
All rights reserved.
59 Outline
60 // play next animation frame if counter > animation rate
61 if ( frameSequences != null && isAnimating() ) { Play next frame of
62 AnimatedPanel.j
animation
63 if ( animationRateCounter > animationRate ) { ava
64 animationRateCounter = 0;
65 determineNextFrame(); Lines 61-69
66 }
67 else
Lines 73-99
68 animationRateCounter++;
69 }
70 } // end method animate Lines 84-93
71
72 // determine next animation frame
Utility method that determines
73 private void determineNextFrame()
next frame of animation to display
74 {
75 int frameSequence[] =
76 ( int[] ) frameSequences.get( currentAnimation );
77
78 // if no more animation frames, determine final frame,
79 // unless loop is specified
80 if ( currentFrameCounter >= frameSequence.length ) {
81 currentFrameCounter = 0; Used for looping purposes in animation: If loop is
82 false, animation terminates after one iteration
83 // if loop is false, terminate animation
84 if ( !isLoop() ) {
85
86 setAnimating( false );
87  2003 Prentice Hall, Inc.
All rights reserved.
88 if ( isDisplayLastFrame() ) Last frame in sequence is displayed if
Outline
89 displayLastFrame is true,
90 // display last frame in sequence
and first frame in sequence is
91 currentFrameCounter = frameSequence.length - 1; AnimatedPanel.j
92 }
displayed if displayLastFrame
93 }
ava
is false
94
95 // set current animation frame Lines 88-91
96 setCurrentFrame( frameSequence[ currentFrameCounter ] );
97 currentFrameCounter++;
Lines 96-97
98 Call method setCurrent-
99 } // end method determineNextFrame Frame to set ImageIcon
100
101
(current image displayed) to the
// add frame sequence (animation) to frameSequences ArrayList
102 public void addFrameSequence( int frameSequence[]ImageIcon
) returned from the
103 { current frame sequence.
104 frameSequences.add( frameSequence );
105 }
106
107 // ask if AnimatedPanel is animating (cycling frames)
108 public boolean isAnimating()
109 {
110 return animating;
111 }
112
113 // set AnimatedPanel to animate
114 public void setAnimating( boolean animate )
115 {
116 animating = animate;
117 }
 2003 Prentice Hall, Inc.
All rights reserved.
118 Outline
119 // set current ImageIcon
120 public void setCurrentFrame( int frame )
121 { AnimatedPanel.j
122 setIcon( imageIcons[ frame ] );
123 }
ava
124
125 // set animation rate
126 public void setAnimationRate( int rate )
127 {
128 animationRate = rate;
129 }
130
131 // get animation rate
132 public int getAnimationRate()
133 {
134 return animationRate;
135 }
136
137 // set whether animation should loop
138 public void setLoop( boolean loopAnimation )
139 {
140 loop = loopAnimation;
141 }
142
143 // get whether animation should loop
144 public boolean isLoop()
145 {
146 return loop;
147 }
148  2003 Prentice Hall, Inc.
All rights reserved.
149 // get whether to display last frame at animation end Outline
150 private boolean isDisplayLastFrame()
151 {
152 return displayLastFrame; AnimatedPanel.j
153 } ava
154
155 // set whether to display last frame at animation end Lines 162-167
156 public void setDisplayLastFrame( boolean displayFrame )
157 {
158 displayLastFrame = displayFrame;
159 }
160
161 // start playing animation sequence of given index
162 public void playAnimation( int frameSequence ) Begin animation
163 {
164 currentAnimation = frameSequence;
165 currentFrameCounter = 0;
166 setAnimating( true );
167 }
168 }

 2003 Prentice Hall, Inc.


All rights reserved.
19.7 (Optional Case Study) Thinking About 39

Objects: Animation and Sound in the View


(Cont.)

frameSequences image sequences

imageIcons 0= 0 1 2 A B C

1= 0 1 3 1 0 A B D B A
A B C D

0 1 2 3 2= 2 1 0 C B A

3= 3 2 2 0 D C C A

Fig. 19.9 Relationship between array imageIcons and List frameSequences.

 2003 Prentice Hall, Inc. All rights reserved.


1 // SoundEffects.java Outline
2 // Returns AudioClip objects
3 package com.deitel.jhtp5.elevator.view;
4 SoundEffects.ja
5 // Java core packages Creates sound effects va
6 import java.applet.*; (AudioClips) for view
7 Line 8
8 public class SoundEffects {
9
Lines 19-20
10 // location of sound files
11 private String prefix = "";
12
Pass soundFile parameter to
13 public SoundEffects() {} static method newAudioClip
14 (of class java.applet.Applet)
15 // get AudioClip associated with soundFile to return AudioClip object
16 public AudioClip getAudioClip( String soundFile )
17 {
18 try {
19 return Applet.newAudioClip( getClass().getResource(
20 prefix + soundFile ) );
21 }
22
23 // return null if soundFile does not exist
24 catch ( NullPointerException nullPointerException ) {
25 return null;
26 }
27 }

 2003 Prentice Hall, Inc.


All rights reserved.
28 Outline
29 // set prefix for location of soundFile
30 public void setPathPrefix( String string )
31 { SoundEffects.ja
32 prefix = string; va
33 }
34 }

 2003 Prentice Hall, Inc.


All rights reserved.

Você também pode gostar