© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 28.1 Java Speech API 28.2 Downloading and.

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 17 Creating User Interfaces.
Advertisements

Basic Java – Interface design. Understand: How to use TextPad for Java How to define classes and objects How to create a GUI interface How event-driven.
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
Multithreading : animation. slide 5.2 Animation Animation shows different objects moving or changing as time progresses. Thread programming is useful.
1 Chapter 8 Objects and Classes Lecture 2 Prepared by Muhanad Alkhalisy.
Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
 2002 Prentice Hall, Inc. All rights reserved. Chapter 14 – Exception Handling Outline 14.1 Introduction 14.2 When Exception Handling Should Be Used 14.3.
Deitel Ch 11-part 1 Java GUIs 1 Java GUIs (Deitel, Chap 14-part1) Focus: Attaching GUI components + event handling  input dialog => only one value for.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 15.1 Test-Driving the Craps Game Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 14.1 Test-Driving the Fund Raiser Application.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 14 - Exception Handling Outline 14.1Introduction 14.2When Exception Handling Should Be Used 14.3Other.
Carnegie Mellon University, MISM1 Java GUI programming and Java Threads GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann Thread.
Lecture 19 Graphics User Interfaces (GUIs)
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
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.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 6.1 Test-Driving the Wage Calculator Application.
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 16.1 Test-Driving the Flag Quiz Application.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Screen Saver Application.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Interfaces & Polymorphism part 2:
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 27 - Phone Book Application Introducing Multimedia.
GUI programming Graphical user interface-based programming.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 18.1 Test-Driving the Microwave Oven Application.
GUI Clients 1 Enterprise Applications CE00465-M Clients with Graphical User Interfaces.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 19.1 Test-Driving the Shipping Hub Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 23.1 Test-Driving the Screen Scraping Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 25 – Ticket Information Application Introducing.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 22.1 Test-Driving the Typing Skills Developer.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 12 – Enhancing the Wage Calculator Application.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 8.1 Test-Driving the Car Payment Calculator.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 11.1 Test-Driving the Security Panel Application.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 10.1 Test-Driving the Interest Calculator.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Enhanced Car Payment.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Chapter 11 - A GUI Interacting With a Problem Domain Class1 Chapter 11 A GUI Interacting With a Problem Domain Class 11.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 13.1 Test-Driving the Enhanced Interest Calculator.
Java the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 2 – Welcome Application Introduction to Graphical.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 27.1 Test-Driving the Drawing Shapes Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 4 – Completing the Inventory Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 5.1 Test-Driving the Enhanced Inventory Application.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 17.1 Test-Driving the Student Grades Application.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Lesson 28: More on the GUI button, frame and actions.
Graphical User Interface (GUI)
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
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.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 7.1 Test-Driving the Dental Payment Application.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
A First Look at GUI Applications
Java Programming: From Problem Analysis to Program Design,
CiS 260: App Dev I Chapter 6: GUI and OOD.
Presentation transcript:

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 28.1 Java Speech API 28.2 Downloading and Installing FreeTTS 28.3 Test-Driving the Phone Book Application 28.4 Constructing the Phone Book Application 28.5 Wrap-Up Tutorial 28 – Phone Book Application Introducing the Java Speech API

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Objectives In this tutorial, you will learn to: –Download and install FreeTTS to run the Java Speech application. –Enhance Java applications using multimedia. –Use the Java Speech API in a Java application.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Java Speech API Multimedia –Graphics, animation, video and sound Java Speech API –Adds speech capabilities to applications –Supports two core speech technologies Speech synthesis Speech recognition

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Java Speech API (Cont.)

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Java Speech API (Cont.) Speech synthesis or text-to-speech technology –Produces synthetic speech from text Speech recognition –Produces text from audio input that contains speech Speech recognition engine Text-to-speech engine Open source –Software freely available for use and modification

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Downloading and Installing FreeTTS Figure 28.1 Locations for downloading FreeTTS. Click the symbol to download

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Downloading and Installing FreeTTS (Cont.) Figure 28.2 Decompressing freetts-1_1_2.tar.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Downloading and Installing FreeTTS (Cont.) Figure 28.3 Extracting the content in freetts-1_1_2.tar. Click the Extract button to extract the content in freetts-1_1_1.tar

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Downloading and Installing FreeTTS (Cont.) Figure 28.4 Specifying the extract destination. Extract the content in freetts-1_1_1.tar to the C:\ directory

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Downloading and Installing FreeTTS (Cont.) Figure 28.5 Accepting the license agreement.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Downloading and Installing FreeTTS (Cont.) Figure 28.6 Completing the Java Speech API implementation installation.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Phone Book Application Figure 28.7 Phone Book application.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Phone Book Application When the Get Phone Number JButton is clicked Get the selected name Speak the phone number of the selected person

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Phone Book Application (Cont.)

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Phone Book Application (Cont.) Figure 28.9 Importing the Java Speech API packages. Importing the Java Speech API packages

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Phone Book Application (Cont.) Figure Declaring an instance variable for the speech synthesizer. Declaring an instance variable for the speech synthesizer

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Phone Book Application (Cont.) SynthesizerModeDesc –A descriptor –Specifies the properties of the speech synthesizer Name Mode of operation Language supported Running state Voice Locale –Represents a specific region –Locale.US represents a Locale object for the United States

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Phone Book Application (Cont.) Central –Provides access to all speech input and output capabilities Synthesizer –Provides speech synthesis capabilities –Use the createSynthesizer method of the Central class to create

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Phone Book Application (Cont.) Figure Initializing the speech synthesizer. Create SynthesizerModeDesc for FreeTTS synthesizer Create a synthesizer

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Phone Book Application (Cont.) allocate method –Allocate resources required by the speech engine resume method –Readies the speech synthesizer to speak

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Phone Book Application (Cont.) Figure Getting the Synthesizer object ready to speak. Get speech synthesizer ready to speak

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Phone Book Application (Cont.) SynthesizerProperties object –Contains various properties of a speech synthesizer –Use the getSynthesizerProperties method to get this object speakingRate property –Specifies the speaking rate in words per minute

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Phone Book Application (Cont.) Figure Setting the properties of the Synthesizer object. Get synthesizer property Set up speaking rate of the speech synthesizer

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Phone Book Application (Cont.) Figure Coding the event handler for getPhoneNumberJButton. speakPlainText method –Used to speak text Get index of the selected person Declare text to speak Speak the person ’ s name and phone number

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Phone Book Application (Cont.) Figure Cleaning up the synthesizer. deallocate method –Free resources that are allocated to the speech engine Deallocate speech synthesizer

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Phone Book Application (Cont.) Figure Running the completed application.

 2004 Prentice Hall, Inc. All rights reserved. Outline 27 PhoneBook.java (1 of 8) 1 // Tutorial 28: PhoneBook.java 2 // An application announces phone number with FreeTTS. 3 import java.awt.*; 4 import java.awt.event.*; 5 import java.util.*; 6 import javax.swing.*; 7 import javax.speech.*; 8 import javax.speech.synthesis.*; 9 10 public class PhoneBook extends JFrame 11 { 12 // JLabels to display instructions 13 private JLabel instruction1JLabel; 14 private JLabel instruction2JLabel; // JComboBox for names 17 private JComboBox nameJComboBox; // JButton to get phone number 20 private JButton getPhoneNumberJButton; // Synthesizer to speak phone number 23 private Synthesizer speechSynthesizer; 24 Import Java Speech API packages Declare the instance variable speechSynthesizer

 2004 Prentice Hall, Inc. All rights reserved. Outline 28 PhoneBook.java (2 of 8) 25 // fill array with people's names 26 private String[] namesArray = { "John", "Jennifer", "Howard" }; // fill array with people's phone numbers 29 private String[] numbersArray = 30 { "(555) ", "(555) ", "(555) " }; // no-argument constructor 33 public PhoneBook() 34 { 35 // initialize Synthesizer 36 try 37 { 38 // create SynthesizerModeDesc for FreeTTS synthesizer 39 SynthesizerModeDesc descriptor = new SynthesizerModeDesc( 40 "Unlimited domain FreeTTS Speech Synthesizer " + 41 "from Sun Labs", null, Locale.US, Boolean.FALSE, null); // create a Synthesizer 44 speechSynthesizer = Central.createSynthesizer( descriptor ); 45 Create a SynthesizerMode- Desc for FreeTTS synthesizer Create a synthesizer object

 2004 Prentice Hall, Inc. All rights reserved. Outline 29 PhoneBook.java (3 of 8) 46 // Synthesizer created successfully 47 if ( speechSynthesizer != null ) 48 { 49 // prepare synthesizer to speak 50 speechSynthesizer.allocate(); 51 speechSynthesizer.resume(); // get synthesizer properties 54 SynthesizerProperties properties = 55 speechSynthesizer.getSynthesizerProperties(); // set up speaking rate 58 properties.setSpeakingRate( 100.0f ); } // end if else 63 { 64 System.err.println( "Synthesizer creation failed." ); 65 System.exit( 1 ); 66 } } // end try 69 Prepare the synthesizer to speak Get synthesizer properties Set up speaking rate of speech synthesizer If synthesizer is not created successfully, print error message and terminate the application

 2004 Prentice Hall, Inc. All rights reserved. Outline 30 PhoneBook.java (4 of 8) 70 catch ( Exception myException ) 71 { 72 myException.printStackTrace(); 73 } 74 // set up GUI 75 createUserInterface(); } // end constructor // create and position GUI components; register event handler 80 private void createUserInterface() 81 { 82 // get content pane for attaching GUI components 83 Container contentPane = getContentPane(); // enable explicit positioning of GUI components 86 contentPane.setLayout( null ); // set up instruction1JLabel 89 instruction1JLabel = new JLabel(); 90 instruction1JLabel.setBounds( 16, 8, 264, 23 ); 91 instruction1JLabel.setText( 92 "Select a name from the combo box." ); 93 contentPane.add( instruction1JLabel ); 94 Catch exceptions

 2004 Prentice Hall, Inc. All rights reserved. Outline 31 PhoneBook.java (5 of 8) 95 // set up instruction2JLabel 96 instruction2JLabel = new JLabel(); 97 instruction2JLabel.setBounds( 16, 35, 264, 23 ); 98 instruction2JLabel.setText( 99 "Click the button to listen to the phone number." ); 100 contentPane.add( instruction2JLabel ); // set up nameJComboBox 103 nameJComboBox = new JComboBox( namesArray ); 104 nameJComboBox.setBounds( 50, 65, 150, 23 ); 105 contentPane.add( nameJComboBox ); // set up getPhoneNumberJButton 108 getPhoneNumberJButton = new JButton( "Get Phone Number" ); 109 getPhoneNumberJButton.setBounds( 50, 100, 150, 23 ); 110 getPhoneNumberJButton.setText( "Get Phone Number" ); 111 contentPane.add( getPhoneNumberJButton ); 112 getPhoneNumberJButton.addActionListener( new ActionListener() // anonymous inner class 115 { 116 // event handler called when getPhoneNumberJButton 117 // is clicked 118 public void actionPerformed( ActionEvent event ) 119 {

 2004 Prentice Hall, Inc. All rights reserved. Outline 32 PhoneBook.java (6 of 8) 120 getPhoneNumberJButtonActionPerformed( event ); 121 } } // end anonymous inner class ); // end call to addActionListener // set properties of application's window 128 setTitle( "Phone Book" ); // set title bar string 129 setSize( 300, 160 ); // set window size 130 setVisible( true ); // display window // ensure synthesizer is cleaned up 133 // when user closes application 134 addWindowListener( new WindowAdapter() // anonymous inner class 137 { 138 public void windowClosing( WindowEvent event ) 139 { 140 frameWindowClosing( event ); 141 } } // end anonymous inner class 144

 2004 Prentice Hall, Inc. All rights reserved. Outline 33 PhoneBook.java (7 of 8) 145 ); // end addWindowListener } // end method createUserInterface // speak a person's phone number 150 private void getPhoneNumberJButtonActionPerformed( 151 ActionEvent event ) 152 { 153 // get index of the selected person 154 int selectedName = nameJComboBox.getSelectedIndex(); // declare text to speak 157 String phoneNumberString = namesArray[ selectedName ] "'s phone number is " + numbersArray[ selectedName ]; // speak the person's name and phone number 161 speechSynthesizer.speakPlainText( phoneNumberString, null ); } // end method getPhoneNumberJButtonActionPerformed // cleanup synthesizer 166 private void frameWindowClosing( WindowEvent event ) 167 { 168 // deallocate synthesizer 169 try Get index of the selected person Speak the person’s name and phone number Declare text to speak

 2004 Prentice Hall, Inc. All rights reserved. Outline 34 PhoneBook.java (8 of 8) 170 { 171 speechSynthesizer.deallocate(); 172 } 173 catch ( Exception myException ) 174 { 175 myException.printStackTrace(); 176 } 177 finally 178 { 179 System.exit( 0 ); 180 } } // end method frameWindowClosed // main method 185 public static void main( String[] args ) 186 { 187 PhoneBook application = new PhoneBook(); } // end method main } // end class PhoneBook Deallocate synthesizer