五子棋

一、实践目标:

1.掌握JavaGUI界面设计

2.掌握鼠标响应事件(MouseMotionListener)

二、实践内容:

设计一个简单的五子棋程序,能够实现五子棋下棋过程。如下图所示

五子棋运行界面

1.五子棋棋盘类

  1. package cn.edu.ouc.fiveChess;
  2. import java.awt.Color;
  3. import java.awt.Cursor;
  4. import java.awt.Dimension;
  5. import java.awt.Graphics;
  6. import java.awt.Graphics2D;
  7. import java.awt.Image;
  8. import java.awt.RadialGradientPaint;
  9. import java.awt.RenderingHints;
  10. import java.awt.Toolkit;
  11. import java.awt.event.MouseEvent;
  12. import java.awt.event.MouseListener;
  13. import java.awt.event.MouseMotionListener;
  14. import java.awt.geom.Ellipse2D;
  15. import javax.swing.*;
  16. /**
  17. * 五子棋--棋盘类
  18. */
  19. public class ChessBoardextends JPanelimplements MouseListener {
  20. public staticfinalint MARGIN=30;//边距
  21. public staticfinalint GRID_SPAN=35;//网格间距
  22. public staticfinalint ROWS=15;//棋盘行数
  23. public staticfinalint COLS=15;//棋盘列数
  24. Point[] chessList=new Point[(ROWS+1)*(COLS+1)];//初始每个数组元素为null
  25. boolean isBlack=true;//默认开始是黑棋先
  26. boolean gameOver=false;//游戏是否结束
  27. int chessCount;//当前棋盘棋子的个数
  28. int xIndex,yIndex;//当前刚下棋子的索引
  29. Image img;
  30. Image shadows;
  31. Color colortemp;
  32. public ChessBoard(){
  33. // setBackground(Color.blue);//设置背景色为橘黄色
  34. img=Toolkit.getDefaultToolkit().getImage("board.jpg");
  35. shadows=Toolkit.getDefaultToolkit().getImage("shadows.jpg");
  36. addMouseListener(this);
  37. addMouseMotionListener(new MouseMotionListener(){
  38. public void mouseDragged(MouseEvent e){
  39. }
  40. public void mouseMoved(MouseEvent e){
  41. int x1=(e.getX()-MARGIN+GRID_SPAN/2)/GRID_SPAN;
  42. //将鼠标点击的坐标位置转成网格索引
  43. int y1=(e.getY()-MARGIN+GRID_SPAN/2)/GRID_SPAN;
  44. //游戏已经结束不能下
  45. //落在棋盘外不能下
  46. //x,y位置已经有棋子存在,不能下
  47. if(x1<0||x1>ROWS||y1<0||y1>COLS||gameOver||findChess(x1,y1))
  48. setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
  49. //设置成默认状态
  50. else setCursor(new Cursor(Cursor.HAND_CURSOR));
  51. }
  52. });
  53. }
  54. //绘制
  55. public void paintComponent(Graphics g){
  56. super.paintComponent(g);//画棋盘
  57. int imgWidth= img.getWidth(this);
  58. int imgHeight=img.getHeight(this);//获得图片的宽度与高度
  59. int FWidth=getWidth();
  60. int FHeight=getHeight();//获得窗口的宽度与高度
  61. int x=(FWidth-imgWidth)/2;
  62. int y=(FHeight-imgHeight)/2;
  63. g.drawImage(img, x, y, null);
  64. for(int i=0;i<=ROWS;i++){//画横线
  65. g.drawLine(MARGIN, MARGIN+i*GRID_SPAN, MARGIN+COLS*GRID_SPAN, MARGIN+i*GRID_SPAN);
  66. }
  67. for(int i=0;i<=COLS;i++){//画竖线
  68. g.drawLine(MARGIN+i*GRID_SPAN, MARGIN, MARGIN+i*GRID_SPAN, MARGIN+ROWS*GRID_SPAN);
  69. }
  70. //画棋子
  71. for(int i=0;i<chessCount;i++){
  72. //网格交叉点x,y坐标
  73. int xPos=chessList[i].getX()*GRID_SPAN+MARGIN;
  74. int yPos=chessList[i].getY()*GRID_SPAN+MARGIN;
  75. g.setColor(chessList[i].getColor());//设置颜色
  76. // g.fillOval(xPos-Point.DIAMETER/2, yPos-Point.DIAMETER/2,
  77. //Point.DIAMETER, Point.DIAMETER);
  78. //g.drawImage(shadows, xPos-Point.DIAMETER/2, yPos-Point.DIAMETER/2, Point.DIAMETER, Point.DIAMETER, null);
  79. colortemp=chessList[i].getColor();
  80. if(colortemp==Color.black){
  81. RadialGradientPaint paint = new RadialGradientPaint(xPos-Point.DIAMETER/2+25, yPos-Point.DIAMETER/2+10,20,newfloat[]{0f, 1f}
  82. , new Color[]{Color.WHITE, Color.BLACK});
  83. ((Graphics2D) g).setPaint(paint);
  84. ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  85. ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_DEFAULT);
  86. }
  87. else if(colortemp==Color.white){
  88. RadialGradientPaint paint = new RadialGradientPaint(xPos-Point.DIAMETER/2+25, yPos-Point.DIAMETER/2+10,70,newfloat[]{0f, 1f}
  89. , new Color[]{Color.WHITE, Color.BLACK});
  90. ((Graphics2D) g).setPaint(paint);
  91. ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  92. ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_DEFAULT);
  93. }
  94. Ellipse2D e = new Ellipse2D.Float(xPos-Point.DIAMETER/2, yPos-Point.DIAMETER/2,34,35);
  95. ((Graphics2D) g).fill(e);
  96. //标记最后一个棋子的红矩形框
  97. if(i==chessCount-1){//如果是最后一个棋子
  98. g.setColor(Color.red);
  99. g.drawRect(xPos-Point.DIAMETER/2, yPos-Point.DIAMETER/2,
  100. 34, 35);
  101. }
  102. }
  103. }
  104. public void mousePressed(MouseEvent e){//鼠标在组件上按下时调用
  105. //游戏结束时,不再能下
  106. if(gameOver) return;
  107. String colorName=isBlack?"黑棋":"白棋";
  108. //将鼠标点击的坐标位置转换成网格索引
  109. xIndex=(e.getX()-MARGIN+GRID_SPAN/2)/GRID_SPAN;
  110. yIndex=(e.getY()-MARGIN+GRID_SPAN/2)/GRID_SPAN;
  111. //落在棋盘外不能下
  112. if(xIndex<0||xIndex>ROWS||yIndex<0||yIndex>COLS)
  113. return;
  114. //如果x,y位置已经有棋子存在,不能下
  115. if(findChess(xIndex,yIndex))return;
  116. //可以进行时的处理
  117. Point ch=new Point(xIndex,yIndex,isBlack?Color.black:Color.white);
  118. chessList[chessCount++]=ch;
  119. repaint();//通知系统重新绘制
  120. //如果胜出则给出提示信息,不能继续下棋
  121. if(isWin()){
  122. String msg=String.format("恭喜,%s赢了!", colorName);
  123. JOptionPane.showMessageDialog(this, msg);
  124. gameOver=true;
  125. }
  126. isBlack=!isBlack;
  127. }
  128. //覆盖mouseListener的方法
  129. public void mouseClicked(MouseEvent e){
  130. //鼠标按键在组件上单击时调用
  131. }
  132. public void mouseEntered(MouseEvent e){
  133. //鼠标进入到组件上时调用
  134. }
  135. public void mouseExited(MouseEvent e){
  136. //鼠标离开组件时调用
  137. }
  138. public void mouseReleased(MouseEvent e){
  139. //鼠标按钮在组件上释放时调用
  140. }
  141. //在棋子数组中查找是否有索引为x,y的棋子存在
  142. private boolean findChess(int x,int y){
  143. for(Point c:chessList){
  144. if(c!=null&&c.getX()==x&&c.getY()==y)
  145. return true;
  146. }
  147. return false;
  148. }
  149. private boolean isWin(){
  150. int continueCount=1;//连续棋子的个数
  151. //横向向西寻找
  152. for(int x=xIndex-1;x>=0;x--){
  153. Color c=isBlack?Color.black:Color.white;
  154. if(getChess(x,yIndex,c)!=null){
  155. continueCount++;
  156. }else
  157. break;
  158. }
  159. //横向向东寻找
  160. for(int x=xIndex+1;x<=COLS;x++){
  161. Color c=isBlack?Color.black:Color.white;
  162. if(getChess(x,yIndex,c)!=null){
  163. continueCount++;
  164. }else
  165. break;
  166. }
  167. if(continueCount>=5){
  168. return true;
  169. }else
  170. continueCount=1;
  171. //继续另一种搜索纵向
  172. //向上搜索
  173. for(int y=yIndex-1;y>=0;y--){
  174. Color c=isBlack?Color.black:Color.white;
  175. if(getChess(xIndex,y,c)!=null){
  176. continueCount++;
  177. }else
  178. break;
  179. }
  180. //纵向向下寻找
  181. for(int y=yIndex+1;y<=ROWS;y++){
  182. Color c=isBlack?Color.black:Color.white;
  183. if(getChess(xIndex,y,c)!=null)
  184. continueCount++;
  185. else
  186. break;
  187. }
  188. if(continueCount>=5)
  189. return true;
  190. else
  191. continueCount=1;
  192. //继续另一种情况的搜索:斜向
  193. //东北寻找
  194. for(int x=xIndex+1,y=yIndex-1;y>=0&&x<=COLS;x++,y--){
  195. Color c=isBlack?Color.black:Color.white;
  196. if(getChess(x,y,c)!=null){
  197. continueCount++;
  198. }
  199. else break;
  200. }
  201. //西南寻找
  202. for(int x=xIndex-1,y=yIndex+1;x>=0&&y<=ROWS;x--,y++){
  203. Color c=isBlack?Color.black:Color.white;
  204. if(getChess(x,y,c)!=null){
  205. continueCount++;
  206. }
  207. else break;
  208. }
  209. if(continueCount>=5)
  210. return true;
  211. else continueCount=1;
  212. //继续另一种情况的搜索:斜向
  213. //西北寻找
  214. for(int x=xIndex-1,y=yIndex-1;x>=0&&y>=0;x--,y--){
  215. Color c=isBlack?Color.black:Color.white;
  216. if(getChess(x,y,c)!=null)
  217. continueCount++;
  218. else break;
  219. }
  220. //东南寻找
  221. for(int x=xIndex+1,y=yIndex+1;x<=COLS&&y<=ROWS;x++,y++){
  222. Color c=isBlack?Color.black:Color.white;
  223. if(getChess(x,y,c)!=null)
  224. continueCount++;
  225. else break;
  226. }
  227. if(continueCount>=5)
  228. return true;
  229. else continueCount=1;
  230. return false;
  231. }
  232. private Point getChess(int xIndex,int yIndex,Color color){
  233. for(Point p:chessList){
  234. if(p!=null&&p.getX()==xIndex&&p.getY()==yIndex
  235. &&p.getColor()==color)
  236. return p;
  237. }
  238. return null;
  239. }
  240. public void restartGame(){
  241. //清除棋子
  242. for(int i=0;i<chessList.length;i++){
  243. chessList[i]=null;
  244. }
  245. //恢复游戏相关的变量值
  246. isBlack=true;
  247. gameOver=false; //游戏是否结束
  248. chessCount =0; //当前棋盘棋子个数
  249. repaint();
  250. }
  251. //悔棋
  252. public void goback(){
  253. if(chessCount==0)
  254. return ;
  255. chessList[chessCount-1]=null;
  256. chessCount--;
  257. if(chessCount>0){
  258. xIndex=chessList[chessCount-1].getX();
  259. yIndex=chessList[chessCount-1].getY();
  260. }
  261. isBlack=!isBlack;
  262. repaint();
  263. }
  264. //矩形Dimension
  265. public Dimension getPreferredSize(){
  266. return new Dimension(MARGIN*2+GRID_SPAN*COLS,MARGIN*2
  267. +GRID_SPAN*ROWS);
  268. }
  269. }
package cn.edu.ouc.fiveChess;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RadialGradientPaint;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Ellipse2D;

import javax.swing.*;
/**
 * 五子棋--棋盘类 
 */

public class ChessBoard extends JPanel implements MouseListener {
   public static final int MARGIN=30;//边距
   public static final int GRID_SPAN=35;//网格间距
   public static final int ROWS=15;//棋盘行数
   public static final int COLS=15;//棋盘列数
   
   Point[] chessList=new Point[(ROWS+1)*(COLS+1)];//初始每个数组元素为null
   boolean isBlack=true;//默认开始是黑棋先
   boolean gameOver=false;//游戏是否结束
   int chessCount;//当前棋盘棋子的个数
   int xIndex,yIndex;//当前刚下棋子的索引
   
   Image img;
   Image shadows;
   Color colortemp;
   public ChessBoard(){
	  
	  // setBackground(Color.blue);//设置背景色为橘黄色
	   img=Toolkit.getDefaultToolkit().getImage("board.jpg");
	   shadows=Toolkit.getDefaultToolkit().getImage("shadows.jpg");
	   addMouseListener(this);
	   addMouseMotionListener(new MouseMotionListener(){
		   public void mouseDragged(MouseEvent e){
			   
		   }
		   
		   public void mouseMoved(MouseEvent e){
		     int x1=(e.getX()-MARGIN+GRID_SPAN/2)/GRID_SPAN;
		     //将鼠标点击的坐标位置转成网格索引
		     int y1=(e.getY()-MARGIN+GRID_SPAN/2)/GRID_SPAN;
		     //游戏已经结束不能下
		     //落在棋盘外不能下
		     //x,y位置已经有棋子存在,不能下
		     if(x1<0||x1>ROWS||y1<0||y1>COLS||gameOver||findChess(x1,y1))
		    	 setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
		     //设置成默认状态
		     else setCursor(new Cursor(Cursor.HAND_CURSOR));
		     
		   }
	   });
   } 
   
  

//绘制
   public void paintComponent(Graphics g){
	 
	   super.paintComponent(g);//画棋盘
	 
	   int imgWidth= img.getWidth(this);
	   int imgHeight=img.getHeight(this);//获得图片的宽度与高度
	   int FWidth=getWidth();
	   int FHeight=getHeight();//获得窗口的宽度与高度
	   int x=(FWidth-imgWidth)/2;
	   int y=(FHeight-imgHeight)/2;
	   g.drawImage(img, x, y, null);
	
	   
	   for(int i=0;i<=ROWS;i++){//画横线
		   g.drawLine(MARGIN, MARGIN+i*GRID_SPAN, MARGIN+COLS*GRID_SPAN, MARGIN+i*GRID_SPAN);
	   }
	   for(int i=0;i<=COLS;i++){//画竖线
		   g.drawLine(MARGIN+i*GRID_SPAN, MARGIN, MARGIN+i*GRID_SPAN, MARGIN+ROWS*GRID_SPAN);
		   
	   }
	   
	   //画棋子
	   for(int i=0;i<chessCount;i++){
		   //网格交叉点x,y坐标
		   int xPos=chessList[i].getX()*GRID_SPAN+MARGIN;
		   int yPos=chessList[i].getY()*GRID_SPAN+MARGIN;
		   g.setColor(chessList[i].getColor());//设置颜色
		  // g.fillOval(xPos-Point.DIAMETER/2, yPos-Point.DIAMETER/2,
				           //Point.DIAMETER, Point.DIAMETER);
		   //g.drawImage(shadows, xPos-Point.DIAMETER/2, yPos-Point.DIAMETER/2, Point.DIAMETER, Point.DIAMETER, null);
		   colortemp=chessList[i].getColor();
		   if(colortemp==Color.black){
			   RadialGradientPaint paint = new RadialGradientPaint(xPos-Point.DIAMETER/2+25, yPos-Point.DIAMETER/2+10, 20, new float[]{0f, 1f}
               , new Color[]{Color.WHITE, Color.BLACK});
               ((Graphics2D) g).setPaint(paint);
               ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
               ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_DEFAULT);

		   }
		   else if(colortemp==Color.white){
			   RadialGradientPaint paint = new RadialGradientPaint(xPos-Point.DIAMETER/2+25, yPos-Point.DIAMETER/2+10, 70, new float[]{0f, 1f}
               , new Color[]{Color.WHITE, Color.BLACK});
               ((Graphics2D) g).setPaint(paint);
               ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
               ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_DEFAULT);

		   }
		 
		   Ellipse2D e = new Ellipse2D.Float(xPos-Point.DIAMETER/2, yPos-Point.DIAMETER/2, 34, 35);
		   ((Graphics2D) g).fill(e);
	       //标记最后一个棋子的红矩形框
		   
		   if(i==chessCount-1){//如果是最后一个棋子
			   g.setColor(Color.red);
			   g.drawRect(xPos-Point.DIAMETER/2, yPos-Point.DIAMETER/2,
				           34, 35);
		   }
	   }
   }
   
   public void mousePressed(MouseEvent e){//鼠标在组件上按下时调用
	   
	   //游戏结束时,不再能下
	   if(gameOver) return;
	   
	   String colorName=isBlack?"黑棋":"白棋";
	   
	   //将鼠标点击的坐标位置转换成网格索引
	   xIndex=(e.getX()-MARGIN+GRID_SPAN/2)/GRID_SPAN;
	   yIndex=(e.getY()-MARGIN+GRID_SPAN/2)/GRID_SPAN;
	   
	   //落在棋盘外不能下
	   if(xIndex<0||xIndex>ROWS||yIndex<0||yIndex>COLS)
		   return;
	   
	   //如果x,y位置已经有棋子存在,不能下
	   if(findChess(xIndex,yIndex))return;
	   
	   //可以进行时的处理
	   Point ch=new Point(xIndex,yIndex,isBlack?Color.black:Color.white);
	   chessList[chessCount++]=ch;
	    repaint();//通知系统重新绘制
	  
	   
	   //如果胜出则给出提示信息,不能继续下棋
	   
	   if(isWin()){
		   String msg=String.format("恭喜,%s赢了!", colorName);
		   JOptionPane.showMessageDialog(this, msg);
		   gameOver=true;
	   }
	   isBlack=!isBlack;
	 }
   //覆盖mouseListener的方法
   public void mouseClicked(MouseEvent e){
	   //鼠标按键在组件上单击时调用
   }
   
   public void mouseEntered(MouseEvent e){
	   //鼠标进入到组件上时调用
   }
   public void mouseExited(MouseEvent e){
	   //鼠标离开组件时调用
   }
   public void mouseReleased(MouseEvent e){
	   //鼠标按钮在组件上释放时调用
   }
   //在棋子数组中查找是否有索引为x,y的棋子存在
   private boolean findChess(int x,int y){
	   for(Point c:chessList){
		   if(c!=null&&c.getX()==x&&c.getY()==y)
			   return true;
	   }
	   return false;
   }
   
   
   private boolean isWin(){
	   int continueCount=1;//连续棋子的个数
	  
	   //横向向西寻找
	   for(int x=xIndex-1;x>=0;x--){
		   Color c=isBlack?Color.black:Color.white;
		   if(getChess(x,yIndex,c)!=null){
			   continueCount++;
		   }else
			   break;
	   }
      //横向向东寻找
       for(int x=xIndex+1;x<=COLS;x++){
	      Color c=isBlack?Color.black:Color.white;
	      if(getChess(x,yIndex,c)!=null){
		     continueCount++;
	      }else
		     break;
       }
       if(continueCount>=5){
	         return true;
       }else 
	   continueCount=1;
       
       //继续另一种搜索纵向
       //向上搜索
       for(int y=yIndex-1;y>=0;y--){
    	   Color c=isBlack?Color.black:Color.white;
    	   if(getChess(xIndex,y,c)!=null){
    		   continueCount++;
    	   }else
    		   break;
       }
       //纵向向下寻找
       for(int y=yIndex+1;y<=ROWS;y++){
    	   Color c=isBlack?Color.black:Color.white;
    	   if(getChess(xIndex,y,c)!=null)
    	       continueCount++;
           else
    	      break;
       
       }
       if(continueCount>=5)
    	   return true;
       else
    	   continueCount=1;
       
       
       //继续另一种情况的搜索:斜向
       //东北寻找
       for(int x=xIndex+1,y=yIndex-1;y>=0&&x<=COLS;x++,y--){
    	   Color c=isBlack?Color.black:Color.white;
    	   if(getChess(x,y,c)!=null){
    		   continueCount++;
    	   }
    	   else break;
       }
       //西南寻找
       for(int x=xIndex-1,y=yIndex+1;x>=0&&y<=ROWS;x--,y++){
    	   Color c=isBlack?Color.black:Color.white;
    	   if(getChess(x,y,c)!=null){
    		   continueCount++;
    	   }
    	   else break;
       }
       if(continueCount>=5)
    	   return true;
       else continueCount=1;
       
       
       //继续另一种情况的搜索:斜向
       //西北寻找
       for(int x=xIndex-1,y=yIndex-1;x>=0&&y>=0;x--,y--){
    	   Color c=isBlack?Color.black:Color.white;
    	   if(getChess(x,y,c)!=null)
    		   continueCount++;
    	   else break;
       }
       //东南寻找
       for(int x=xIndex+1,y=yIndex+1;x<=COLS&&y<=ROWS;x++,y++){
    	   Color c=isBlack?Color.black:Color.white;
    	   if(getChess(x,y,c)!=null)
    		   continueCount++;
    	   else break;
       }
       if(continueCount>=5)
    	   return true;
       else continueCount=1;
       
       return false;
     }
   
   
   private Point getChess(int xIndex,int yIndex,Color color){
	   for(Point p:chessList){
		   if(p!=null&&p.getX()==xIndex&&p.getY()==yIndex
				   &&p.getColor()==color)
			   return p;
	   }
	   return null;
   }
   
   
   public void restartGame(){
	   //清除棋子
	   for(int i=0;i<chessList.length;i++){
		   chessList[i]=null;
	   }
	   //恢复游戏相关的变量值
	   isBlack=true;
	   gameOver=false; //游戏是否结束
	   chessCount =0; //当前棋盘棋子个数
	   repaint();
   }
   
   //悔棋
   public void goback(){
	   if(chessCount==0)
		   return ;
	   chessList[chessCount-1]=null;
	   chessCount--;
	   if(chessCount>0){
		   xIndex=chessList[chessCount-1].getX();
		   yIndex=chessList[chessCount-1].getY();
	   }
	   isBlack=!isBlack;
	   repaint();
   }
   
   //矩形Dimension

   public Dimension getPreferredSize(){
	   return new Dimension(MARGIN*2+GRID_SPAN*COLS,MARGIN*2
	                        +GRID_SPAN*ROWS);
   }
   
   
   
}


