1 / 41

Multimedia Basics: Images, Animation, and Audio in Java

Learn how to load, display, and scale images, create smooth animations, and play audio clips in Java applications. Explore image maps and discover resources on the internet and World Wide Web.

wmarlene
Download Presentation

Multimedia Basics: Images, Animation, and Audio in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 19 – Multimedia: Images, Animation and Audio Outline 19.1   Introduction19.2   Loading, Displaying and Scaling Images19.3   Animating a Series of Images19.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

  2. 19.1 Introduction • Multimedia • Use of sound, image, graphics and video • Makes applications “come alive”

  3. 19.1 Introduction (cont.) • This chapter focuses on • Image-manipulation basics • Creating smooth animations • Playing audio files (via AudioClip interface) • Creating image maps

  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

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

  6. Method paintIcon displays ImageIcon object on screen 26 27 // draw icon using its paintIcon method 28 logo2.paintIcon( this, g, 180, 0 ); 29 } 30 31 } // end class LoadImageAndScale LoadImageAndScale.javaLine 28Program Output

  7. 19.3 Animating a Series of Images • Demonstrate animating series of images • Images are stored in array

  8. Load ImageIcon objects into array Create array that stores series of ImageIcon objects 1 // Fig. 19.2: LogoAnimator.java 2 // Animation of a series of images. 3 import java.awt.*; 4 import java.awt.event.*; 5 import javax.swing.*; 6 7 publicclass LogoAnimator extends JPanel implements ActionListener { 8 9 privatefinalstatic String IMAGE_NAME = "deitel"; // base image name 10 protected ImageIcon images[]; // array of images 11 12 privateint totalImages = 30; // number of images 13 privateint currentImage = 0; // current image index 14 privateint animationDelay = 50; // millisecond delay 15 privateint width; // image width 16 privateint height; // image height 17 18 private Timer animationTimer; // Timer drives animation 19 20 // initialize LogoAnimator by loading images 21 public LogoAnimator() 22 { 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( 28 "images/" + IMAGE_NAME + count + ".gif" ) ); LogoAnimator.javaLine 23Lines 26 and 28

  9. Override method paintComponent of class JPanel to display ImageIcons Timer invokes method actionPerformed every animationDelay (50) milliseconds 29 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 33 } 34 35 // display current image 36 publicvoid paintComponent( Graphics g ) 37 { 38 super.paintComponent( g ); 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 publicvoid actionPerformed( ActionEvent actionEvent ) 49 { 50 repaint(); // repaint animator 51 } 52 53 // start or restart animation 54 publicvoid startAnimation() 55 { LogoAnimator.javaLines 36-45Lines 48-51

  10. Method stop indicates that Timer should stop generating events 56 if ( animationTimer == null ) { 57 currentImage = 0; 58 animationTimer = new Timer( animationDelay, this ); 59 animationTimer.start(); 60 } 61 else// continue from last image displayed 62 if ( ! animationTimer.isRunning() ) 63 animationTimer.restart(); 64 } 65 66 // stop animation timer 67 publicvoid stopAnimation() 68 { 69 animationTimer.stop(); 70 } 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 returnnew Dimension( width, height ); 82 } 83 LogoAnimator.javaLine 69

  11. 84 // execute animation in a JFrame 85 publicstaticvoid main( String args[] ) 86 { 87 LogoAnimator animation = new LogoAnimator(); // create LogoAnimator 88 89 JFrame window = new JFrame( "Animator test" ); // set up window 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 LogoAnimator.java

  12. 19.4 Image Maps • Image map • Contains hot areas • Message appears when user moves cursor over these areas

  13. Add MouseListener for when mouse pointer exits applet area 1 // Fig. 19.3: ImageMap.java 2 // Demonstrating an image map. 3 import java.awt.*; 4 import java.awt.event.*; 5 import javax.swing.*; 6 7 publicclass ImageMap extends JApplet { 8 private ImageIcon mapImage; 9 10 privatestaticfinal 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 publicvoid init() 17 { 18 addMouseListener( 19 20 new MouseAdapter() { // anonymous inner class 21 22 // indicate when mouse pointer exits applet area 23 publicvoid mouseExited( MouseEvent event ) 24 { 25 showStatus( "Pointer outside applet" ); 26 } 27 28 } // end anonymous inner class 29 30 ); // end call to addMouseListener ImageMap.javaLines 18-30

  14. Add MouseMotionListener for hot areas 31 32 addMouseMotionListener( 33 34 new MouseMotionAdapter() { // anonymous inner class 35 36 // determine icon over which mouse appears 37 publicvoid mouseMoved( MouseEvent event ) 38 { 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 publicvoid paint( Graphics g ) 53 { 54 super.paint( g ); 55 mapImage.paintIcon( this, g, 0, 0 ); 56 } 57 ImageMap.javaLines 32-45Lines 63-76

  15. Test coordinates to determine the icon over which the mouse was positioned 58 // return tip caption based on mouse coordinates 59 public String translateLocation( int x, int y ) 60 { 61 // if coordinates outside image, return immediately 62 if ( x >= mapImage.getIconWidth() || y >= mapImage.getIconHeight() ) 63 return""; 64 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 ImageMap.javaLine 62

  16. ImageMap.javaProgram Output

  17. ImageMap.javaProgram Output

  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)

  19. Declare three AudioClip objects 1 // Fig. 19.4: LoadAudioAndPlay.java 2 // Load an audio clip and play it. 3 4 import java.applet.*; 5 import java.awt.*; 6 import java.awt.event.*; 7 import javax.swing.*; 8 9 publicclass LoadAudioAndPlay extends JApplet { 10 private AudioClip sound1, sound2, currentSound; 11 private JButton playSound, loopSound, stopSound; 12 private JComboBox chooseSound; 13 14 // load the image when the applet begins executing 15 publicvoid 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 LoadAudioAndPlay.javaLine 10

  20. 27 // stop sound and change to sound to user's selection 28 publicvoid itemStateChanged( ItemEvent e ) 29 { 30 currentSound.stop(); 31 32 currentSound = 33 chooseSound.getSelectedIndex() == 0 ? sound1 : sound2; 34 } 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 ); LoadAudioAndPlay.javaLines 62-63

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

  22. Method stop stops playing the audio clip Method play starts playing the audio clip Method loops plays the audio clip continually 73 // process play, loop and stop button events 74 publicvoid actionPerformed( ActionEvent actionEvent ) 75 { 76 if ( actionEvent.getSource() == playSound ) 77 currentSound.play(); 78 79 elseif ( actionEvent.getSource() == loopSound ) 80 currentSound.loop(); 81 82 elseif ( actionEvent.getSource() == stopSound ) 83 currentSound.stop(); 84 } 85 86 } // end class ButtonHandler 87 88 } // end class LoadAudioAndPlay LoadAudioAndPlay.javaLine 77Line 80Line 83

  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

  24. ElevatorView 19.7 (Optional Case Study) Thinking About Objects: Animation and Sound in the View (cont.) javax.swing.JPanel 1..* ImagePanel 1 MovingPanel audio 1 1..* 1 1 1..* 1 SoundEffects AnimatedPanel view graphics Fig 19.5 Class diagram of elevator simulation view.

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

  26. Use imageName argument to instantiate ImageIcon that will be displayed on screen 27 // constructor initializes position and image 28 public ImagePanel( int identifier, String imageName ) 29 { 30 super( null ); // specify null layout 31 setOpaque( false ); // make transparent 32 33 // set unique identifier 34 ID = identifier; 35 36 // set location 37 position = new Point2D.Double( 0, 0 ); 38 setLocation( 0, 0 ); 39 40 // create ImageIcon with given imageName 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 ImagePanel.javaLines 41-42

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

  28. Override method remove to remove ImagePanel child 76 // remove ImagePanel child from ImagePanel 77 public void remove( ImagePanel panel ) 78 { 79 panelChildren.remove( panel ); 80 super.remove( panel ); 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 ImagePanel.javaLines 77-81

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

  30. MovingPanel represents moving object in model Use double to represent velocity with high-precision 1 // MovingPanel.java 2 // JPanel subclass with on-screen moving capabilities 3 package com.deitel.jhtp5.elevator.view; 4 5 // Java core packages 6 import java.awt.*; 7 import java.awt.geom.*; 8 import java.util.*; 9 10 // Java extension packages 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 20 private double xVelocity; 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 MovingPanel.javaLine 13Lines 20-21

  31. If MovingPanel is moving, update MovingPanel position, as well as position of MovingPanel’s children 28 // set MovingPanel velocity 29 xVelocity = 0; 30 yVelocity = 0; 31 32 } // end MovingPanel constructor 33 34 // update MovingPanel position and animation frame 35 public void animate() 36 { 37 // update position according to MovingPanel velocity 38 if ( isMoving() ) { 39 double oldXPosition = getPosition().getX(); 40 double oldYPosition = getPosition().getY(); 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 } 60 MovingPanel.javaLines 38-52

  32. 61 // set MovingPanel to move on screen 62 public void setMoving( boolean move ) 63 { 64 moving = move; 65 } 66 67 // set MovingPanel x and y velocity 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 } MovingPanel.javaLines 68-84

  33. AnimatedPanel represents moving object in model and has several frames of animation ImageIcon array that stores images in an animation sequence Variables to determine which frame of animation to display Variables to control animation rate 1 // AnimatedPanel.java 2 // MovingPanel subclass with animation capabilities 3 package com.deitel.jhtp5.elevator.view; 4 5 // Java core packages 6 import java.awt.*; 7 import java.util.*; 8 9 // Java extension packages 10 import javax.swing.*; 11 12 public class AnimatedPanel extends MovingPanel { 13 14 // should ImageIcon cycle frames 15 private boolean animating; 16 17 // frame cycle rate (i.e., rate advancing to next frame) 18 private int animationRate; 19 private int animationRateCounter; 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; 27 privateint currentAnimation; AnimatedPanel.javaLine 12Lines 19-20Line 23Lines 26-27

  34. AnimatedPanel constructor creates ImageIcon array from String array argument, which contains names of image files Override method animate of class MovingPanel to update AnimatedPanel position and current frame of animation 28 29 // should loop (continue) animation at end of cycle? 30 private boolean loop; 31 32 // should animation display last frame at end of animation? 33 private boolean displayLastFrame; 34 35 // helps determine next displayed frame 36 private int currentFrameCounter; 37 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 46 for ( int i = 0; i < imageIcons.length; i++ ) { 47 imageIcons[ i ] = new ImageIcon( 48 getClass().getResource( imageName[ i ] ) ); 49 } 50 51 frameSequences = new ArrayList(); 52 53 } // end AnimatedPanel constructor 54 55 // update icon position and animation frame 56 public void animate() 57 { 58 super.animate(); AnimatedPanel.javaLines 44-49Lines 56-70

  35. Utility method that determines next frame of animation to display Used for looping purposes in animation: If loop is false, animation terminates after one iteration Play next frame of animation 59 60 // play next animation frame if counter > animation rate 61 if ( frameSequences != null && isAnimating() ) { 62 63 if ( animationRateCounter > animationRate ) { 64 animationRateCounter = 0; 65 determineNextFrame(); 66 } 67 else 68 animationRateCounter++; 69 } 70 } // end method animate 71 72 // determine next animation frame 73 private void determineNextFrame() 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; 82 83 // if loop is false, terminate animation 84 if ( !isLoop() ) { 85 86 setAnimating( false ); 87 AnimatedPanel.javaLines 61-69Lines 73-99Lines 84-93

  36. Last frame in sequence is displayed if displayLastFrame is true, and first frame in sequence is displayed if displayLastFrame is false Call method setCurrent-Frame to set ImageIcon (current image displayed) to the ImageIcon returned from the current frame sequence. 88 if ( isDisplayLastFrame() ) 89 90 // display last frame in sequence 91 currentFrameCounter = frameSequence.length - 1; 92 } 93 } 94 95 // set current animation frame 96 setCurrentFrame( frameSequence[ currentFrameCounter ] ); 97 currentFrameCounter++; 98 99 } // end method determineNextFrame 100 101 // add frame sequence (animation) to frameSequences ArrayList 102 public void addFrameSequence( int frameSequence[] ) 103 { 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 } AnimatedPanel.javaLines 88-91Lines 96-97

  37. 118 119 // set current ImageIcon 120 public void setCurrentFrame( int frame ) 121 { 122 setIcon( imageIcons[ frame ] ); 123 } 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 AnimatedPanel.java

  38. Begin animation 149 // get whether to display last frame at animation end 150 private boolean isDisplayLastFrame() 151 { 152 return displayLastFrame; 153 } 154 155 // set whether to display last frame at animation end 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 ) 163 { 164 currentAnimation = frameSequence; 165 currentFrameCounter = 0; 166 setAnimating( true ); 167 } 168 } AnimatedPanel.javaLines 162-167

  39. frameSequences image sequences A D B A B A C C B B A D D C A C C B A imageIcons 0 1 2 0= 1= 0 1 3 1 0 2= 2 1 0 0 1 2 3 3= 3 2 2 0 19.7 (Optional Case Study) Thinking About Objects: Animation and Sound in the View (Cont.) Fig. 19.9 Relationship between array imageIcons and ListframeSequences.

  40. Creates sound effects (AudioClips) for view Pass soundFile parameter to static method newAudioClip (of class java.applet.Applet) to return AudioClip object 1 // SoundEffects.java 2 // Returns AudioClip objects 3 package com.deitel.jhtp5.elevator.view; 4 5 // Java core packages 6 import java.applet.*; 7 8 public class SoundEffects { 9 10 // location of sound files 11 private String prefix = ""; 12 13 public SoundEffects() {} 14 15 // get AudioClip associated with soundFile 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 } SoundEffects.javaLine 8Lines 19-20

  41. 28 29 // set prefix for location of soundFile 30 public void setPathPrefix( String string ) 31 { 32 prefix = string; 33 } 34 } SoundEffects.java

More Related