(c) 2008 E.S.Boese All Rights Reserved. Threads and Media Chapter 8 - Lecture Slides 1.

Slides:



Advertisements
Similar presentations
Using Thread for Animation. Threads When we are doing multiple things at once we say we are multitasking Computers multitask when they run several programs.
Advertisements

Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
(c) 2006 E.S.Boese All Rights Reserved. Threads and Timers Chapter 19 - Student.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Event Handling Events and Listeners Timers and Animation.
18-Jun-15 Applets. 2 An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page.
JEditorPane Chapter 18 - Student. (c) 2005 by Elizabeth Sugar Boese JEditorPane creation Declare a JEditorPane component. JEditorPane pane; Check to see.
1 Recitation 11. Applet Applets. An applet is a Java program that is started by a browser (e.g. netscape or internet explorer) when an html file has a.
22-Jun-15 Threads and Multithreading. 2 Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three.
Audio Chapter 17 - Student. (c) 2005 by Elizabeth Sugar Boese Audio Files Java can play the following audio types: .au (Sun Audio) .wav (Windows Wave)
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 14 Applets, Images,
28-Jun-15 Applets. 2 An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page.
29-Jun-15 Threads and Multithreading. 2 Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three.
Lecture 17: Animation Yoni Fridman 7/27/01 7/27/01.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L05 (Chapter 16) Applets.
1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components.
Programming Task: Task 1 Controlled Assessment Practice.
CS4273: Distributed System Technologies and Programming I Lecture 3: Java Applets and Animations.
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.
Applets Java API.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
Chapter 15 Multithreading F Threads Concept  Creating Threads by Extending the Thread class  Creating Threads by Implementing the Runnable Interface.
Threads and Multimedia Animation, Images, Sound. Animation nAnimation, displaying a sequence of frames to create the illusion of motion, is a typical.
 2002 Prentice Hall, Inc. All rights reserved. Chapter 18 – Multimedia: Images, Animation, Audio and Video Outline 18.1 Introduction 18.2 Loading, Displaying.
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.
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.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
Pravin Yannawar, DOCS, NMU Jalgaon Basic Java : Applets 2 Objectives of This Session Identify the need for Applets Distinguish between Applets and Applications.
(c)2009 by E.S. Boese. All Rights Reserved. Classes Chapter 13 – Lecture Slides 1 Got class?
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
Layout Managers Arranges and lays out the GUI components on a container.
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.
Introduction Multimedia –Use of sound, image, graphics and video –Makes applications “come alive”
Game Art & Sound. Sound ● Midi files – Musical Instrument Digital Interface – Advantages ● small ● fast – Disadvantages ● synthesized ● rendered ● Wav.
CompSci Event Handling. CompSci Event Handling The Plan  Sequential (Single Thread) Model  Event Model  Making the GUI interactive  Examples.
Chapter 14 Applets and Advanced GUI  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets F.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Threads II IS Outline  Quiz  Thread review  Stopping a thread  java.util.Timer  Swing threads javax.swing.Timer  ProgressMonitor.
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.
1 Sound in Java Summary: r Sound API Basics r MIDI JavaSound - Part of the UIT User Interface Toolkit.
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.
Threads and Multithreading. Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three general approaches:
Lesson 28: More on the GUI button, frame and actions.
Java Applets Adding Animation. Import Files You still need to include the same files: –import java.applet.*; –import java.awt.*;
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 28.1 Java Speech API 28.2 Downloading and.
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
Chapter 13: Multithreading
Threads and Multithreading
Chapter 11 JTabbedPane, ToolTips, Borders, Audio, JFrame, MediaTracker
Ellen Walker Hiram College
Threads II IS
Threads and Multithreading
Threads and Multithreading
Threads and Multithreading
Threads and Multithreading
Threads and Multithreading
Chapter 15 Multithreading
Threads and Multithreading
Threads and Multithreading
Threads and Multithreading
Threads and Multithreading
Presentation transcript:

