Using Thread with paint java

I'm developing a checkers game in Java, and I stumbled on a problem. What I'm trying to create is that when a checkerpiece is pressed, the available 2 spots in front of it should turn into grey for 2 seconds.

Turn into grey is easy, but when I want it to turn back after 2 seconds using Thread.sleep(2000), I noticed that it first does the rest of the case in this switch before actually sleeping (so it turns the fresh grey squares back to black immediately)

What went wrong? Thanks 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 it out with timers(I'm new to these so please don't facepalm too hard)

/*
 * To change this template, choose Tools | Templates
 * and open the template in the 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();

        for (int i = 0; i < 8; i++) {
            for (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) {
    }
}

Answers


You could use a Swing javax.swing.Timer to achieve what you 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();

Need Your Help

Android Studio not picking up generated Android Annotations

android-studio android-annotations android-gradle

I've got Android Studio and Android Annotations working fine, though I'm having to add

How to prevent clients from passing a nullptr to a constructor?

c++ c++11 constructor unique-ptr

With a constructor that expects a std::unique_ptr, how do you prevent clients from passing a nullptr effectively?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.