multithreading - Using Thread with paint java -


i'm developing checkers game in java, , stumbled on problem. i'm trying create when checkerpiece pressed, available 2 spots in front of should turn grey 2 seconds.

turn grey easy, when want turn after 2 seconds using thread.sleep(2000), noticed first rest of case in switch before sleeping (so turns fresh grey squares black immediately)

what went wrong? in advance!

switch (bord[ypos][xpos]) {                 case 0:                      break;                 case 1:                      break;                 case 2:                      if (bord[ypos + 1][xpos - 1] == 1) {                         bord[ypos + 1][xpos - 1] = 4;                      }                     if (bord[ypos + 1][xpos + 1] == 1) {                         bord[ypos + 1][xpos + 1] = 4;                      }                     repaint();                     try {                         thread.sleep(2000);                     } catch (interruptedexception e) {                     }                     if (bord[ypos + 1][xpos - 1] == 4) {                         bord[ypos + 1][xpos - 1] = 1;                     }                     if (bord[ypos + 1][xpos + 1] == 4) {                         bord[ypos + 1][xpos + 1] = 1;                     }                     break;                 case 3:                      break;             } 

trying out timers(i'm new these please don't facepalm hard)

/*  * change template, choose tools | templates  * , open template in editor.  */ package dammen;  import java.awt.color; import java.awt.dimension; import java.awt.graphics; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.mouseevent; import java.awt.event.mouselistener; import java.util.logging.level; import java.util.logging.logger; import javax.swing.jpanel; import javax.swing.timer;  /**  *  * @author boyen  */ public class board extends jpanel implements mouselistener {      boolean test;     boolean black = false;     boolean redpiece = false;     boolean bluepiece = true;     timer timer;     private int[][] bord = {{0, 2, 0, 2, 0, 2, 0, 2},         {2, 0, 2, 0, 2, 0, 2, 0},         {0, 2, 0, 2, 0, 2, 0, 2},         {1, 0, 1, 0, 1, 0, 1, 0},         {0, 1, 0, 1, 0, 1, 0, 1},         {3, 0, 3, 0, 3, 0, 3, 0},         {0, 3, 0, 3, 0, 3, 0, 3},         {3, 0, 3, 0, 3, 0, 3, 0}};      public board(dammen parent) {         addmouselistener(this);         timer = new timer(100,taskperformer);         timer.setrepeats(false);     }      public void start() {     }      public void paint(graphics g) {         super.paint(g);          dimension size = getsize();          (int = 0; < 8; i++) {             (int j = 0; j < 8; j++) {                 switch (bord[i][j]) {                     case 0:                         g.setcolor(color.white);                         break;                     case 1:                         g.setcolor(color.black);                         break;                     case 2:                         g.setcolor(color.red);                         break;                     case 3:                         g.setcolor(color.blue);                         break;                     case 4:                         g.setcolor(color.gray);                         break;                 }                 g.fillrect((size.width / 8) * j, (size.height / 8) * i, size.width / 8, size.height / 8);             }          }     }     int mousex, mousey;      @override     public void mouseclicked(mouseevent e) {           mousex = e.getx();         mousey = e.gety();          zoekmogelijkespots(mousex, mousey);      }     actionlistener taskperformer = new actionlistener() {          @override         public void actionperformed(actionevent e) {          }   };      public void zoekmogelijkespots(int mousex, int mousey) {              dimension size = getsize();             system.out.println(mousex + "," + mousey);             int xpos;             int ypos;             xpos = (int) (mousex / (size.width / 8));             ypos = (int) (mousey / (size.height / 8));             system.out.println(ypos + "," + xpos);             system.out.println(bord[ypos][xpos]);             switch (bord[ypos][xpos]) {                 case 0:                      break;                 case 1:                      break;                 case 2:                      if (bord[ypos + 1][xpos - 1] == 1) {                         bord[ypos + 1][xpos - 1] = 4;                      }                     if (bord[ypos + 1][xpos + 1] == 1) {                         bord[ypos + 1][xpos + 1] = 4;                      }                     repaint();                     timer.start();                     if (bord[ypos + 1][xpos - 1] == 4) {                         bord[ypos + 1][xpos - 1] = 1;                     }                     if (bord[ypos + 1][xpos + 1] == 4) {                         bord[ypos + 1][xpos + 1] = 1;                     }                     break;                 case 3:                      break;             }       }      @override     public void mousepressed(mouseevent e) {     }      @override     public void mousereleased(mouseevent e) {     }      @override     public void mouseentered(mouseevent e) {     }      @override     public void mouseexited(mouseevent e) {     } } 

you use swing javax.swing.timer achieve want...

if (bord[ypos + 1][xpos - 1] == 1) {     bord[ypos + 1][xpos - 1] = 4; } if (bord[ypos + 1][xpos + 1] == 1) {     bord[ypos + 1][xpos + 1] = 4; } repaint(); timer timer = new timer(2000, new actionlistener() {     public void actionperformed(actionevent evt) {         if (bord[ypos + 1][xpos - 1] == 4) {             bord[ypos + 1][xpos - 1] = 1;         }         if (bord[ypos + 1][xpos + 1] == 4) {             bord[ypos + 1][xpos + 1] = 1;         }     } }); timer.setrepeats(false); timer.start(); 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -