扫雷GUI计时器

时间:2016-05-09 05:14:15

标签: java timer

我正在尝试制作一个能够跟踪我的扫雷游戏时间的实际计时器。但是,我不知道如何编写我的actionPerformed方法来设置计时器启动和计时器结束。 我希望计时器在玩家点击雷区中的按钮后启动,我希望计时器在玩家获胜或失败时结束。 我确实做了一个gameEnd方法来确定玩家何时赢或输,但就像我说的那样,我只是不知道如何编写actionPerformed方法。

import java.util.Scanner;
import java.util.Random;
//import java.util.*;
import java.awt.*;
import java.awt.event.*;
//import java.io.*;
//import java.io.FileWriter;
//import java.io.PrintWriter;
import javax.swing.*;
import javax.swing.Timer;
import javax.swing.border.BevelBorder;
import javax.swing.border.EtchedBorder;

public class Minesweeper extends JFrame implements ActionListener {
  public static Scanner myScanner = new Scanner (System.in);
  public static Random random = new Random ();

  JPanel pan1 = new JPanel ();
  JPanel pan2 = new JPanel ();
  JPanel pan3 = new JPanel ();
  JPanel pan1_1 = new JPanel ();
  JPanel pan1_2 = new JPanel ();
  JPanel pan1_3 = new JPanel ();
  JPanel pan1_3_D = new JPanel ();

  JButton button [][];
  JButton Save = new JButton ("Save");
  JButton Load = new JButton ("Load");
  JButton New = new JButton ("New");
  JButton One = new JButton ("1");
  JButton Two = new JButton ("2");
  JButton Three = new JButton ("3");
  JButton R = new JButton ("R");

  JLabel instruction = new JLabel ("Instruction Panel");//changeable
  JLabel timeLabel = new JLabel ();
  JLabel score = new JLabel ("Score");//it is only temporary

  static Timer timer;

  public Minesweeper (int row, int column) {
    button = new JButton [row][column];

    GridLayout gl_pan1 = new GridLayout (1, 3);
    GridLayout gl_pan2 = new GridLayout (row, column);
    GridLayout gl = new GridLayout (2, 1);//gl is for the 3 sub-panels in pan1
    GridLayout gl_pan1_3_D = new GridLayout (1, 4);

    pan1.setLayout (gl_pan1);
    pan2.setLayout (gl_pan2);
    pan1_1.setLayout (gl);
    pan1_2.setLayout (gl);
    pan1_3.setLayout (gl);
    pan1_3_D.setLayout (gl_pan1_3_D);

    setTitle ("Minesweeper");

    JPanel frameBorder = (JPanel)getContentPane ();
    frameBorder.setLayout (new BoxLayout (getContentPane (), BoxLayout.Y_AXIS));
    frameBorder.setBorder (new BevelBorder (BevelBorder.LOWERED));

    pan1.setBorder (new EtchedBorder (EtchedBorder.LOWERED));
    pan2.setBorder (new EtchedBorder (EtchedBorder.LOWERED));
    pan3.setBorder (new EtchedBorder (EtchedBorder.LOWERED));

    Save.setMinimumSize (new Dimension (25, 50));
    Save.setPreferredSize (new Dimension (25, 50));
    Save.setMaximumSize (new Dimension (25, 50));
    Load.setMinimumSize (new Dimension (25, 50));
    Load.setPreferredSize (new Dimension (25, 50));
    Load.setMaximumSize (new Dimension (25, 50));
    New.setMinimumSize (new Dimension (25, 50));
    New.setPreferredSize (new Dimension (25, 50));
    New.setMaximumSize (new Dimension (25, 50));

    for (int r = 0; r < row; r++) {
      for (int c = 0; c < column; c++) {
        button [r][c] = new JButton ();
        button [r][c].setMinimumSize (new Dimension (20, 40));
        button [r][c].setPreferredSize (new Dimension (20, 40));
        button [r][c].setMaximumSize (new Dimension (20, 40));
        pan2.add (button [r][c]);
        button [r][c].addActionListener (this);
      }
    }

    pan1_1.add (Save);
    pan1_1.add (Load);
    pan1_2.add (timeLabel);
    pan1_2.add (score);
    pan1_3.add (New);
    pan1_3_D.add (One);
    pan1_3_D.add (Two);
    pan1_3_D.add (Three);
    pan1_3_D.add (R);
    pan1_3.add (pan1_3_D);

    Save.addActionListener (this);
    Load.addActionListener (this);
    New.addActionListener (this);
    One.addActionListener (this);
    Two.addActionListener (this);
    Three.addActionListener (this);
    R.addActionListener (this);

    pan1.add (pan1_1);
    pan1.add (pan1_2);
    pan1.add (pan1_3);
    pan3.add (instruction);

    frameBorder.add (pan1);
    frameBorder.add (pan2);
    frameBorder.add (pan3);

    setContentPane (frameBorder);
    pack ();
    setVisible (true);
  }

  private void setTimerText (String sTime) {
    timeLabel.setText (sTime);
  }

  public void actionPerformed (ActionEvent e) {

  }

public static void main (String args []) throws Exception {
    int row = 10;
    int column = 10;

    new Minesweeper (row, column);

    int current_row = 5;
    int current_col = 5;
    int percentage = 10; //this is subject to change
    int number_of_mines = (int)(row*column*percentage*0.01);
    int mine_position [] = new int [number_of_mines];

    boolean revealed [][] = new boolean [row][column]; //whether the button is revealed or not
    boolean mines [][] = new boolean [row][column]; //whether there is a mine or not
    int revealed_number [][] = new int [row][column]; //the number that should appear on the revealed button

    int counter = 0; //this is for the gameEnd method
    boolean end = false; //whether the game is ended or not
}

public static void gameEnd (int row, int column, boolean revealed [][], boolean mines [][], int number_of_mines, int counter, boolean end) {
    for (int r = 0; r < row; r++) {
      for (int c = 0; c < column; c++) {
        if (mines [r][c] == false && revealed [r][c] == true) {
          counter++;
        }

        else if (mines [r][c] == true && revealed [r][c] == true) {
          System.out.println ("Sorry, you lost because you clicked on a mine.");
          end = true;
          break;
        }
      }
    }

    if (counter == (row*column-number_of_mines)) {
      System.out.println ("Congratulations! You won the game!");
      end = true;
    }
  }
}

1 个答案:

答案 0 :(得分:0)

timer.scheduleAtFixedRate(task, 1000, 1000);

其中task是重写java.util.TimerTask的类的实例。

在run()方法(TimerTask中的摘要)中,您将更新计时器标签(密切关注multithreading!),并在endGame中调用

timer.cancel();