Thread.sleep()和GUI

时间:2016-11-12 20:13:22

标签: java multithreading swing user-interface sleep

我刚刚在论坛上搜索过但我找不到任何可以帮助我的东西..我的问题如下:我正在尝试创建一个小游戏,你必须在你朋友的背后走,才能赢,当其中一个人达到这个目的时,游戏结束。 这是我用来解释我的问题的简单代码

我的应用程序中有3个类(属于包代码):

  • 主要

  • 游戏

  • 位置

我的应用程序中有9个.png(属于img包):

  • 0.png
  • j1_0.png
  • j1_1.png
  • j1_2.png
  • j1_3.png
  • j2_0.png
  • j2_1.png
  • j2_2.png
  • j2_3.png

两个软件包都在src root

头等舱:

package code;

public class Main{

    public static Game game;

    public static void main(String[] args){

        game = new Game(8, 8);

    }

}

秒级:

package code;

import javax.swing.JPanel;
import javax.swing.JLabel;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

@SuppressWarnings("serial")
public class Game extends JFrame{

    private Position j1, j2;
    private JPanel panel;
    private JLabel[][] grid;
    private int largeur, hauteur;

    public Game(int hauteur, int largeur){ // >= 2

        super("Rattrape l'autre");
        this.largeur = largeur;
        this.hauteur = hauteur;
        this.panel = new JPanel(new GridBagLayout());
        this.add(this.panel);
        this.grid = new JLabel[largeur][hauteur];
        int x1 = (int) (Math.random() * largeur), y1 = (int) (Math.random() * hauteur), x2, y2;
        do{
            x2 = (int) (Math.random() * largeur);
            y2 = (int) (Math.random() * hauteur);
        }while(x2 == x1 && y2 == y1);
        this.j1 = new Position(x1, y1, (int) (Math.random() * 4));
        this.j2 = new Position(x2, y2, (int) (Math.random() * 4));
        updatePanel();
        this.setResizable(false);
        this.pack();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        this.addKeyListener(new KeyListener(){

            @Override
            public void keyPressed(KeyEvent e){

                boolean over = false;
                if(e.getKeyCode() == KeyEvent.VK_Z){
                    if(j1.getY() != 0 && !(j1.getY() == j2.getY() + 1 && j1.getX() == j2.getX())){
                        j1.goesUp();
                    }else if(j2.getDirection() == 0 && j1.getY() == j2.getY() + 1 && j1.getX() == j2.getX()){
                        over = true;
                    }
                    j1.turnUp();
                }else if(e.getKeyCode() == KeyEvent.VK_D){
                    if(j1.getX() != largeur - 1 && !(j1.getX() == j2.getX() - 1 && j1.getY() == j2.getY())){
                        j1.goesRight();
                    }else if(j2.getDirection() == 1 && j1.getX() == j2.getX() - 1 && j1.getY() == j2.getY()){
                        over = true;
                    }
                    j1.turnRight();
                }else if(e.getKeyCode() == KeyEvent.VK_S){
                    if(j1.getY() != hauteur - 1 && !(j1.getY() == j2.getY() - 1 && j1.getX() == j2.getX())){
                        j1.goesDown();
                    }else if(j2.getDirection() == 2 && j1.getY() == j2.getY() - 1 && j1.getX() == j2.getX()){
                        over = true;
                    }
                    j1.turnDown();
                }else if(e.getKeyCode() == KeyEvent.VK_Q){
                    if(j1.getX() != 0 && !(j1.getX() == j2.getX() + 1 && j1.getY() == j2.getY())){
                        j1.goesLeft();
                    }else if(j2.getDirection() == 3 && j1.getX() == j2.getX() + 1 && j1.getY() == j2.getY()){
                        over = true;
                    }
                    j1.turnLeft();
                }
                if(e.getKeyCode() == KeyEvent.VK_UP){
                    if(j2.getY() != 0 && !(j2.getY() == j1.getY() + 1 && j2.getX() == j1.getX())){
                        j2.goesUp();
                    }else if(j1.getDirection() == 0 && j2.getY() == j1.getY() + 1 && j2.getX() == j1.getX()){
                        over = true;
                    }
                    j2.turnUp();
                }else if(e.getKeyCode() == KeyEvent.VK_RIGHT){
                    if(j2.getX() != largeur - 1 && !(j2.getX() == j1.getX() - 1 && j2.getY() == j1.getY())){
                        j2.goesRight();
                    }else if(j1.getDirection() == 1 && j2.getX() == j1.getX() - 1 && j2.getY() == j1.getY()){
                        over = true;
                    }
                    j2.turnRight();
                }else if(e.getKeyCode() == KeyEvent.VK_DOWN){
                    if(j2.getY() != hauteur - 1 && !(j2.getY() == j1.getY() - 1 && j2.getX() == j1.getX())){
                        j2.goesDown();
                    }else if(j1.getDirection() == 2 && j2.getY() == j1.getY() - 1 && j2.getX() == j1.getX()){
                        over = true;
                    }
                    j2.turnDown();
                }else if(e.getKeyCode() == KeyEvent.VK_LEFT){
                    if(j2.getX() != 0 && !(j2.getX() == j1.getX() + 1 && j2.getY() == j1.getY())){
                        j2.goesLeft();
                    }else if(j1.getDirection() == 3 && j2.getX() == j1.getX() + 1 && j2.getY() == j1.getY()){
                        over = true;
                    }
                    j2.turnLeft();
                }
                updatePanel();
                if(over){
                    end();
                }

            }

            @Override
            public void keyReleased(KeyEvent e){}

            @Override
            public void keyTyped(KeyEvent e){}

        });

    }

