 2000 Prentice Hall, Inc. All rights reserved. Java Multimedia: Images, Animation, Audio and Video Outline 30.1 Introduction 30.2Loading, Displaying and.

Slides:



Advertisements
Similar presentations
Computer Science 209 Images and GUIs. Working with Java Colors The class java.awt.Color includes constants, such as Color.red, for some commonly used.
Advertisements

1 More on Applets Overview l Changing Colors l Changing Fonts & Styles l Applet Life-Cycle l Input using Dialog Window l Input using HTML parameters l.
Graphics You draw on a Graphics object The Graphics object cannot directly be created by your code, instead one is generated when the method paintComponent.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
More Java Drawing in 2D Animations with Timer. Drawing Review A simple two-dimensional coordinate system exists for each graphics context or drawing surface.
Computer Science 209 Applets. Applications and Applets A Java application runs on a stand-alone computer and may connect to other computers via sockets.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
1 L45 Multimedia: Applets and Applications. 2 OBJECTIVES  How to get and display images.  To create animations from sequences of images.  To create.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter Java Multimedia: Images, Animation, Audio.
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.
COMP 14 Introduction to Programming Miguel A. Otaduy June 8, 2004.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 19 – Multimedia: Images, Animation and Audio Outline 19.1 Introduction 19.2 Loading, Displaying.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 21, 2005.
 2000 Prentice Hall, Inc. All rights reserved. Java Multimedia: Images, Animation, Audio and Video Outline 30.1 Introduction 30.2Loading, Displaying and.
Unit 071 Review of Applets Learning Outcomes oDistinguish between Java Applications and Java Applets. oWrite applet programs that can load images and play.
 Pearson Education, Inc. All rights reserved Multimedia: Applets and Applications.
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.
GUI Tutorial Images. Useful Info – not on final exam.
Lesson 35: Review of the Java GUI. The JFrame, Container and JButton.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 16 - Multimedia: Images, Animation, Audio and Video Outline 16.1 Introduction 16.2Downloading the.
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.
Introduction Multimedia –Use of sound, image, graphics and video –Makes applications “come alive”
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 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.
 2002 Prentice Hall, Inc. All rights reserved. Chapter 18 – Multimedia: Images, Animation, Audio and Video Outline 18.1 Introduction 18.2 Loading, Displaying.
