My program has 2 threads, a turn thread and a timer thread. When the player enters some input the timer thread stops and if the user doesn't reply in 10 seconds, the user thread gets stopped.
The problem comes in the way i get the input from the user. So far im using JOptionPane and when the timer kills the turn thread the panel remains. Any ideas of how to remove it?
I tried creating a JOptionPane object, initializing it in Turn's run method.
Im sort of new to threads and guis with java. Any help/ideas are appreciated.
class Turn extends Thread {
private boolean done=false;
public void run() {
Main.ans = JOptionPane.showOptionDialog(Main.test, "Is the celebrity's answer correct?", "word", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
if(!done) {
Main.terminateTimer();
System.out.println("Timer got killed");
System.out.println("ANS: "+Main.ans);
}
}
public void kill() {
done=true;
}
}
class TimerThread extends Thread {
private boolean done=false;
public static long startTime = System.currentTimeMillis();
protected static final long age() {
return (System.currentTimeMillis() - startTime);
}
public void run() {
System.out.println(this.getName());
while(age()<1000 && !done) {;} //wait 1 second
if(!done) {
Main.terminateTurn(); // terminate players turn
System.out.println("TIMES UP!");
System.out.println("ANS: "+Main.ans);
}
}
public void kill() {
done = true;
}
}
public class Main {
static int ans=-1;
static Turn turn = new Turn();
static TimerThread timer = new TimerThread();
public static void terminateTurn() {
turn.kill();
}
public static void terminateTimer() {
timer.kill();
}
public static void main(String[] args) {
turn.start();
timer.start();
}
}

New Topic/Question
Reply




MultiQuote



|