Java,15个益智游戏的倒数计时器?请帮助我了解计时器,

问题描述 投票:0回答:1

[在开始之前,我想解释一下,我已经看过许多其他问题,例如网络上的许多视频,但是这些视频都无法帮助我了解计时器的工作原理或如何为自己创建一个计时器,希望没有一个认为这是不必要的帖子。

例如,我不明白延迟的用途,我想学习有关计时器及其使用方法的所有知识。

所以我正在创建一个15拼图游戏,游戏已经完成,但是我想添加另一个功能。

显示在JLabel上的计时器。详情:

  1. 计时器从5或10分钟倒数。
  2. 计时器的期望格式“ mm:ss”。
  3. 只要按下第一个按钮,计时器就会开始倒计时。
  4. 如果在计时器用尽之前解决,则您赢了,否则您输了。
  5. 如果您按“新游戏”按钮,它将重置为5或10分钟。
  6. 当您按下游戏中的第一个按钮时,倒数便再次开始。

如果您能解释这些参数的用途以及我应该为我的情况使用的计时器,我会很乐意。我也在用java swing。

到目前为止,如果您有兴趣,这是我的比赛:

class GameLogic extends JFrame implements ActionListener {

private JPanel grid = new JPanel();
private JPanel overHead = new JPanel();
private JButton newGameButton = new JButton("NEW GAME");
private JButton[][] buttons = new JButton[4][4];
private JButton[][] winPattern = new JButton[4][4];
private JButton button0 = new JButton("");
private JLabel timerLabel = new JLabel("TIMER");
private int emptyIndex;
private int sourceIndex;
private int sourceRow;
private int sourceCol;
private int blankRow;
private int blankCol;
private int movesCounter = 0;
private JLabel movesLabel = new JLabel("MOVES");

public GameLogic() {
    setLayout(new BorderLayout());
    add(overHead, BorderLayout.NORTH);
    overHead.setLayout(new BorderLayout());
    overHead.add(newGameButton, BorderLayout.WEST);
    overHead.add(movesLabel, BorderLayout.EAST);
    overHead.add(timerLabel, BorderLayout.CENTER);
    movesLabel.setFont(new Font("Street Cred", Font.PLAIN, 20));
    newGameButton.setPreferredSize(new Dimension(100, 50));
    newGameButton.setFont(new Font("Street Cred", Font.PLAIN, 13));
    overHead.setBorder(new EmptyBorder(50, 50, 10, 50));
    grid.setBorder(new EmptyBorder(50, 50, 50, 50));
    newGameButton.addActionListener(this);
    add(grid, BorderLayout.CENTER);
    setBackground(Color.RED);
    grid.setLayout(new GridLayout(4, 4));
    try {
        GraphicsEnvironment ge =
                GraphicsEnvironment.getLocalGraphicsEnvironment();
        ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File("street cred.ttf")));
    } catch (IOException | FontFormatException e) {
        //Handle exception
    }
    int i = 1;
    for (int row = 0; row < buttons.length; row++) {
        for (int col = 0; col < buttons.length; col++) {
            if (row == 3 && col == 3) {
                buttons[row][col] = button0;
                grid.add(buttons[row][col]);
                buttons[row][col].setBackground(Color.WHITE);
                buttons[row][col].setName("button0");
                winPattern[row][col] = button0;
            } else {
                buttons[row][col] = new JButton(i + "");
                grid.add(buttons[row][col]);
                buttons[row][col].addActionListener(this);
                buttons[row][col].setBackground(Color.RED);
                buttons[row][col].setName("button" + i);
                buttons[row][col].setFont(new Font("Street Cred", Font.PLAIN, 40));
                winPattern[row][col] = buttons[row][col];
                i++;
            }
        }
    }

    do {
        shuffle();
    } while (!isSolvable());
    try {
        setIconImage(ImageIO.read(new File("C:\\Users\\Allan\\Documents\\Nackademin\\OOP\\test2\\icon.png")));
    } catch (IOException e) {
        e.printStackTrace();
    }
    movesLabel.setText("<html>MOVES<br><html>" + "----- " + movesCounter + " -----");
    setTitle("PUZZLE GAME");
    setCursor(new Cursor(Cursor.HAND_CURSOR));
    setResizable(false);
    setLocation(500, 200);
    setSize(600, 600);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public boolean isSolvable() {
    int inv_counter = 0;
    int[] values = new int[16];
    // Lägger alla komponenters nummer i en int array
    for (int i = 0; i < grid.getComponents().length; i++) {
        if (grid.getComponents()[i] == button0) {
            values[i] = 0;
        } else {
            JButton temp = (JButton) grid.getComponents()[i];
            values[i] = Integer.parseInt(temp.getText());
        }

    }

    for (int i = 0; i < values.length - 1; i++) {
        for (int j = i + 1; j < values.length; j++) {
            if (values[i] < values[j]) {
                inv_counter++;
            }
        }
    }

    return inv_counter % 2 == 0;
}


public boolean isSwappable(JButton button) {

    // för att hitta platsen på knappen man trycker och även den blanka platsen
    for (int row = 0; row < buttons.length; row++) {
        for (int col = 0; col < buttons.length; col++) {
            if (buttons[row][col] == button) {
                sourceRow = row;
                sourceCol = col;
            } else if (buttons[row][col] == button0) {
                blankRow = row;
                blankCol = col;
            }
        }
    }
    sourceIndex = (sourceRow * 4) + sourceCol;
    emptyIndex = (blankRow * 4) + blankCol;
    // om den är till höger
    if (sourceCol != 3 && sourceRow == blankRow && buttons[sourceRow][sourceCol + 1] == button0) {
        return true;
    }
    // om den är till vänster
    else if (sourceCol != 0 && sourceRow == blankRow && buttons[blankRow][sourceCol - 1] == button0) {
        return true;
    }
    //om den är nedanför
    else if (sourceRow != 0 && sourceCol == blankCol && buttons[sourceRow - 1][sourceCol] == button0) {
        return true;
    }
    //om den är ovanför
    else if (sourceRow != 3 && sourceCol == blankCol && buttons[sourceRow + 1][sourceCol] == button0) {
        return true;
    }
    return false;
}

public void swap(JButton source) {
    JButton tempButton = buttons[sourceRow][sourceCol];
    buttons[sourceRow][sourceCol] = buttons[blankRow][blankCol];
    buttons[blankRow][blankCol] = tempButton;
    grid.remove(button0);
    grid.remove(source);

    if (emptyIndex < sourceIndex) {
        grid.add(source, emptyIndex);
        grid.add(button0, sourceIndex);
    } else if (emptyIndex > sourceIndex) {
        grid.add(button0, sourceIndex);
        grid.add(source, emptyIndex);
    }
    revalidate();
    repaint();
}

public void shuffle() {
    Random random = new Random();

    //randomize positions for 2D array buttons
    for (int row = 0; row < buttons.length; row++) {
        for (int col = 0; col < buttons.length; col++) {
            int randomNumber = random.nextInt(16);
            int randomRow = randomNumber / 4;
            int randomCol = randomNumber % 4;
            JButton temp = buttons[row][col];
            buttons[row][col] = buttons[randomRow][randomCol];
            buttons[randomRow][randomCol] = temp;
        }
    }
    //remove all components from panel
    grid.removeAll();

    // add components with randomized position to panel
    for (int row = 0; row < buttons.length; row++) {
        for (int col = 0; col < buttons.length; col++) {
            grid.add(buttons[row][col]);
        }
    }
    revalidate();
    repaint();
}

public boolean isSolved() {
    int counter = 0;
    for (int row = 0; row < buttons.length; row++) {
        for (int col = 0; col < buttons.length; col++) {
            if (winPattern[row][col].getText().equals(buttons[row][col].getText())) {
                counter++;
            }
        }
    }
    if (counter == 16) {
        return true;
    } else {
        return false;
    }
}

public void moves() {
    movesCounter++;
    movesLabel.setText("<html>MOVES<br><html>" + "----- " + movesCounter + " -----");
}

public void timerCountDown() {
}

public void reset() {
    movesCounter = 0;
    movesLabel.setText("<html>MOVES<br><html>" + "----- " + movesCounter + " -----");
}

public void newGame() {
    do {
        shuffle();
        reset();
    } while (!isSolvable());
}

@Override
public void actionPerformed(ActionEvent e) {
    JButton source = (JButton) e.getSource();

    if (source == newGameButton) {
        newGame();
    } else if (isSwappable(source)) {
        swap(source);
        moves();
    }

    if (isSolved()) {
        JOptionPane.showMessageDialog(null, "YOU BEAT THE GAME!\nMoves: " + movesCounter + "\nTime: ");
        newGame();
    }
}

}

java swing timer jlabel countdown
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.