(c) 2008 E.S.Boese All Rights Reserved. Threads and Media Chapter 8 - Lecture Slides 1.
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.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Screen Saver Application.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
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.
GUI programming Graphical user interface-based programming.
Timer Objects  regularly generates action events at programmer-specified time intervals  Timer class is in javax.swing package  Timer (int delay, ActionListener.
Canvas and Graphics CS 21a. 9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas.
Lesson 34: Layering Images with Java GUI. The FlowLayout RECAP.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 16 Applets and Multimedia.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Review of Graphics in Java,
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.
Lesson 39: More wrapup on GUIs. 1.This presentation will focus on the decisions around software architecture and the decisions that will drive the code.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
Java Programming Working with Sound and Images. Topics Learn about the paint() and repaint() methods Learn about paintComponent() method Use the drawString()
Introduction Multimedia –Use of sound, image, graphics and video –Makes applications “come alive”
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Animation To animate our programs, that is, to have real time interactions where objects move on the screen, we want to call repaint( ) every few milliseconds.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Field Trip #23 Hangman By Keith Lynn. JApplet A JApplet is a top-level container An applet is a small Java program that is executed by another program.
1 GUI programming Graphical user interface-based programming Chapter G1 (pages )
Chapter 16 Multimedia F Audio Files F Playing Audio F Running Audio on a Separate Thread F Displaying Images F Displaying a Sequence of Images  Using.
Swing. Introduction to Swing What is Swing? “ Swing is a diverse collection of lightweight components that can be used to build sophisticated user interfaces.”
Applets Java code is compiled into byte code instead of machine language –Languages like C, C++, Pascal and others are compiled into machine language so.
© 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
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 27.1 Test-Driving the Drawing Shapes Application.
Introduction to Applets Chapter 21. Applets An applet is a Java application that is intended to be invoked and executed through a Web browser. Click Here.
Lesson 28: More on the GUI button, frame and actions.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 28.1 Java Speech API 28.2 Downloading and.
Applets. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 2 Applets Usually.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Will not cover Section 3.7 Thinking About Objects: Identifying.
Chapter 14: Multimedia Audio Files Audio Files Playing Audio Playing Audio Running Audio on a Separate Thread Running Audio on a Separate Thread Displaying.
Mouse, Keyboard, Sounds, and Images JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin,
Chapter 6 Building Java GUIs. MVC Model View Controller The model passes its data to the view for rendering The view determines which events are passed.
Chapter 14 Applets and More. 2 Contents I. Introduction to Applets II. A Brief Introduction to HTML III. Creating Applets with Swing IV. Using AWT for.
Lecture 09 Applets.
Multimedia in Java Multimedia combines graphics, animation and sound
Graphical user interface-based programming
UNIT-5.
Presentation transcript:

 2000 Prentice Hall, Inc. All rights reserved. Java Multimedia: Images, Animation, Audio and Video Outline 30.1 Introduction 30.2Loading, Displaying and Scaling Images 30.3Loading and Playing Audio Clips 30.4Animating a Series of Images

 2000 Prentice Hall, Inc. All rights reserved Introduction Revolution in computer industry –Before, computers used for high-speed calculations –Now, data manipulation important Multimedia –"sizzle" of Java - images, sound, video –CDs, DVDs, video cards –Demands extraordinary computing power Fast processors making multimedia possible Java –Has built-in multimedia capabilities Most programming languages do not –Develop powerful multimedia applications

 2000 Prentice Hall, Inc. All rights reserved. 30.2Loading, Displaying and Scaling Images Java Multimedia –Graphics, images, animations, sounds, and video Begin with images Class Image ( java.awt ) –Abstract class, cannot create an object directly Must request that an Image be loaded and returned to you –Class Applet (superclass of JApplet ) has this method getImage( imageLocation, filename ); imageLocation - getDocumentBase() - URL (address) of HTML file filename - Java supports.gif and.jpg (.jpeg )

 2000 Prentice Hall, Inc. All rights reserved. 30.2Loading, Displaying and Scaling Images (II) Displaying Image s with drawImage –Many overloaded versions g.drawImage( myImage, x, y, ImageObserver ); myImage - Image object x,y - coordinates to display image ImageObserver - object on which image is displayed –Use " this" to indicate the applet –Can be any object that implements ImageObserver interface –g.drawImage( myImage, x, y, width, height, ImageObserver ); width and height - dimensions of image (automatically scaled) –getWidth(), getHeight() - return dimensions of applet

 2000 Prentice Hall, Inc. All rights reserved. 30.2Loading, Displaying and Scaling Images (III) Class ImageIcon –Not an abstract class (can create objects) –Example constructor private ImageIcon myIcon; myIcon = new ImageIcon( "myIcon.gif" ); Displaying Icons with method paintIcon myIcon.paintIcon( Component, Graphics, x, y ) –Component - Component object on which to display image ( this ) –Graphics - Graphics object used to render image ( g ) –x, y - coordinates of Icon

 2000 Prentice Hall, Inc. All rights reserved. 30.2Loading, Displaying and Scaling Images (IV) Usage –ImageIcon s are simpler than Image s Create objects directly No need for ImageObserver reference –However, cannot scale ImageIcon s Scaling –Use ImageIcon method getImage Returns Image reference This can be used with drawImage and be scaled

 2000 Prentice Hall, Inc. All rights reserved. Outline Image Example 1. import 1.1 Declare objects 2. init() 2.1 Initialize objects 3. paint() 3.1 drawImage calls 1// Fig. 30.1: LoadImageAndScale.java 2// Load an image and display it in its original size 3// and scale it to twice its original width and height. 4// Load and display the same image as an ImageIcon. 5import java.applet.Applet; 6import java.awt.*; 7import javax.swing.*; 8 9public class LoadImageAndScale extends JApplet { 10 private Image logo1; 11 private ImageIcon logo2; // load the image when the applet is loaded 14 public void init() 15 { 16 logo1 = getImage( getDocumentBase(), "logo.gif" ); 17 logo2 = new ImageIcon( "logo.gif" ); 18 } // display the image 21 public void paint( Graphics g ) 22 { 23 // draw the original image 24 g.drawImage( logo1, 0, 0, this ); // draw the image scaled to fit the width of the applet 27 // and the height of the applet minus 120 pixels 28 g.drawImage( logo1, 0, 120, 29 getWidth(), getHeight() - 120, this ); // draw the icon using its paintIcon method

 2000 Prentice Hall, Inc. All rights reserved. Outline 3.2 paintIcon call Program Output 32 logo2.paintIcon( this, g, 180, 0 ); 33 } 34}

 2000 Prentice Hall, Inc. All rights reserved. 30.3Loading and Playing Audio Clips Audio clips –Require speakers and a sound board –Sound engine - plays audio clips Supports.au,.wav,. aif,.mid Java Media Framework supports additional formats Playing audio clips –play method in Applet –Plays clip once, marked for garbage collection when finished play( location, soundFileName ); location - getDocumentBase (URL of HTML file) play( soundURL ); soundURL - URL that contains location and filename of clip

 2000 Prentice Hall, Inc. All rights reserved. 30.3Loading and Playing Audio Clips (II) Playing audio clips –Method play from AudioClip interface –More flexible than Applet method play Audio stored in program, can be reused –getAudioClip Returns reference to an AudioClip Same format as Applet method play –getAudioClip( location, filename ) –getAudioClip( soundURL ) –Once AudioClip loaded, use methods play - plays audio once loop - continuous loops audio in background stop - terminates clip that is currently playing

 2000 Prentice Hall, Inc. All rights reserved. Outline 1. import 1.1 Declare objects 2. init 2.1 Set layout 1// Fig. 30.2: LoadAudioAndPlay.java 2// Load an audio clip and play it. 3import java.applet.*; 4import java.awt.*; 5import java.awt.event.*; 6import javax.swing.*; 7 8public class LoadAudioAndPlay extends JApplet { 9 private AudioClip sound1, sound2, currentSound; 10 private JButton playSound, loopSound, stopSound; 11 private JComboBox chooseSound; // load the image when the applet begins executing 14 public void init() 15 { 16 Container c = getContentPane(); 17 c.setLayout( new FlowLayout() ); String choices[] = { "Welcome", "Hi" }; 20 chooseSound = new JComboBox( choices ); 21 chooseSound.addItemListener( 22 new ItemListener() { 23 public void itemStateChanged( ItemEvent e ) 24 { 25 currentSound.stop(); currentSound = 28 chooseSound.getSelectedIndex() == 0 ? 29 sound1 : sound2; 30 } 31 } 32 );

 2000 Prentice Hall, Inc. All rights reserved. Outline 2.2 Add buttons 2.3 Initialize audio clips 3. stop 4. Class ButtonHandler 33 c.add( chooseSound ); ButtonHandler handler = new ButtonHandler(); 36 playSound = new JButton( "Play" ); 37 playSound.addActionListener( handler ); 38 c.add( playSound ); 39 loopSound = new JButton( "Loop" ); 40 loopSound.addActionListener( handler ); 41 c.add( loopSound ); 42 stopSound = new JButton( "Stop" ); 43 stopSound.addActionListener( handler ); 44 c.add( stopSound ); sound1 = getAudioClip( 47 getDocumentBase(), "welcome.wav" ); 48 sound2 = getAudioClip( 49 getDocumentBase(), "hi.au" ); 50 currentSound = sound1; 51 } // stop the sound when the user switches Web pages 54 // (i.e., be polite to the user) 55 public void stop() 56 { 57 currentSound.stop(); 58 } private class ButtonHandler implements ActionListener { 61 public void actionPerformed( ActionEvent e ) 62 { 63 if ( e.getSource() == playSound ) 64 currentSound.play();

 2000 Prentice Hall, Inc. All rights reserved. Outline Program Output 65 else if ( e.getSource() == loopSound ) 66 currentSound.loop(); 67 else if ( e.getSource() == stopSound ) 68 currentSound.stop(); 69 } 70 } 71}

 2000 Prentice Hall, Inc. All rights reserved. 30.4Animating a Series of Images Following example –Use a series of images stored in an array –Use same techniques to load and display ImageIcon s Class Timer –Generates ActionEvents at a fixed interval in milliseconds Timer ( animationDelay, ActionListener ); ActionListener - ActionListener that will respond to ActionEvents –Methods start stop restart isRunning

 2000 Prentice Hall, Inc. All rights reserved. 30.4Animating a Series of Images (II) Method repaint –Calls update, which calls paintComponent Subclasses of JComponent should draw in method paintComponent Call superclass's paintComponent to make sure Swing components displayed properly View area –Width and height specify entire window, not client area –Dimension objects Contain width and height values myDimObject = new Dimension( 100, 200 ); myDimObject.width

 2000 Prentice Hall, Inc. All rights reserved. 30.4Animating a Series of Images (III) getImageLoadStatus –ImageIcon method Determines if image is completely loaded into memory Only complete images should be displayed (smooth animation) –If loaded, returns MediaTracker.COMPLETE –MediaTracker Can determine when images are loaded, or force program to wait if not ImageIcon creates our MediaTracker for us

 2000 Prentice Hall, Inc. All rights reserved. Outline 1. import 1.1 implements ActionListener 1.2 Declare objects 1.3 Constructor 1.4 Initialize ImageIcon array 2. paintComponent 2.1 Call to superclass paintComponent 1// Fig. 30.3: LogoAnimator.java 2// Animation a series of images 3import java.awt.*; 4import java.awt.event.*; 5import javax.swing.*; 6 7public class LogoAnimator extends JPanel 8 implements ActionListener { 9 protected ImageIcon images[]; 10 protected int totalImages = 30, 11 currentImage = 0, 12 animationDelay = 50; // 50 millisecond delay 13 protected Timer animationTimer; public LogoAnimator() 16 { 17 setSize( getPreferredSize() ); images = new ImageIcon[ totalImages ]; for ( int i = 0; i < images.length; ++i ) 22 images[ i ] = 23 new ImageIcon( "images/deitel" + i + ".gif" ); startAnimation(); 26 } public void paintComponent( Graphics g ) 29 { 30 super.paintComponent( g ); 31

 2000 Prentice Hall, Inc. All rights reserved. Outline 2.2 If image loaded, display it ( paintIcon ) 2.3 Increment currentImage 3. actionPerformed 3.1 startAnimation 3.2 stopAnimation 3.3 getMinimumSize 32 if ( images[ currentImage ].getImageLoadStatus() == 33 MediaTracker.COMPLETE ) { 34 images[ currentImage ].paintIcon( this, g, 0, 0 ); 35 currentImage = ( currentImage + 1 ) % totalImages; 36 } 37 } public void actionPerformed( ActionEvent e ) 40 { 41 repaint(); 42 } public void startAnimation() 45 { 46 if ( animationTimer == null ) { 47 currentImage = 0; 48 animationTimer = new Timer( animationDelay, this ); 49 animationTimer.start(); 50 } 51 else // continue from last image displayed 52 if ( ! animationTimer.isRunning() ) 53 animationTimer.restart(); 54 } public void stopAnimation() 57 { 58 animationTimer.stop(); 59 } public Dimension getMinimumSize() 62 { 63 return getPreferredSize(); 64 }

 2000 Prentice Hall, Inc. All rights reserved. Outline 3.4. getPreferredSize 4. main public Dimension getPreferredSize() 67 { 68 return new Dimension( 160, 80 ); 69 } public static void main( String args[] ) 72 { 73 LogoAnimator anim = new LogoAnimator(); JFrame app = new JFrame( "Animator test" ); 76 app.getContentPane().add( anim, fBorderLayout.CENTER ); app.addWindowListener( 79 new WindowAdapter() { 80 public void windowClosing( WindowEvent e ) 81 { 82 System.exit( 0 ); 83 } 84 } 85 ); // The constants 10 and 30 are used below to size the 88 // window 10 pixels wider than the animation and 89 // 30 pixels taller than the animation. 90 app.setSize( anim.getPreferredSize().width + 10, 91 anim.getPreferredSize().height + 30 ); 92 app.show(); 93 } 94}

 2000 Prentice Hall, Inc. All rights reserved. Outline Program Output