📄 zipcity.java
字号:
/*
* Copyright 1999-2004 Carnegie Mellon University.
* Portions Copyright 2004 Sun Microsystems, Inc.
* Portions Copyright 2004 Mitsubishi Electric Research Laboratories.
* All Rights Reserved. Use is subject to license terms.
*
* See the file "license.terms" for information on usage and
* redistribution of this file, and for a DISCLAIMER OF ALL
* WARRANTIES.
*
*/
package demo.sphinx.zipcity;
import edu.cmu.sphinx.recognizer.Recognizer;
import edu.cmu.sphinx.result.Result;
import edu.cmu.sphinx.util.props.ConfigurationManager;
import edu.cmu.sphinx.util.props.PropertyException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.LayoutManager;
import java.awt.Toolkit;
import java.awt.Graphics;
import java.awt.FontMetrics;
import java.awt.Font;
import java.awt.geom.Rectangle2D;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
/**
* A simple demonstration application for Sphinx-4, suitable for use
* as a WebStart application. ZipCity listens for spoken US zip codes
* and shows the city/state associated with the code.
*/
public class ZipCity extends JFrame {
private final static Color backgroundColor = new Color(0x42, 0x42, 0x42);
private final static Color NORM_COLOR = new Color(0x72, 0x72, 0x82);
private final static Color HIGHLIGHT_COLOR = new Color(0xAA, 0xBB, 0x33);
private final static Font labelFont = new Font("SanSerif", Font.BOLD, 16);
private JTextField messageTextField;
private JButton speakButton;
private JPanel mapPanel;
private JPanel imagePanel;
private ZipInfo currentInfo;
private ZipRecognizer zipRecognizer;
private ZipDatabase zipDB;
private boolean continuousMode = false;
/**
* Constructs a ZipCity with the given title.
*
* @param continuousMode if true recognition is continuous
*/
public ZipCity(boolean continuousMode) {
super("ZipCity - a Sphinx-4 WebStart Demo");
this.continuousMode = continuousMode;
setSize(900, 520);
setDefaultLookAndFeelDecorated(true);
imagePanel = createImagePanel();
mapPanel = createMapPanel();
setApplicationIcon();
getContentPane().add(imagePanel, BorderLayout.CENTER);
getContentPane().add(createMainPanel(), BorderLayout.SOUTH);
// exit if the window is closed
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
if (zipRecognizer != null) {
zipRecognizer.shutdown();
}
System.exit(0);
}
});
// when the 'speak' button is pressed, enable recognition
speakButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if (speakButton.isEnabled()) {
speakButton.setEnabled(false);
startListening();
}
}
});
}
/**
* Replaces the splash s4 logo with the map
*/
public void displayMap() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
getContentPane().remove(imagePanel);
getContentPane().add(mapPanel, BorderLayout.CENTER);
speakButton.setEnabled(true);
validate();
repaint();
}
});
}
/**
* Starts listening for zipcodes
*/
private void startListening() {
if (zipRecognizer.microphoneOn()) {
setMessage("Speak a zip code ...");
} else {
setMessage(
"Sorry, can't find the microphone on your computer.");
}
}
/**
* Perform any needed initializations and then enable the 'speak'
* button, allowing recognition to proceed
*/
public void go() {
try {
setMessage("Loading zip codes...");
zipDB = new ZipDatabase();
setMessage("Loading recognizer...(apologies to Alaska and Hawaii)");
zipRecognizer = new ZipRecognizer();
setMessage("Starting recognizer...");
zipRecognizer.startup();
zipRecognizer.addZipListener(new ZipListener() {
public void notify(String zip) {
updateMap(zip);
if (continuousMode) {
zipRecognizer.microphoneOn();
}
}
});
displayMap();
if (continuousMode) {
startListening();
} else {
setMessage("ZipCity Version 2.0 - Ready");
}
} catch (Throwable e) {
setMessage("Error: " + e.getMessage());
}
}
/**
* Update the display with the new zip code information. The
* zip info is retrieved and if available disabled on the gui
*
* @param zip the zip code (in the form XXXX)
*/
private void updateMap(final String zip) {
final ZipInfo zipInfo = zipDB.lookup(zip);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
if (zip == null) {
setMessage("I didn't understand what you said");
} else {
if (zipInfo == null) {
setMessage("Can't find " + zip + " in the database");
} else {
String location = zipInfo.getCity() + ", "
+ zipInfo.getState();
setMessage("");
currentInfo = zipInfo;
}
}
mapPanel.repaint();
speakButton.setEnabled(true);
}
});
}
/**
* Sets the application icon. The image icon is visible when the
* application is iconified.
*/
private void setApplicationIcon() {
URL url = ZipCity.class.getResource("s4.jpg");
Image image = Toolkit.getDefaultToolkit().getImage(url);
setIconImage(image);
}
/**
* Sets the message to be displayed at the bottom of the Frame.
*
* @param message message to be displayed
*/
public void setMessage(final String message) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
messageTextField.setText(message);
}
});
}
/**
* Enables or disables the "Speak" button.
*
* @param enable boolean to enable or disable
*/
public void setSpeakButtonEnabled(final boolean enabled) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
speakButton.setEnabled(enabled);
}
});
}
/**
* Returns a JPanel with the given layout and custom background color.
*
* @param layout the LayoutManager to use for the returned JPanel
*
* @return a JPanel
*/
private JPanel getJPanel(LayoutManager layout) {
JPanel panel = getJPanel();
panel.setLayout(layout);
return panel;
}
/**
* Returns a JPanel with the custom background color.
*
* @return a JPanel
*/
private JPanel getJPanel() {
JPanel panel = new JPanel();
panel.setBackground(backgroundColor);
return panel;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -