Java教程

用JavaSwing实现五子棋小游戏

本文主要是介绍用JavaSwing实现五子棋小游戏,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
  1 package FiveChess;
  2 import java.awt.Color;
  3 import java.awt.Dimension;
  4 import java.awt.Font;
  5 import java.awt.Graphics;
  6 import java.awt.event.KeyAdapter;
  7 import java.awt.event.KeyEvent;
  8 import java.awt.event.KeyListener;
  9 import java.awt.event.MouseAdapter;
 10 import java.awt.event.MouseEvent;
 11 import java.awt.image.BufferedImage;
 12 import java.io.File;
 13 import java.io.IOException;
 14 
 15 import javax.imageio.ImageIO;
 16 import javax.swing.*;
 17 
 18 /**
 19  * 
 20  * @author WENRUI
 21  *
 22  */
 23 public class Game {
 24     
 25     //the three elements of board
 26     BufferedImage black;
 27     BufferedImage white;
 28     BufferedImage table;
 29     
 30     //the size of elements
 31      final int BOARD_WIDTH = 535;
 32      final int BOARD_HEIGHT = 536;
 33      final int PIECE_SIZE = 15;
 34      final int PIECE_RATE = BOARD_WIDTH/PIECE_SIZE;
 35      final int X_OFFSET = 5;
 36      final int Y_OFFSET = 6;
 37 
 38      
 39      //kind、order and state
 40      int [][] category = new int[PIECE_SIZE][PIECE_SIZE];//0:null,1:while,2:black
 41      int order = 1;//1:white piece to be down,2:black piece to be down
 42      String state = "RUNNING";
 43      
 44      //painting
 45      public class ChessBoard extends JPanel{
 46          @Override
 47          public void paint(Graphics g) {
 48              g.drawImage(table,0,0,null);
 49              for(int i = 0;i < PIECE_SIZE;i++)
 50                  for(int j = 0;j < PIECE_SIZE;j++) {
 51                      
 52                      if(category[i][j] == 1) {
 53                          g.drawImage(white,i*PIECE_RATE+X_OFFSET,j*PIECE_RATE+Y_OFFSET,null);
 54                      }
 55                      else if(category[i][j] == 2) {
 56                          g.drawImage(black,i*PIECE_RATE+X_OFFSET,j*PIECE_RATE+Y_OFFSET,null);
 57                      }
 58                  }
 59              
 60              if(state != "RUNNING") {
 61                  
 62                  g.setColor(new Color(0xffffff));
 63                  g.setFont(new Font(Font.SERIF,Font.BOLD,30));
 64                  g.drawString(state, 200, 200);
 65                  g.drawString("q:退出  r:再来一局",200,350);
 66              }
 67          }
 68          
 69      }
 70      
 71      //Swing
 72      JFrame gameFrame = new JFrame("the five chess");
 73      ChessBoard chessBoard = new ChessBoard(); 
 74      
 75      //judging
 76      public boolean isOver(int a,int b) {
 77          
 78          int i,j;
 79          //horizontal
 80          for(i = a-4, j = b; i <= a; i++) {
 81              if(i > 0 && j < PIECE_SIZE && category[i][j] == category[i+1][j] 
 82                      && category[i][j] == category[i+2][j] 
 83                      && category[i][j] == category[i+3][j]
 84                      && category[i][j] == category[i+4][j])
 85                  return true;
 86          }
 87              
 88              
 89          //vertical
 90          for(j = b-4, i = a; j <= b; j++) {
 91              if(j > 0 && i < PIECE_SIZE && category[i][j] == category[i][j+1]
 92                      &&category[i][j] == category[i][j+2]
 93                      &&category[i][j] == category[i][j+3]
 94                      &&category[i][j] == category[i][j+4])
 95                  return true;
 96          }
 97          
 98          //upper left
 99          for(i = a-4, j = b-4; i <= a&&j <= b; i++,j++) {
100              if(i > 0 && i < PIECE_SIZE && j >0 &&j< PIECE_SIZE &&category[i][j] == category[i+1][j+1]
101                      &&category[i][j] == category[i+2][j+2]
102                      &&category[i][j] == category[i+3][j+3]
103                      &&category[i][j] == category[i+4][j+4])
104                  return true;
105          }
106          
107          //upper right
108          for(i = a-4, b = b+4; i <= a&& j >= b; i++,j--) {
109              if(i > 0 && i < PIECE_SIZE && j >0 &&j< PIECE_SIZE &&category[i][j] == category[i+1][j-1]
110                      &&category[i][j] == category[i+2][j-2]
111                      &&category[i][j] == category[i+3][j-3]
112                      &&category[i][j] == category[i+4][j-4])
113                 return true;
114          }
115          
116          return false;
117      }
118      
119     public void init() throws IOException {
120         table = ImageIO.read(new File("src//image//board.jpg"));
121         white = ImageIO.read(new File("src//image//white.gif"));
122         black = ImageIO.read(new File("src//image//black.gif"));
123         //the ImageIo must be set in functions,in order to throw IOException
124         
125         chessBoard.addMouseListener(new MouseAdapter() {
126                 @Override
127                 public void mouseClicked(MouseEvent e) {
128                     int xPos = (e.getX()-X_OFFSET)/PIECE_RATE;
129                     int yPos = (e.getY()-Y_OFFSET)/PIECE_RATE;
130                     if(order == 1){
131                         category[xPos][yPos] = 1;
132                         order = 2;
133                     }
134                     else if(order == 2) {
135                          category[xPos][yPos] = 2;
136                          order = 1;
137                     }
138                     if(isOver(xPos,yPos)) {
139                         if(order == 1)
140                             state = "BLACKWIN";
141                         else if(order == 2)
142                             state = "WHITEWHI";
143                     }
144                     chessBoard.repaint();
145                     
146                     
147                 }});
148         
149         
150         chessBoard.addKeyListener(new KeyAdapter() {
151             @Override
152             public void keyPressed(KeyEvent e) {
153                 
154                 int key = e.getKeyCode();
155                 if( state != "RUNNING")
156                 {
157                     if(key == KeyEvent.VK_Q ) {
158                         System.exit(0);
159                     }
160                         
161                     else if(key == KeyEvent.VK_R) {
162                         for(int i = 0;i < PIECE_SIZE; i++)
163                             for(int j =0; j < PIECE_SIZE; j++) 
164                                 category[i][j] = 0;
165                                 
166                         state = "RUNNING";
167                         chessBoard.repaint();
168                             }
169                     }
170                 }
171         });
172         chessBoard.setFocusable(true);//不知为啥要加这一句,但好像加了KeyListener就有效了
173         
174         
175         
176         
177         chessBoard.setPreferredSize(new Dimension(BOARD_WIDTH,BOARD_HEIGHT));
178         gameFrame.add(chessBoard);
179         gameFrame.pack();
180         gameFrame.setVisible(true);
181     }
182     
183 
184     public static void main(String args[]) throws IOException {
185         new Game().init();
186     }
187 }

大二上学期刚学完JavaSwing时写的,试了几遍没太大问题,也许有不成熟和出错的地方,还请园子大佬们包涵和赐教

以下是我的图片资源地址

https://files.cnblogs.com/files/blogs/718940/image.zip

这篇关于用JavaSwing实现五子棋小游戏的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!