Jump to content

Need some help with Java timers

- - - - -

  • Please log in to reply
2 replies to this topic

#1
Serialcek

Serialcek

    Learning Programmer

  • Members
  • PipPipPip
  • 72 posts
Hello I have a little bit of problems with stoping one of the timers in my program.
I will try to explain my problem trough my simplified code.



public int timer_true;

public int timer_false;

public int timer_false1;

public int timer_false2;

...


public int points = 0;

public int points_true = 0;

public int points false1 = 0;

public int points false2 = 0;

...


Public class problem {


public void Presed(){

if (Button Start.Pressed){ //When the button Start is pressed I generate some random integers which I will use in my functions with timers


timer_true = (int) (Math.random() * (4 - 2+ 1)) + 2;

timer_false1 = (int) (Math.random() * (4 - 2+ 1)) + 2;

timer_false2 = (int) (Math.random() * (4 - 2+ 1)) + 2;

...


// After generating random timers I start the functions

true_method();

false_method1();

false_method2();

...


}

}


public void start_true() {


		final Timer timer = new Timer(); 

		timer.schedule(new TimerTask() {

			public void run() {



if(points>points_true){ // IF I DO THAT THE TIMER WILL STOP FOREVER, THE FUNCTION start_true() WILL NOT RUN

timer.cancel();

points_true++;

}



			  if (The right button is pressed){

                            points++;

                            timer.cancel(); //THIS IS NOT WORKING

                            restart_method(); //THIS METHOD DOES THE SAME AS THE METHOD Presed();

                                     }	

				}

			}

		}, 0, true_method * 1000); 

							


	}



public void start_false() {


		final Timer timer = new Timer(); 

		timer.schedule(new TimerTask() {

			public void run() {



if(points>points_false){ THIS IS WORKING GREAT!!

timer.cancel();

points_false++;

}



			  if (The wring button is pressed){

   

                             System.out.print(You lost);

                                     }	

				}

			}

		}, 0, true_method * 1000); 

							


	}




}



I will try to explain a little bitt. I have a game with timers. If a player chose the right solution I would like that al the timers reset and a now problem to solve is shown to the player. The problem is that the timers are reseted in the right way just for the timers of the wrong solutions.
I don't know how to reset the or stop or whatever the timer of the right solution.

Let's say that the first int timer is 2 (2 second) and I answer correct and the method generate a new timer which let's say is again 2 and then it wil work like the timer is set for 1 Second.

I hope you guys understood my problem.. if I was clear enough tell me, and I will try to re-explain my problem.

Thx to anyone who will try to help me!

Edited by Serialcek, 12 December 2011 - 07:21 AM.


#2
Serialcek

Serialcek

    Learning Programmer

  • Members
  • PipPipPip
  • 72 posts
So I really don't know where to search for my errors..
This is a complete code of the method start_true();



	public void start_true() {


		final Timer timer = new Timer();


		timer.schedule(new TimerTask() {


			public void run() {


				 System.out.println("Timer prvi je: " + FIRST);

				 

				


				if (p <= -1) {

					timer.cancel();

					enacba.setText("Too late!");

					konec();

					return;

				}


				grid[p][P].setText(true_answer);

				grid[p][P].setHorizontalTextPosition(JButton.CENTER);

				grid[p][P].setVerticalTextPosition(JButton.CENTER);



				if (p < 10) {

					grid[p + 1][P].setText("");

				}


				if (p >= 0) {

					p--;

				}


				if (equation.getText().equals("WRONG!")) {

					timer.cancel();

					konec();


				}


				for (int x = 0; x <= 10; x++) {

					grid[x][P].addMouseListener(new java.awt.event.MouseAdapter() {

						

								public void mousePressed(

										java.awt.event.MouseEvent evt) {


									

									for (int x = 0; x <= 10; x++) {

										if (grid[x][P] == evt.getSource()) {


											System.out.println("X je: "+x);

											

											if (grid[x][P].getText()

													.equals(true_answer)) {


												timer.cancel();


												st_tock++;

												

												System.out.println("Bravo");


												reeset();

												return;

											}

											

											else{

											}

										}


									}


									Pressed(evt);

								}

							});


				}

			}


		}, 0, FIRST * 1000); // initial delay,

							// subsequent delay

	}




So this is it.. When I play this "game" it works ok till some point. And then it looks like the method start_true(); was started for 2 times and this go faster then the timer.
I don't know if is a problem with this method, so I am asking here in the forum if someone can give me some advice what should I change or where should I pay more attention.

Thx

#3
Serialcek

Serialcek

    Learning Programmer

  • Members
  • PipPipPip
  • 72 posts
So It think that I found a problem. In my grid layout I put an answer on the label and when the label is pressed I reset everything except mouse listener. So when the answer is putted again on the same column, when is clicked it recognize the click twice (depend on how many times the answer was on the same column).
Can be this true? As I tested is this the problem.
I have one solution and I need some help with it:


		final MouseListener ml = new MouseAdapter() {

					public void mouseClicked(java.awt.event.MouseEvent evt) {

						mouseClicked(evt);

				

				

				for (int x = 0; x <= 10; x++) {

				grid[x][P].addMouseListener (ml); // Here it tels me that local variable ml may not have been 

initialized, and I don't know what to.

				}

				

				

				for (int x = 0; x <= 10; x++) {

				if (grid[x][P] == evt.getSource()) {

					

					

					if (grid[x][P].getText()

							.equals(res)) {

						


						for (int y = 0; y <= 10; y++) {

							grid[x][P].removeMouseListener (ml);

							}

						

						reset();

						timer.cancel();

					

				

				   }

				}

				

				}

				

					}

				};



What I would like to do is to remove the mouse listener when the player will answer correctly.
So could be this the right solution for my problem or should I look for something else?
Have fun!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users