java GUI实现五子棋游戏

2020-02-26 10:00:59刘景俊

设置棋子

// An inner class for a cell
 public class Cell extends JPanel implements MouseListener {

 // Token used for this cell
 private char token = ' ';

 public Cell() {
  setBorder(new LineBorder(Color.black, 1)); // Set cell's border
  addMouseListener(this); // Register listener
 }

 // The getter method for token
 public char getToken() {
  return token;
 }

 // The setter method for token
 public void setToken(char c) {
  token = c;
  repaint();
 }

导入图片

// Paint the cell
 @Override
 public void paintComponent(Graphics g) {
  if (token == 'X') {
  ImageIcon icon = new ImageIcon("C:UsersLenovoDesktopBlack.png");
  Image image = icon.getImage();
  g.drawImage(image,0,0,35,35,this); 
  }else if (token=='O'){
  ImageIcon icon = new ImageIcon("C:UsersLenovoDesktopWhite.png");
  Image image = icon.getImage();
  g.drawImage(image,0,0,35,35,this);  
  }
  super.paintComponents(g);
 }

游戏结束的锁定与弹窗

// Handle mouse click on a cell
 @Override
 public void mouseClicked(MouseEvent e) {
  if (ISOVER) return; // if game is over, any issue should be forbidden
  int response=-1;
  if (token == ' ') // If cell is not occupied
  {
  if (whoseTurn == 'X') // If it is the X player's turn
  {
   setToken('X'); // Set token in the cell
   whoseTurn = 'O'; // Change the turn
   jlblStatus.setText("The White's Turn"); // Display status
   if (isWon('X')) {
   jlblStatus.setText("The Black Won! The Game Is Over!");
   response = JOptionPane.showConfirmDialog(null, "The Black Won! The Game Is Over!n"
    +"Do you want to quit?","提示",JOptionPane.YES_NO_OPTION);
   
   ISOVER = true;
   if (response == 0) System.exit(0); // choose "Yes" than quit;
   } 
  } else if (whoseTurn == 'O') // If it is the O player's turn
  {
   setToken('O'); // Set token in the cell
   whoseTurn = 'X'; // Change the turn
   jlblStatus.setText("The Black's Turn"); // Display status
   if (isWon('O')) {
   jlblStatus.setText("The White Won! The Game Is Over!");
   response = JOptionPane.showConfirmDialog(null, "The White Won! The Game Is Over!n"
    +"Do you want to quit?","提示",JOptionPane.YES_NO_OPTION);
   ISOVER = true;
   
   if (response == 0) System.exit(0); // choose "Yes" than quit;
   } 
  }
  if (isFull()) {
   jlblStatus.setText("Plain Game! The Game Is Over!");
   response = JOptionPane.showConfirmDialog(null, "Plain Game! The Game Is Over!n"
    +"Do you want to quit?","提示",JOptionPane.YES_NO_OPTION);
   ISOVER = true;
   if (response == 0) System.exit(0); // choose "Yes" than quit;
  }
  }
  
 }

其他棋子信息

@Override
 public void mousePressed(MouseEvent e) {
  // TODO: implement this java.awt.event.MouseListener method;
 }

 @Override
 public void mouseReleased(MouseEvent e) {
  // TODO: implement this java.awt.event.MouseListener method;
 }

 @Override
 public void mouseEntered(MouseEvent e) {
  // TODO: implement this java.awt.event.MouseListener method;
 }

 @Override
 public void mouseExited(MouseEvent e) {
  // TODO: implement this java.awt.event.MouseListener method;
 }
 }
}