I have a problem.
In my program i have 2 classes:
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"); } } }
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 ?