Creating the Select Voice Interface


The SelectVoice.java file creates the user interface to select a voice. Listing 3-2 shows the contents of the SelectVoice.java file:

Listing 3-2: The SelectVoice.java File
start example
 /* Import required swing classes */ import javax.swing.JRadioButton; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JFrame; import javax.swing.ButtonGroup; import javax.swing.JOptionPane; import javax.swing.event.*; /* Import required awt classes */ import java.awt.Font; import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; /* Import required util classes */ import java.util.Enumeration; import java.util.Locale; import java.util.Hashtable; /* Import required speech classes */ import javax.speech.Central; import javax.speech.Engine; import javax.speech.EngineList; import javax.speech.synthesis.Synthesizer; import javax.speech.synthesis.SynthesizerModeDesc; import javax.speech.synthesis.SynthesizerProperties; import javax.speech.synthesis.Voice; /* Class: SelectVoice-Selects the voice in which the synthesizer speaks the text. Fields: retValue: Represents the voice that is currently selected by the user. Methods: getVoices(): Retrieves all the voices in the general locale that are available on the user's computer. actionPerformed(): Determines the operations to be performed when the user clicks any button. */ public class SelectVoice extends JFrame implements ActionListener {    /* Declare variables */    JLabel pageTitle;    JRadioButton rb;    ButtonGroup bg;    JButton ok, cancel;    JPanel voicePanel;    JPanel buttonPanel;    JPanel mainPanel;    Font f, f1;    Hashtable voicehash;    TextPad textpad = null;    /*    Set the default value for the selected voice    */    public static String retValue = "kevin16";    public static String retkey=null;    /*    SelectVoice: Define the default constructor to create the user interface for the SelectVoice.    Parameters:    textpad: An object of TextPad.java class    Return Type: NA    */    public SelectVoice(TextPad textpad)    {       this.textpad=textpad;       this.setSize(150, 200);       this.setResizable(false);       /* Initialize the variables */       pageTitle=new JLabel("Select the voice");       voicePanel=new JPanel();       buttonPanel=new JPanel();       mainPanel=new JPanel();       bg=new ButtonGroup();       /* Set layout for the panels */       buttonPanel.setLayout(new GridLayout(1, 2));       mainPanel.setLayout(new BorderLayout());       /* Create objects of Font class */       f=new Font("Verdana", Font.BOLD, 12);       pageTitle.setFont(f);       f1=new Font("Verdana", Font.BOLD, 10);       /* Initialize the OK button */       ok=new JButton("OK");       ok.setFont(f1);       ok.addActionListener(this);       /* Initialize the cancel button */       cancel=new JButton("Cancel");       cancel.setFont(f1);       cancel.addActionListener(this);       /*       Invoke the getVoices() method to retrieve all available voices.       */       getVoices("general");       /* Add buttons to buttonPanel */       buttonPanel.add(ok);       buttonPanel.add(cancel);       /* Add contents to the mainPanel */       mainPanel.add(pageTitle, BorderLayout.NORTH);       mainPanel.add(voicePanel, BorderLayout.CENTER);       mainPanel.add(buttonPanel, BorderLayout.SOUTH);       /* Add mainPanel to the main window */       getContentPane().add(mainPanel);       /* Add window listener to this window */       addWindowListener(new WindowAdapter()       {          public void windowClosing(WindowEvent we)          {             setVisible(false);          }       });    }    /*    getVoices(): Retrieves all the voices available in the general locale of user computer.    Parameters:    modename: String representing the  selected mode.    Return Type: NA    */    public void getVoices(String modename)    {       /*       Create an object of SynthesizerModeDesc class       */       SynthesizerModeDesc required = new SynthesizerModeDesc       (          null,          modename,          Locale.US,          null,          null);          /*          Retrieve available synthesizers in an object of EngineList class          */          EngineList engineList = Central.availableSynthesizers(required);          /*          Run a loop from 1 to the size of EngineList object          */          voicehash=new Hashtable();          for (int i = 0; i < engineList.size(); i++)           {             /*             Initialize an object of SynthesizerModeDesc class             */             SynthesizerModeDesc desc = (SynthesizerModeDesc) engineList.get(i);             /*             Retrieve the voices available in the current synthesizer             */             Voice[] voices = desc.getVoices();             /* Set layout of the voicePanel */             voicePanel.setLayout(new GridLayout(voices.length,1));             /*             Add all the available voices to the voicePanel             */             for (int j = 0; j < voices.length; j++)              {                String mp=voices[j].getName().toString();                int x=j+1;                String voiceno="Voice "+x;                voicehash.put(voiceno,temp);                System.out.println(temp);                /*                Initialize an object of JRadioButton class                */                rb=new JRadioButton(voiceno);                /*                Set kelvin16 as the default selected voice                */                if(temp.equals("kevin16"))                rb.setSelected(true);                /*                Add JRadioButton to a buttopngroup                */                bg.add(rb);                /*                Add JRadioButton to voicePanel                */                voicePanel.add(rb);             }          }       }       /*       actionPerformed(): Defines the operations to be performed when the user clicks any button.       Parameters:       ev: An object of ActionEvent class       Return Type: NA       */       public void actionPerformed(ActionEvent ev)       {          /*          This code executes when the user clicks the OK button.          */          if(ev.getSource()==ok)          {             /*             Retrieve all the elements in the button group in an enumeration             */             Enumeration enum=bg.getElements();             /*             Run until the enumeration has any element             */             while(enum.hasMoreElements())             {                JRadioButton button=(JRadioButton)enum.nextElement();                /*                Retrieve the label of the JRadioButton that is currently selected.                */                if (button.isSelected())                {                   retkey=button.getText();                   retValue=(String)voicehash.get(retkey);                   this.setVisible(false);                   textpad.setvoice(retValue);                }             }          }          else          /*          This part of code executes when the user clicks cancel button.          */          if(ev.getSource()==cancel)          {             /*             Hide the user interface of SelectVoice.java file             */             this.setVisible(false);          }       }    } 
end example
 

Download this listing .

In the above code, the constructor of the SelectVoice class takes an object of theTextPad class as an input parameter to invoke the methods of the TextPad class. The SelectVoice class allows you to create the user interface to select the voice in which the Voice Synthesizer application reads out the text specified by the end user.

The methods defined in Listing 3-2 are:

  • getVoices (): Retrieves all the voices that an end user's computer supports.

  • actionPerformed (): Acts as an event listener and activates the appropriate method, based on the action an end user performs .

To select the required voice, end users need to select Voice-> Select Voice. The Select the voice dialog box appears, as shown in Figure 3-6:

this figure shows the dialog box to select the voice for the voice synthesizer application.
Figure 3-6: The Select the Voice Dialog Box



Java InstantCode. Developing Applications using Java Speech API
Java InstantCode. Developing Applications using Java Speech API
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 46

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net