0

Hello I am working on a Java Applet.

I have the requirements of listening for click events in a while loop. However, after entering the while loop the quit button is not responding. So my requirement is after clicking on quit button the applet will be closed.

package app;

import java.applet.Applet;
import java.awt.Button;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class test extends Applet implements ActionListener{
     Image img;
     Button b;
     Button b1;
     boolean quit = false;
     //String actionMessage="";

     public void init(){

        b=new Button("Start");
        b1=new Button("Quit");
        add(b);
        add(b1);
        b.addActionListener(this);
        b1.addActionListener(this);
     }
       public void paint(Graphics g){
          b.setLocation(70, 350);
          b1.setLocation(180, 350);
          g.drawImage(img,10,20,300,300,this);

          this.setSize(322,380);

          //g.drawString(actionMessage, 10, 50);
     }

       Button source =null;   
       public void actionPerformed(ActionEvent ae){
           OuterLoop:

        source = (Button)ae.getSource();



    if(source.getLabel()=="Start"){
               img = getImage(getCodeBase(), "images/S2T.png");
               repaint();
               System.out.println("HI");
               quit = true;
                 test t1 = new test ();
                 t1.voce();
           }

           if(source.getLabel()=="Quit"){
               System.out.println("Hello");

               quit = false;
               test t1 = new test ();
             t1.voce();
           }

           /*       if(ae.getSource()==b){
             img = getImage(getCodeBase(), "images/S2T.png");
             repaint();
             quit = true;
             test t1 = new test ();
             t1.voce();
             //actionMessage="Speak now";

        }

        if(ae.getSource()==b1){
             img = getImage(getCodeBase(), "images/S2T1.png");
             //actionMessage="Thanks for using this Software";
             repaint();
             quit = false;
               test t1 = new test ();
             t1.voce();
             System.exit(0);         
        }*/

     }  


    public void voce(){
           voce.SpeechInterface.init("D:/Project/VoiceRecognization/src/", false, true, 
                    "file:D:/Project/VoiceRecognization/src/", "digits");

                System.out.println("This is a speech recognition test. " 
                    + "Speak digits from 0-9 into the microphone. " 
                    + "Speak 'quit' to quit.");

                while (!quit)
                {

                    // Normally, applications would do application-specific things 
                    // here.  For this sample, we'll just sleep for a little bit.
                    try
                    {
                        Thread.sleep(200);
                    }
                    catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }

                    while (voce.SpeechInterface.getRecognizerQueueSize() > 0)
                    {
                        String s = voce.SpeechInterface.popRecognizedString();

                        if(s.equals("san")){
                            System.out.println("DONE IT !!!!!!!!!!!!!" );
                            Runtime rs = Runtime.getRuntime();
                            try{
                                /*rs.exec("notepad");*/
                                //rs.exec("explorer.exe");
                                rs.exec("explorer d:\\Project\\VoiceRecognization");
                                //Desktop.getDesktop().open(new File("C:\\folder"));
                            }catch(Exception ie){
                                ie.printStackTrace();
                            }
                            System.out.println("Done");                 
                        }else if(s.equals("hello")){
                            System.out.println("DONE IT !!!!!!!!!!!!!" );
                            Runtime rs = Runtime.getRuntime();
                            try{
                                /*rs.exec("notepad");*/
                                rs.exec("explorer =");
                                /*rs.exec("explorer d:\\Project\\VoiceRecognization");*/
                                //Desktop.getDesktop().open(new File("C:\\folder"));
                            }catch(Exception ie){
                                ie.printStackTrace();
                            }
                            System.out.println("Done");                 
                        }else{
                            System.out.println("In the else part");
                        }


                        // Check if the string contains 'quit'.
                        if (-1 != s.indexOf("quit"))
                        {
                            quit = true;
                            System.out.println("NOT DONE IT +++++++++++ " );
                        }

                        System.out.println("You said: " + s);
                        //voce.SpeechInterface.synthesize(s);



                     }
                    continue OuterLoop;
}
                }
       }
Andrew Regan
  • 5,087
  • 6
  • 37
  • 73
  • [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) and [Java Plugin support deprecated](http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/) and [Moving to a Plugin-Free Web](https://blogs.oracle.com/java-platform-group/entry/moving_to_a_plugin_free) – MadProgrammer Apr 13 '16 at 21:24
  • You problem is likely one associated with concurrency, you are blocking the event dispatching thread which will prevent the UI from responding to new events until the while loop exists – MadProgrammer Apr 13 '16 at 21:26
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Apr 14 '16 at 02:01
  • Can you suggest me some example from that , So I can run my loop on separate thread . – bhavesh bhavsar Apr 14 '16 at 07:39

0 Answers0