2.棋子类

  1. package cn.edu.ouc.fiveChess;
  2. import java.awt.Color;
  3. /**
  4. * 棋子类
  5. */
  6. public class Point {
  7. private int x;//棋盘中的x索引
  8. private int y;//棋盘中的y索引
  9. private Color color;//颜色
  10. public staticfinalint DIAMETER=30;//直径
  11. public Point(int x,int y,Color color){
  12. this.x=x;
  13. this.y=y;
  14. this.color=color;
  15. }
  16. public int getX(){//拿到棋盘中x的索引
  17. return x;
  18. }
  19. public int getY(){
  20. return y;
  21. }
  22. public Color getColor(){//获得棋子的颜色
  23. return color;
  24. }
  25. }
package cn.edu.ouc.fiveChess;

import java.awt.Color;
/**
 * 棋子类
 */
public class Point {
  private int x;//棋盘中的x索引
  private int y;//棋盘中的y索引
  private Color color;//颜色
  public static final int DIAMETER=30;//直径
  
  public Point(int x,int y,Color color){
	  this.x=x;
	  this.y=y;
	  this.color=color;
  } 
  
  public int getX(){//拿到棋盘中x的索引
	  return x;
  }
  public int getY(){
	  return y;
  }
  public Color getColor(){//获得棋子的颜色
	  return color;
  }
}


