Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  sleep thread within four loop JFrame  (Read 1521 times)
0 Members and 1 Guest are viewing this topic.
Offline Shane75776
« Posted 2009-02-11 18:27:41 »

My progrm is a Keno like game where you choose your numbers out of a grid of JTextFields and then you click play and the computer should randomly change the textfield colors up to 20 times.

My problem

Alright so i have a sleep thread within my four loop, the sleep thread should pause whats happening within the four loop for a second and then change the textfield color every second but instead it waits like 10 seconds and displays all the random textfields that were chosen and changed there colors all at the same time.

Here is the code

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
   public void computerPick()
   {

     

      for(int i = 0; i < rollAmnt; i++)
      {
         
         
     
         
      SwingUtilities.invokeLater(new Runnable()
      {
         public void run()
         {
           
            try
            {
               roll = (int)(Math.random()* 79)+ 1;
               Thread.sleep(100);
               if(field[roll].getBackground() == Color.white)
               {
                  field[roll].setBackground(Color.orange);
               }
               else if(field[roll].getBackground() == Color.green)
               {
                  field[roll].setBackground(Color.cyan);
                  win++;
               }
               else if(field[roll].getBackground() == Color.cyan)
               {
                  field[roll].setBackground(Color.magenta);
                  field[roll].setText("NICE!");
                  win++;
               }
            }
            catch(InterruptedException aoe){}
         }
      });
      }
     
     
     
   }


please if anyone could edit this and show me what i can do to fix this i would appreciate it a ton.

Check out my Snipping Tool++ ! An advanced snippet/screenshot/text uploading tool! Meant to replace the windows snipping tool.

Check out Pixel Rain My most recent Swing based game!
Offline Mr_Light

Senior Duke


Medals: 1


shiny.


« Reply #1 - Posted 2009-02-11 18:58:29 »

In short: You only use 'one' thread the same thread that handles the painting EDT.

Have a look at http://java.sun.com/javase/6/docs/api/java/util/concurrent/ScheduledExecutorService.html

so something like this should work:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
final Runnable roll = new Runnable() {
   public void run() {
      try {
         roll = (int)(Math.random()* 79)+ 1;

         if(field[roll].getBackground() == Color.white)   {
            field[roll].setBackground(Color.orange);
         } else if(field[roll].getBackground() == Color.green) {
            field[roll].setBackground(Color.cyan);
            win++;
         } else if(field[roll].getBackground() == Color.cyan)   {
            field[roll].setBackground(Color.magenta);
            field[roll].setText("NICE!");
            win++;
         }
      } catch(InterruptedException aoe){
         Thread.interrupt();
      }
   }
};
Runnable task = new Runnable() {
   public void run() {
      SwingUtilities.invokeLater(roll);
   }
}
for(int i = 1; i < rollAmnt + 1; i++)   {
   executor.schedule(task, 100 * i, TimeUnit.MILLISECONDS);
}


Though you should probably change your roll code to something else.

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Offline Mr_Light

Senior Duke


Medals: 1


shiny.


« Reply #2 - Posted 2009-02-11 19:13:08 »

Oh my bad, I forgot to do invokeLater

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Shane75776
« Reply #3 - Posted 2009-02-12 07:25:27 »

cool thanks ill try this out and let you know if it works :-)

Check out my Snipping Tool++ ! An advanced snippet/screenshot/text uploading tool! Meant to replace the windows snipping tool.

Check out Pixel Rain My most recent Swing based game!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (44 views)
2014-10-16 15:22:06

Norakomi (34 views)
2014-10-16 15:20:20

lcass (38 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (68 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45

BurntPizza (87 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!