(c) 2008 E.S.Boese All Rights Reserved. Threads and Media Chapter 8 - Lecture Slides 1

(c) 2008 E.S.Boese All Rights Reserved. Threads Multi-processing: 2 or more things occurring at the same time When we run a program, a single thread automatically starts to run the program. To run two or more things simultaneously, we need to create a second (or more) thread(s). Ideal for slideshows, progress bars, animation, game programming  For example, if we want to perform a slideshow while the user is typing inside a, we need to use a separate thread for the slideshow. JTextArea  If we didn't use a second thread, the applet would hang while we ran through the code for the slideshow. 2

(c) 2008 E.S.Boese All Rights Reserved. Threads – steps to make it work There are four steps to get Thread to work: 1. The class needs to implement the Runnable interface. implements Runnable 2. Create a Thread Thread runner; runner = new Thread( this ); 3. Start the Thread runner.start( ); 4. Create a method named run public void run( ) { } When we use Threads we put the code inside the run method. 3

(c) 2008 E.S.Boese All Rights Reserved. run( ) method Code goes inside the run method. Usually we want the code to loop. We can set up the loop as an infinite loop. public void run( ) { while( true ) { // code } Use the method sleep in the Thread class to simulate a delay: (This is the proper way to delay, unlike the for loop we used earlier in the book) Thread.sleep( delayInMilliseconds ); OR thread.sleep( delayInMilliseconds );// thread variable To use the sleep method, we need to put it within a try... catch block to catch any exceptions (errors) that may may occur (usually put the try...catch around the while(true) loop): try { Thread.sleep( delayInMiliseconds ); } catch( Exception exc ) { } infinite loop – always true! 4

Applet methods Method HeaderDescription public void init( ) Initialize our applet; called only once public void start( ) Starts our applet running; start up necessary threads public void stop( ) Applet is no longer being displayed; stop our threads from running (c) 2008 E.S.Boese All Rights Reserved. 5

Examples Slideshow inside main applet class  see Slideshow.java Slideshow in a separate class  see Slides.java and SlidesApplet.java Animation is done by redrawing  see Wink.java and Smile.java  See AnimateThreads.java Animation Example 6

Game play - process (c) 2008 E.S.Boese All Rights Reserved. 7

Game play – offscreen images (c) 2008 E.S.Boese All Rights Reserved. 8

MediaTracker Use for loading images and sound files before the applet starts Ensures everything is loaded properly before the applet runs  Prevents empty screens or missing graphics/media 9

MediaTracker import java.awt.*; import javax.swing.*; public class MediaTrackerEx extends JApplet { Image one, two, three, four; MediaTracker imgTracker; public void init() { loadImages(); } public void start() { try { imgTracker.waitForAll(); }catch (Exception e) { } } public void loadImages() { one = getImage(getCodeBase(), "one.gif"); two = getImage(getCodeBase(), "two.gif"); three = getImage(getCodeBase(), "three.gif"); four = getImage(getCodeBase(), "four.gif"); imgTracker.addImage(one, 1); imgTracker.addImage(two, 2); imgTracker.addImage(three, 3); imgTracker.addImage(four, 4); } (c) 2008 E.S.Boese All Rights Reserved. 10

(c) 2008 E.S.Boese All Rights Reserved. Audio 11

Audio Files Java can play the following audio types: .au (Sun Audio) .wav (Windows Wave) .aif or.aiff (Macintosh AIFF) .mid (Musical Instrument Digital Interface (MIDI) .rmf Note: mp3 is NOT supported in the Java API. However, There are libraries you can download and use Note: wav files from your CDs are HUGE! Won’t run well – only use small WAV files

Audio AudioClip clip; clip = getAudioClip( getCodeBase( ), audioFilename ); clip.play( ); There are three methods we can call on our AudioClip  play play the sound file once through  loop play the sound file continually  stop stop playing the file getCodeBase( ) works same for images as audio files

Audio import java.awt.*; import java.applet.*; import javax.swing.*; public class AudioPlay extends JApplet { String audioFilename = "mySounds.mid"; AudioClip ac; public void init( ) { ac = getAudioClip( getCodeBase( ), audioFilename ); ac.play( ); }

Audio To have sound clip loop: get an AudioClip call the loop( ) method import java.awt.*; import java.applet.*; import javax.swing.*; public class AudioPlay extends JApplet { String audioFilename = "mySounds.mid"; AudioClip ac; public void init( ) { ac = getAudioClip( getCodeBase( ), audioFilename ); ac.loop( ); }

Audio: Need for stop( ) See Example Each button starts an audio file playing If the previous one isn’t stopped first, then the new one plays on TOP of the other one This is usually not desired However, some applications want this technique (see example of Singing Horses on Internet at:

stop method import java.awt.*; import java.applet.*; import java.awt.event.*; import javax.swing.*; public class LoopAudio extends JApplet { String audioFilename = "happyDaze.wav"; AudioClip ac; public void init( ) { ac = getAudioClip( getCodeBase( ), audioFilename ); ac.loop( ); // loop instead of play } public void stop( ) { // can't stop it if it isn't running, check first if( ac != null ) ac.stop( ); } Stop playing audio when user leaves the applet: Write a stop method

(c) 2008 E.S.Boese All Rights Reserved. Timers 18

(c) 2008 E.S.Boese All Rights Reserved. Timers Timers are useful for repeating steps at particular intervals.  Examples: progress bars, custom clocks and timed animation, tooltips, blinking cursor Timers are based on event processing,  Important that the code to be run from a Timer event can be executed quickly to enable the system to handle the next event. (Threads do not have this limitation and therefore are ideal for more time-intensive code processing). Create a Timer object by specifying the delay count in milliseconds and the listener for the ActionEvent : Timer timer; timer = new Timer( delay, this ); 19

(c) 2008 E.S.Boese All Rights Reserved. Timers Similar to threads, we then need to start the timer: timer.start( ); After the delay in milliseconds, the actionPerformed method is called. Therefore, just like when we listen for events on buttons, we need to implement the ActionListener. public void actionPerformed( ActionEvent ae ) { Object src = ae.getSource( ); if ( src instanceof Timer ) // do something since Timer expired else if ( src instanceof JButton ) // do something based on a button click } 20

(c) 2008 E.S.Boese All Rights Reserved. Timers There are five steps to get Timer to work: 1. Import the package to handle ActionEvent events: import java.awt.event.*; 2. Specify that we're listening for events: public class xyz extends JApplet implements ActionListener 3. Create a Timer object Timer timer; timer = new Timer( delay, this ); 4. Start the timer: timer.start( ); 5. Add an actionPerformed method: public void actionPerformed( ActionEvent ae ) { Object src = actionEvent.getSource( ); if ( src instanceof Timer )... else if ( src instanceof JButton )... } 6. (Optional) Stop the Timer timer.stop( ); 21

TimedClock Example import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TimedClock extends JApplet implements ActionListener { JLabel countdown; int count = 10; Timer timer; int DELAY = 800; // delay in miliseconds public void init( ) { setLayout( new FlowLayout( ) ); countdown = new JLabel( String.valueOf( count ) ); countdown.setFont( new Font( "Serif", Font.BOLD, 46 ) ); add( countdown ); timer = new Timer( DELAY, this ); timer.start( ); } public void actionPerformed( ActionEvent ae ) { Object src = ae.getSource( ); if ( src == timer && count > 0 ) { count = count - 1; countdown.setText( String.valueOf( count ) ); } if ( count == 0 ) countdown.setText( "Blast-off!" ); } (c) 2008 E.S.Boese All Rights Reserved. 22

(c) 2008 E.S.Boese All Rights Reserved. Summary Threads MediaTracker Audio Timers 23