Multimedia: Images, Animation
Transcription
Multimedia: Images, Animation
chpt_16.fm Page 774 Wednesday, February 23, 2000 12:22 PM 774 MULTIMEDIA: IMAGES, ANIMATION, AUDIO AND VIDEO CHAPTER 16 16 Multimed ia: Images, Animation, Audio an d Video 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 / Fig. 16.1: LoadImageAndScale.java // Load an image and display it in its original size // and scale it to twice its original width and height. // Load and display the same image as an ImageIcon. import java.applet.Applet; import java.awt.*; import javax.swing.*; public class LoadImageAndScale extends JApplet { private Image logo1; private ImageIcon logo2; // load the image when the applet is loaded public void init() { logo1 = getImage( getDocumentBase(), "logo.gif" ); logo2 = new ImageIcon( "logo.gif" ); } // display the image public void paint( Graphics g ) { // draw the original image g.drawImage( logo1, 0, 0, this ); // draw the image scaled to fit the width of the applet // and the height of the applet minus 120 pixels g.drawImage( logo1, 0, 120, getWidth(), getHeight() - 120, this ); // draw the icon using its paintIcon method logo2.paintIcon( this, g, 180, 0 ); } } Fig. 16.1 Loading and displaying an image in an applet (part 1 of 2). © Copyright 2000 by Prentice Hall. All Rights Reserved. For use only by instructors in classes for which Java How to Program, Third Edition is the required textbook. chpt_16.fm Page 775 Wednesday, February 23, 2000 12:22 PM CHAPTER 16 Fig. 16.1 MULTIMEDIA: IMAGES, ANIMATION, A UDIO AND VIDEO 775 Loading and displaying an image in an applet (part 2 of 2). © Copyright 2000 by Prentice Hall. All Rights Reserved. For use only by instructors in classes for which Java How to Program, Third Edition is the required textbook. chpt_16.fm Page 776 Wednesday, February 23, 2000 12:22 PM 776 1 2 3 4 5 6 MULTIMEDIA: IMAGES, ANIMATION, AUDIO AND VIDEO // Fig. 16.2: LoadAudioAndPlay.java // Load an audio clip and play it. import java.applet.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; Fig. 16.2 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 CHAPTER 16 Loading and playing an AudioClip (part 1 of 3). public class LoadAudioAndPlay extends JApplet { private AudioClip sound1, sound2, currentSound; private JButton playSound, loopSound, stopSound; private JComboBox chooseSound; // load the image when the applet begins executing public void init() { Container c = getContentPane(); c.setLayout( new FlowLayout() ); String choices[] = { "Welcome", "Hi" }; chooseSound = new JComboBox( choices ); chooseSound.addItemListener( new ItemListener() { public void itemStateChanged( ItemEvent e ) { currentSound.stop(); currentSound = chooseSound.getSelectedIndex() == 0 ? sound1 : sound2; } } ); c.add( chooseSound ); ButtonHandler handler = new ButtonHandler(); playSound = new JButton( "Play" ); playSound.addActionListener( handler ); c.add( playSound ); loopSound = new JButton( "Loop" ); loopSound.addActionListener( handler ); c.add( loopSound ); stopSound = new JButton( "Stop" ); stopSound.addActionListener( handler ); c.add( stopSound ); sound1 = getAudioClip( getDocumentBase(), "welcome.wav" ); sound2 = getAudioClip( getDocumentBase(), "hi.au" ); currentSound = sound1; © Copyright 2000 by Prentice Hall. All Rights Reserved. For use only by instructors in classes for which Java How to Program, Third Edition is the required textbook. chpt_16.fm Page 777 Wednesday, February 23, 2000 12:22 PM CHAPTER 16 51 52 53 54 55 56 57 58 59 777 } // stop the sound when the user switches Web pages // (i.e., be polite to the user) public void stop() { currentSound.stop(); } Fig. 16.2 60 61 62 63 64 65 66 67 68 69 70 71 MULTIMEDIA: IMAGES, ANIMATION, A UDIO AND VIDEO Loading and playing an AudioClip (part 2 of 3). private class ButtonHandler implements ActionListener { public void actionPerformed( ActionEvent e ) { if ( e.getSource() == playSound ) currentSound.play(); else if ( e.getSource() == loopSound ) currentSound.loop(); else if ( e.getSource() == stopSound ) currentSound.stop(); } } } Fig. 16.2 Loading and playing an AudioClip (part 3 of 3). © Copyright 2000 by Prentice Hall. All Rights Reserved. For use only by instructors in classes for which Java How to Program, Third Edition is the required textbook. chpt_16.fm Page 778 Wednesday, February 23, 2000 12:22 PM 778 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 MULTIMEDIA: IMAGES, ANIMATION, AUDIO AND VIDEO // Fig. 16.3: MediaPlayerDemo.java // Uses a Java Media Player to play media files. import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; import javax.media.*; public class MediaPlayerDemo extends JFrame { private Player player; private File file; public MediaPlayerDemo() { super( "Demonstrating the Java Media Player" ); JButton openFile = new JButton( "Open file to play" ); openFile.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { openFile(); createPlayer(); } } ); getContentPane().add( openFile, BorderLayout.NORTH ); setSize( 300, 300 ); show(); } private void openFile() { JFileChooser fileChooser = new JFileChooser(); fileChooser.setFileSelectionMode( JFileChooser.FILES_ONLY ); int result = fileChooser.showOpenDialog( this ); Fig. 16.3 41 42 43 44 45 46 47 CHAPTER 16 Demonstrating the Java Media Player (part 1 of 4). // user clicked Cancel button on dialog if ( result == JFileChooser.CANCEL_OPTION ) file = null; else file = fileChooser.getSelectedFile(); } © Copyright 2000 by Prentice Hall. All Rights Reserved. For use only by instructors in classes for which Java How to Program, Third Edition is the required textbook. chpt_16.fm Page 779 Wednesday, February 23, 2000 12:22 PM CHAPTER 16 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 Fig. 16.3 91 92 93 94 95 96 MULTIMEDIA: IMAGES, ANIMATION, A UDIO AND VIDEO 779 private void createPlayer() { if ( file == null ) return; removePreviousPlayer(); try { // create a new player and add listener player = Manager.createPlayer( file.toURL() ); player.addControllerListener( new EventHandler() ); player.start(); // start player } catch ( Exception e ){ JOptionPane.showMessageDialog( this, "Invalid file or location", "Error loading file", JOptionPane.ERROR_MESSAGE ); } } private void removePreviousPlayer() { if ( player == null ) return; player.close(); Component visual = player.getVisualComponent(); Component control = player.getControlPanelComponent(); Container c = getContentPane(); if ( visual != null ) c.remove( visual ); if ( control != null ) c.remove( control ); } public static void main(String args[]) { MediaPlayerDemo app = new MediaPlayerDemo(); Demonstrating the Java Media Player (part 2 of 4). app.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit(0); } © Copyright 2000 by Prentice Hall. All Rights Reserved. For use only by instructors in classes for which Java How to Program, Third Edition is the required textbook. chpt_16.fm Page 780 Wednesday, February 23, 2000 12:22 PM 780 MULTIMEDIA: IMAGES, ANIMATION, AUDIO AND VIDEO 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 } CHAPTER 16 } ); } // inner class to handler events from media player private class EventHandler implements ControllerListener { public void controllerUpdate( ControllerEvent e ) { if ( e instanceof RealizeCompleteEvent ) { Container c = getContentPane(); // load Visual and Control components if they exist Component visualComponent = player.getVisualComponent(); if ( visualComponent != null ) c.add( visualComponent, BorderLayout.CENTER ); Component controlsComponent = player.getControlPanelComponent(); if ( controlsComponent != null ) c.add( controlsComponent, BorderLayout.SOUTH ); c.doLayout(); } } } Initial GUI at execution Fig. 16.3 Demonstrating the Java Media Player (part 3 of 4). © Copyright 2000 by Prentice Hall. All Rights Reserved. For use only by instructors in classes for which Java How to Program, Third Edition is the required textbook. chpt_16.fm Page 781 Wednesday, February 23, 2000 12:22 PM CHAPTER 16 MULTIMEDIA: IMAGES, ANIMATION, A UDIO AND VIDEO 781 Selecting a media file to play from a JFileChooser dialog box The Sample3.mpg file loaded and playing Fig. 16.3 Demonstrating the Java Media Player (part 4 of 4). © Copyright 2000 by Prentice Hall. All Rights Reserved. For use only by instructors in classes for which Java How to Program, Third Edition is the required textbook. chpt_16.fm Page 782 Wednesday, February 23, 2000 12:22 PM 782 MULTIMEDIA: IMAGES, ANIMATION, AUDIO AND VIDEO Pause button: Displayed when the clip is playing. Play button: Displayed when the clip is playing. Fig. 16.4 Position indicator Mute Volume control CHAPTER 16 Information about the currently playing clip. Controls for the Java Media Player when an audio or video is playing. © Copyright 2000 by Prentice Hall. All Rights Reserved. For use only by instructors in classes for which Java How to Program, Third Edition is the required textbook. chpt_16.fm Page 783 Wednesday, February 23, 2000 12:22 PM CHAPTER 16 1 2 3 4 5 6 7 8 9 10 11 12 13 783 // Fig. 16.5: LogoAnimator.java // Animation a series of images import java.awt.*; import java.awt.event.*; import javax.swing.*; public class LogoAnimator extends JPanel implements ActionListener { protected ImageIcon images[]; protected int totalImages = 30, currentImage = 0, animationDelay = 50; // 50 millisecond delay protected Timer animationTimer; Fig. 16.5 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 MULTIMEDIA: IMAGES, ANIMATION, A UDIO AND VIDEO Animating a series of images (part 1 of 3). public LogoAnimator() { setSize( getPreferredSize() ); images = new ImageIcon[ totalImages ]; for ( int i = 0; i < images.length; ++i ) images[ i ] = new ImageIcon( "images/deitel" + i + ".gif" ); startAnimation(); } public void paintComponent( Graphics g ) { super.paintComponent( g ); if ( images[ currentImage ].getImageLoadStatus() == MediaTracker.COMPLETE ) { images[ currentImage ].paintIcon( this, g, 0, 0 ); currentImage = ( currentImage + 1 ) % totalImages; } } public void actionPerformed( ActionEvent e ) { repaint(); } public void startAnimation() { if ( animationTimer == null ) { currentImage = 0; animationTimer = new Timer( animationDelay, this ); animationTimer.start(); © Copyright 2000 by Prentice Hall. All Rights Reserved. For use only by instructors in classes for which Java How to Program, Third Edition is the required textbook. chpt_16.fm Page 784 Wednesday, February 23, 2000 12:22 PM 784 MULTIMEDIA: IMAGES, ANIMATION, AUDIO AND VIDEO 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 } else // continue from last image displayed if ( ! animationTimer.isRunning() ) animationTimer.restart(); } public void stopAnimation() { animationTimer.stop(); } public Dimension getMinimumSize() { return getPreferredSize(); } Fig. 16.5 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 CHAPTER 16 Animating a series of images (part 2 of 3). public Dimension getPreferredSize() { return new Dimension( 160, 80 ); } public static void main( String args[] ) { LogoAnimator anim = new LogoAnimator(); JFrame app = new JFrame( "Animator test" ); app.getContentPane().add( anim, BorderLayout.CENTER ); app.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); // The constants 10 and 30 are used below to size the // window 10 pixels wider than the animation and // 30 pixels taller than the animation. app.setSize( anim.getPreferredSize().width + 10, anim.getPreferredSize().height + 30 ); app.show(); } } © Copyright 2000 by Prentice Hall. All Rights Reserved. For use only by instructors in classes for which Java How to Program, Third Edition is the required textbook. chpt_16.fm Page 785 Wednesday, February 23, 2000 12:22 PM CHAPTER 16 Fig. 16.5 MULTIMEDIA: IMAGES, ANIMATION, A UDIO AND VIDEO 785 Animating a series of images (part 3 of 3). © Copyright 2000 by Prentice Hall. All Rights Reserved. For use only by instructors in classes for which Java How to Program, Third Edition is the required textbook. chpt_16.fm Page 786 Wednesday, February 23, 2000 12:22 PM 786 1 2 3 4 5 6 MULTIMEDIA: IMAGES, ANIMATION, AUDIO AND VIDEO // Fig. 16.6: LogoAnimator.java // Animating a series of images import java.awt.*; import java.awt.event.*; import javax.swing.*; Customizing an applet via the param HTML tag (part 1 of 5). Fig. 16.6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 CHAPTER 16 public class LogoAnimator extends JPanel implements ActionListener { protected ImageIcon images[]; protected int totalImages = 30, currentImage = 0, animationDelay = 50; // 50 millisecond delay protected String imageName = "deitel"; protected Timer animationTimer; public LogoAnimator() { initializeAnim(); } // new constructor to support customization public LogoAnimator( int num, int delay, String name ) { totalImages = num; animationDelay = delay; imageName = name; initializeAnim(); } private void initializeAnim() { images = new ImageIcon[ totalImages ]; for ( int i = 0; i < images.length; ++i ) images[ i ] = new ImageIcon( "images/" + imageName + i + ".gif" ); // moved here so getPreferredSize can check the size of // the first loaded image. setSize( getPreferredSize() ); startAnimation(); } public void paintComponent( Graphics g ) { super.paintComponent( g ); © Copyright 2000 by Prentice Hall. All Rights Reserved. For use only by instructors in classes for which Java How to Program, Third Edition is the required textbook. chpt_16.fm Page 787 Wednesday, February 23, 2000 12:22 PM CHAPTER 16 50 51 52 53 54 55 56 57 58 787 if ( images[ currentImage ].getImageLoadStatus() == MediaTracker.COMPLETE ) { images[ currentImage ].paintIcon( this, g, 0, 0 ); currentImage = ( currentImage + 1 ) % totalImages; } } public void actionPerformed( ActionEvent e ) { Customizing an applet via the param HTML tag (part 2 of 5). Fig. 16.6 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 MULTIMEDIA: IMAGES, ANIMATION, A UDIO AND VIDEO repaint(); } public void startAnimation() { if ( animationTimer == null ) { currentImage = 0; animationTimer = new Timer( animationDelay, this ); animationTimer.start(); } else // continue from last image displayed if ( ! animationTimer.isRunning() ) animationTimer.restart(); } public void stopAnimation() { animationTimer.stop(); } public Dimension getMinimumSize() { return getPreferredSize(); } public Dimension getPreferredSize() { return new Dimension( images[ 0 ].getIconWidth(), images[ 0 ].getIconHeight() ); } public static void main( String args[] ) { LogoAnimator anim = new LogoAnimator(); JFrame app = new JFrame( "Animator test" ); app.getContentPane().add( anim, BorderLayout.CENTER ); app.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { © Copyright 2000 by Prentice Hall. All Rights Reserved. For use only by instructors in classes for which Java How to Program, Third Edition is the required textbook. chpt_16.fm Page 788 Wednesday, February 23, 2000 12:22 PM 788 MULTIMEDIA: IMAGES, ANIMATION, AUDIO AND VIDEO 101 102 103 104 105 106 107 108 109 110 } Fig. 16.6 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 // // // // // // // // // // // // // // // CHAPTER 16 System.exit( 0 ); } } ); app.setSize( anim.getPreferredSize().width + 10, anim.getPreferredSize().height + 30 ); app.show(); } Customizing an applet via the param HTML tag (part 3 of 5). Fig. 16.6: LogoApplet.java Customizing an applet via HTML parameters HTML parameter "animationdelay" is an int indicating milliseconds to sleep between images (default 50). HTML parameter "imagename" is the base name of the images that will be displayed (i.e., "deitel" is the base name for images "deitel0.gif," "deitel1.gif," etc.). The applet assumes that images are in an "images" subdirectory of the directory in which the applet resides. HTML parameter "totalimages" is an integer representing the total number of images in the animation. The applet assumes images are numbered from 0 to totalimages - 1 (default 30). import java.awt.*; import javax.swing.*; public class LogoApplet extends JApplet{ public void init() { String parameter; parameter = getParameter( "animationdelay" ); int animationDelay = ( parameter == null ? 50 : Integer.parseInt( parameter ) ); String imageName = getParameter( "imagename" ); parameter = getParameter( "totalimages" ); int totalImages = ( parameter == null ? 0 : Integer.parseInt( parameter ) ); // Create an instance of LogoAnimator LogoAnimator animator; if ( imageName == null || totalImages == 0 ) animator = new LogoAnimator(); © Copyright 2000 by Prentice Hall. All Rights Reserved. For use only by instructors in classes for which Java How to Program, Third Edition is the required textbook. chpt_16.fm Page 789 Wednesday, February 23, 2000 12:22 PM CHAPTER 16 150 151 152 153 154 155 156 157 158 159 160 } MULTIMEDIA: IMAGES, ANIMATION, A UDIO AND VIDEO 789 else animator = new LogoAnimator( totalImages, animationDelay, imageName ); setSize( animator.getPreferredSize().width, animator.getPreferredSize().height ); getContentPane().add( animator, BorderLayout.CENTER ); animator.startAnimation(); } Fig. 16.6 Customizing an applet via the param HTML tag (part 4 of 5). Fig. 16.6 Customizing an applet via the param HTML tag (part 5 of 5). © Copyright 2000 by Prentice Hall. All Rights Reserved. For use only by instructors in classes for which Java How to Program, Third Edition is the required textbook. chpt_16.fm Page 790 Wednesday, February 23, 2000 12:22 PM 790 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 MULTIMEDIA: IMAGES, ANIMATION, AUDIO AND VIDEO // Fig. 16.7: ImageMap.java // Demonstrating an image map. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ImageMap extends JApplet { private ImageIcon mapImage; private int width, height; Fig. 16.7 42 43 44 45 46 47 48 49 50 CHAPTER 16 public void init() { addMouseListener( new MouseAdapter() { public void mouseExited( MouseEvent e ) { showStatus( "Pointer outside applet" ); } } ); addMouseMotionListener( new MouseMotionAdapter() { public void mouseMoved( MouseEvent e ) { showStatus( translateLocation( e.getX() ) ); } } ); mapImage = new ImageIcon( "icons2.gif" ); width = mapImage.getIconWidth(); height = mapImage.getIconHeight(); setSize( width, height ); } public void paint( Graphics g ) { mapImage.paintIcon( this, g, 0, 0 ); } Demonstrating an image map (part 1 of 2). public String translateLocation( int x ) { // determine width of each icon (there are 6) int iconWidth = width / 6; if ( x >= return else if ( return 0 && x <= iconWidth ) "Common Programming Error"; x > iconWidth && x <= iconWidth * 2 ) "Good Programming Practice"; © Copyright 2000 by Prentice Hall. All Rights Reserved. For use only by instructors in classes for which Java How to Program, Third Edition is the required textbook. chpt_16.fm Page 791 Wednesday, February 23, 2000 12:22 PM CHAPTER 16 51 52 53 54 55 56 57 58 59 60 61 62 MULTIMEDIA: IMAGES, ANIMATION, A UDIO AND VIDEO else if ( return else if ( return else if ( return else if ( return x > iconWidth * 2 && x <= iconWidth "Performance Tip"; x > iconWidth * 3 && x <= iconWidth "Portability Tip"; x > iconWidth * 4 && x <= iconWidth "Software Engineering Observation"; x > iconWidth * 5 && x <= iconWidth "Testing and Debugging Tip"; 791 * 3 ) * 4 ) * 5 ) * 6 ) return ""; } } Fig. 16.7 Demonstrating an image map (part 2 of 2). © Copyright 2000 by Prentice Hall. All Rights Reserved. For use only by instructors in classes for which Java How to Program, Third Edition is the required textbook. chpt_16.fm Page 792 Wednesday, February 23, 2000 12:22 PM 792 MULTIMEDIA: IMAGES, ANIMATION, AUDIO AND VIDEO CHAPTER 16 The Java Plug-in HTML Converter. This program allows you to convert all the HTML files in one directory or select a specific file. In this case, One File is selected. Fig. 16.8 The Java Plug-in HTML Converter (part 1 of 4). © Copyright 2000 by Prentice Hall. All Rights Reserved. For use only by instructors in classes for which Java How to Program, Third Edition is the required textbook. chpt_16.fm Page 793 Wednesday, February 23, 2000 12:22 PM CHAPTER 16 MULTIMEDIA: IMAGES, ANIMATION, A UDIO AND VIDEO 793 This dialog allows you to select the file that will be converted. Fig. 16.8 The Java Plug-in HTML Converter (part 2 of 4). © Copyright 2000 by Prentice Hall. All Rights Reserved. For use only by instructors in classes for which Java How to Program, Third Edition is the required textbook. chpt_16.fm Page 794 Wednesday, February 23, 2000 12:22 PM 794 MULTIMEDIA: IMAGES, ANIMATION, AUDIO AND VIDEO CHAPTER 16 The HTML Converter window after a file is selected for conversion. The Template File combo box allows you to choose the browsers in which the plug-in should be used. Fig. 16.8 The Java Plug-in HTML Converter (part 3 of 4). © Copyright 2000 by Prentice Hall. All Rights Reserved. For use only by instructors in classes for which Java How to Program, Third Edition is the required textbook. chpt_16.fm Page 795 Wednesday, February 23, 2000 12:22 PM CHAPTER 16 MULTIMEDIA: IMAGES, ANIMATION, A UDIO AND VIDEO 795 The confirmation dialog showing that one applet was found in the HTML file and converted. Fig. 16.8 The Java Plug-in HTML Converter (part 4 of 4). © Copyright 2000 by Prentice Hall. All Rights Reserved. For use only by instructors in classes for which Java How to Program, Third Edition is the required textbook.