3.五子棋主框架类

 
package cn.edu.ouc.fiveChess;
import java.awt.event.*;
import java.awt.*;

import javax.swing.*;
/*
 五子棋主框架類,程序啟動類
 */
public class StartChessJFrame extends JFrame {
  private ChessBoard chessBoard;
  private JPanel toolbar;
  private JButton startButton,backButton,exitButton;
  
  private JMenuBar menuBar;
  private JMenu sysMenu;
  private JMenuItem startMenuItem,exitMenuItem,backMenuItem;
  //重新开始,退出,和悔棋菜单项
  public StartChessJFrame(){
	  setTitle("单机版五子棋");//设置标题
	  chessBoard=new ChessBoard();
	
	  
	  Container contentPane=getContentPane();
	  contentPane.add(chessBoard);
	  chessBoard.setOpaque(true);
	  
	  
	  //创建和添加菜单
	  menuBar =new JMenuBar();//初始化菜单栏
	  sysMenu=new JMenu("系统");//初始化菜单
	  //初始化菜单项
	  startMenuItem=new JMenuItem("重新开始");
	  exitMenuItem =new JMenuItem("退出");
	  backMenuItem =new JMenuItem("悔棋");
	  //将三个菜单项添加到菜单上
	  sysMenu.add(startMenuItem);
	  sysMenu.add(exitMenuItem);
	  sysMenu.add(backMenuItem);
	  //初始化按钮事件监听器内部类
	  MyItemListener lis=new MyItemListener();
	  //将三个菜单注册到事件监听器上
	  this.startMenuItem.addActionListener(lis);
	  backMenuItem.addActionListener(lis);
	  exitMenuItem.addActionListener(lis);
	  menuBar.add(sysMenu);//将系统菜单添加到菜单栏上
	  setJMenuBar(menuBar);//将menuBar设置为菜单栏
	  
	  toolbar=new JPanel();//工具面板实例化
      //三个按钮初始化
	  startButton=new JButton("重新开始");
	  exitButton=new JButton("退出");
	  backButton=new JButton("悔棋");
	  //将工具面板按钮用FlowLayout布局
	  toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));
	  //将三个按钮添加到工具面板
	  toolbar.add(startButton);
	  toolbar.add(exitButton);
	  toolbar.add(backButton);
	  //将三个按钮注册监听事件
	  startButton.addActionListener(lis);
	  exitButton.addActionListener(lis);
	  backButton.addActionListener(lis);
	  //将工具面板布局到界面”南方“也就是下方
	  add(toolbar,BorderLayout.SOUTH);
	  add(chessBoard);//将面板对象添加到窗体上
	  //设置界面关闭事件
	  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	  //setSize(800,800);
	  pack();//自适应大小
	  
  }
  
  private class MyItemListener implements ActionListener{
	  public void actionPerformed(ActionEvent e){
		  Object obj=e.getSource();//获得事件源
		  if(obj==StartChessJFrame.this.startMenuItem||obj==startButton){
			  //重新开始
			  //JFiveFrame.this内部类引用外部类
			  System.out.println("重新开始");
			  chessBoard.restartGame();
		  }
		  else if (obj==exitMenuItem||obj==exitButton)
			  System.exit(0);
		  else if (obj==backMenuItem||obj==backButton){
			  System.out.println("悔棋...");
			  chessBoard.goback();
		  }
	  }
  }
  
  
  
  public static void main(String[] args){
	  StartChessJFrame f=new StartChessJFrame();//创建主框架
	  f.setVisible(true);//显示主框架
	  
  }
}


三、总结

1.菜单的设计与实现?

2.鼠标点击棋盘后,如何绘制棋子?如何为刚下的棋子绘制一个红色框?

3.棋谱是如何一个数据结构?

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值