    public void updatePanel(){

        GridBagConstraints c = new GridBagConstraints();
        this.panel.removeAll();
        for(int x = 0; x < largeur; x++){
            for(int y = 0; y < hauteur; y++){
                if(x == j1.getX() && y == j1.getY()){
                    this.grid[x][y] = new JLabel(new ImageIcon(Main.class.getResource("/img/j1_" + j1.getDirection() + ".png")));
                }else if(x == j2.getX() && y == j2.getY()){
                    this.grid[x][y] = new JLabel(new ImageIcon(Main.class.getResource("/img/j2_" + j2.getDirection() + ".png")));
                }else{
                    this.grid[x][y] = new JLabel(new ImageIcon(Main.class.getResource("/img/0.png")));
                }
                c.gridx = x;
                c.gridy = y;
                this.panel.add(this.grid[x][y], c);
            }
        }
        this.panel.revalidate();

    }

    public void end(){

        try{
            Thread.sleep(1000);
        }catch(InterruptedException e){}
        Main.game.dispose();


    }

}

第三课:

package code;

public class Position{

    private int x, y, direction;

    public Position(int x, int y, int direction){

        this.x = x;
        this.y = y;
        this.direction = direction;

    }

    public int getX(){

        return this.x;

    }

    public int getY(){

        return this.y;

    }

    public int getDirection(){

        return this.direction;

    }

    public void goesUp(){

        this.y--;

    }

    public void goesRight(){

        this.x++;

    }

    public void goesDown(){

        this.y++;

    }

    public void goesLeft(){

        this.x--;

    }

    public void turnUp(){

        this.direction = 0;

    }

    public void turnRight(){

        this.direction = 1;

    }

    public void turnDown(){

        this.direction = 2;

    }

    public void turnLeft(){

        this.direction = 3;

    }

}

最后,我的问题是这个问题:

我希望这样,例如,当玩家1向左看并且他只是在玩家2的顶部时,而玩家2向下看,如果玩家1向下移动,它应该赢得游戏,但是在显示之后球员1往下看。

我尝试了很多输入o测试,它似乎工作,因为当over设置为true时,程序进入updatePanel()然后启动end(),它应该睡眠1秒然后处理frame ..但它没有,它确实在最后启动updatePanel(),它更新j1的值(作为前面的算法,我的意思是在键盘输入功能中赋值的那个,让他看不起)但它没有显示新的网格,所以,我迷路了:p

感谢您的建议,我希望您能解决问题,加上算法很简单,而且时间不长,所以对您来说理解它应该不难:)

1 个答案:

答案 0 :(得分:0)

  

应该睡1秒然后处理框架..

您无法在Thread.sleep()上使用Event Dispatch Thread(EDT)。这将导致GUI进入睡眠状态,这意味着它不会重新绘制。

相反,您需要使用单独的Thread,可能是SwingWorker。 SwingWorker将允许您使用Thread.sleep(),然后您可以在完成睡眠时发布结果。

阅读Concurrency上Swing教程中的部分,了解更多信息和示例。