0

I have a problem.

In my program i have 2 classes:

  1. MainFrame

    public class MainFrame extends javax.swing.JFrame 
    {
    
        Logika logika;
    
        .
        .
        .
    
        private void przyciskKeyPressed(java.awt.event.KeyEvent evt) {
            // TODO add your handling code here:
            char znak = evt.getKeyChar();
            int kod = evt.getKeyCode();
    
    
            if(kod==KeyEvent.VK_A)
            {
                logika.key_pressed("a");
            }
        }
    
    }
    
  2. Logika

    public class Logika {
    
        .
        .
        .
    
    
        Calendar cal = Calendar.getInstance();
    
    
    
    
        public void start()
        {
            gra_rozpoczeta=true;
            punkty=0;
            liczba=1;
            x[0]=251;
            y[0]=301;
    
            cal = Calendar.getInstance();
            czas=cal.getTimeInMillis();
            while(gra_rozpoczeta==true)
            {   
                cal=Calendar.getInstance();
    
                dif=dif-(int)(cal.getTimeInMillis()-czas);
                if(dif<0)
                    akcja();
                try {
                    Thread.currentThread().sleep(50);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Logika.class.getName()).log(Level.SEVERE, null, ex);
                }
                czas=cal.getTimeInMillis();
            }
    
        }
    
        private void akcja()
        {
            dif=500;
    
            liczba++;
            if(liczba==10)
                key_presed=true;
            if(key_presed==true)
                gra_rozpoczeta=false;
        }
    
        public void key_pressed(String s)
        {
            key_presed=true;
            key=s;
        }
    
    }
    

With this methods i have problem.

Loop while last 5 seconds but i want to stop it earlier if i will press key "a".

Now it is working that: -program start, -loop last 5 seconds(im pressing "a") -The program responds to the pressed key's only after the loop

Is it possible to do ?

Steve Czetty
  • 6,147
  • 9
  • 39
  • 48
user1736332
  • 693
  • 4
  • 11
  • 22

1 Answers1

0

Your code examples are a bit lacking, but I would guess that this behaviour is due to improper use of threads. When your mainframe starts the loop, it does not start it in a new thread, it starts it in the main thread, the one that is also listening to keyboard input. So while the loop is running, keyboard entry is blocked, and processed only after the loop is finished.

If you want to use logica in a different thread, you must change it to be like this:

public class Logica implements Runnable {
    [...]
    @Override
    public void run() {
        [do loop]
    }
}

and start it as a new thread in MainFrame like this:

//start the loop as a new thread...
final Thread thread = new Thread(logika);
thread.start();
//keep listening for keyboard input in the main thread
Tobb
  • 11,850
  • 6
  • 52
  • 77