import javax.speech.*; import javax.speech.recognition.*; import java.io.FileReader; //required to read grammar file import java.util.Locale; //required for locale in createRecognizer call import java.io.*; import java.awt.*; import java.applet.*; import javax.swing.*; import java.awt.event.*; import java.net.*; public class test_applet extends javax.swing.JApplet implements ActionListener, Runnable{ Thread listenning; int current = 0; String Affich; static Recognizer rec; static RuleGrammar gram; static Rule r1; //image Image back, workspace; Graphics offscreen; int x1=0; int x2; String text = "Listening"; String received ="machin"; // Listening display JPanel Display = new JPanel(); JLabel DisplayLabel = new JLabel(text); // Buttons JPanel Buttons = new JPanel(); JButton Start = new JButton("Start"); JButton Stop = new JButton("Stop"); static ResultListener ruleListener = new ResultAdapter() { public void resultAccepted(ResultEvent e) { System.out.println("said: "); FinalRuleResult result = (FinalRuleResult) e.getSource(); String [] tags = result.getTags(); if(tags != null){ if(tags[0].equals("GT")== true) { if(tags[1].equals("ho")== true) try{ Process r = Runtime.getRuntime().exec("cmd /c start http://localhost/MonSite/"); URL fileURL = new URL("http://localhost/MonSite/"); URLConnection c=fileURL.openConnection(); c.connect () ; } catch(Exception rex) { rex.printStackTrace(); } // System.out.println("Open"); System.out.println("said: " + tags[0] + tags[1] ); } else if (tags[0].equals("CL")== true){ try{ Process r = Runtime.getRuntime().exec("cmd /c start http://localhost/MonSite/computing.php"); } catch(Exception rex) { rex.printStackTrace(); } System.out.println("Close"); System.out.println("Tags received:"+ tags[0]); } else System.out.println("Tags received:"+ tags[0]); } else System.out.println("no tag received"); } public void resultRejected(ResultEvent e) { System.out.println("rejected"); } public void resultUpdated(ResultEvent e) { System.out.println("updated"); } }; class vrec1{ public vrec1(String gramfile) { try { // Create a recognizer that supports English. rec = Central.createRecognizer(new EngineModeDesc(Locale.ENGLISH)); //System.out.println("Starting recognizer . . . "); Affich = "Starting recognizer . . . " ; // System.out.println("How now brown cow OR my dog has fleas"); rec.allocate(); // Start up the recognizer // Load the grammar from a file, and enable it -- all of the next //seven lines are required when you load or change grammar file FileReader reader = new FileReader(gramfile); gram = rec.loadJSGF(reader); gram.addResultListener(ruleListener); gram.setEnabled(true); rec.commitChanges(); rec.requestFocus(); rec.resume(); } catch (Exception e) { e.printStackTrace(); } } public void main(String args[]) { vrec1 vr = new vrec1("gram2.txt"); } } //**************************************************** init() ***************************************************************** public void init() { vrec1 vr = new vrec1("gram2.txt"); //image workspace = createImage(getSize().width, getSize().height); offscreen = workspace.getGraphics(); String imageBack = getParameter("background"); if (imageBack !=null) back = getImage(getDocumentBase(), imageBack); x2= getSize().width; //container Container SpeechInterface = getContentPane(); BorderLayout appletLayout = new BorderLayout (); SpeechInterface.setLayout(appletLayout); //listeners Start.addActionListener(this); Stop.addActionListener(this); FlowLayout DisplayLayout = new FlowLayout (FlowLayout.CENTER); SpeechInterface.setLayout(DisplayLayout); Display.add(DisplayLabel, BorderLayout.CENTER); SpeechInterface.add(Display); FlowLayout ButtonsLayout = new FlowLayout (FlowLayout.CENTER); SpeechInterface.setLayout(ButtonsLayout); Buttons.add(Start, BorderLayout.SOUTH); Buttons.add(Stop, BorderLayout.SOUTH); SpeechInterface.add(Buttons); setContentPane(SpeechInterface); } public void start() { if (listenning == null){ listenning = new Thread(this); listenning.start(); } } public void run(){ Thread thisThread = Thread.currentThread(); while (listenning == thisThread){ repaint(); current++; if (current > 5) current=0; try{ Thread.sleep(1000); }catch (InterruptedException e){ } } } public void stop(){ if (listenning != null){ listenning = null; } } //******************************************************** Display Image ***************************************************** public void paint(Graphics screen){ Graphics2D screen2D = (Graphics2D) screen; offscreen.drawImage(back, x1, 0, null); offscreen.drawImage(back, x2, 0, null); screen2D.drawImage(workspace, 0, 0, this); screen2D.drawString("Starting applet . . .", 5, 50); //screen2D.drawString(Affich, 10, 100); } public void update(Graphics screen){ paint(screen); } //*********************************************** Actions ********************************************************************** public void actionPerformed(ActionEvent event){ String command = event.getActionCommand(); if (command == "Start"){ listenning = new Thread(this); Start.setEnabled(false); Stop.setEnabled(true); } if (command == "Stop"){ Start.setEnabled(true); Stop.setEnabled(false); listenning = null; } } }