【Java精品源码栏目提醒】:网学会员为需要Java精品源码的朋友们搜集整理了【精品】双人五子棋的Java源代码 - 大学课件相关资料,希望对各位网友有所帮助!
第一个文件:import javax.swing.import
java.awt.event.import
java.awt./五子棋-主框架类 程序启动类 /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//初始化面板对象 // 创建和添加菜单 menuBar new JMenuBar//初始化菜单栏 sysMenu new JMenu系统//初始化菜单 startMenuItem new JMenuItem重新开始 exitMenuItem new JMenuItem退出 backMenuItem new JMenuItem悔棋//初始化菜单项 sysMenu.addstartMenuItem//将三个菜单项添加到菜单上 sysMenu.addbackMenuItem sysMenu.addexitMenuItem MyItemListener lis new MyItemListener//初始化按钮事件监听器内部类 this.startMenuItem.addActionListenerlis//将三个菜单项注册到事件监听器上 backMenuItem.addActionListenerlis exitMenuItem.addActionListenerlis menuBar.addsysMenu//将系统菜单添加到菜单栏上 setJMenuBarmenuBar// 将 menuBar 设置为菜单栏 toolbar new JPanel//工具面板栏实例化 startButton new JButton重新开始//三个按钮初始化 backButton new JButton悔棋 exitButton new JButton退出 toolbar.setLayoutnew FlowLayoutFlowLayout.LEFT// 将 工 具 面 板 按 钮 用FlowLayout 布局 toolbar.addstartButton//将三个按钮添加到工具面板上 toolbar.addbackButton toolbar.addexitButton startButton.addActionListenerlis//将三个按钮注册监听事件 backButton.addActionListenerlis exitButton.addActionListenerlis addtoolbar BorderLayout.SOUTH//将工具面板布局到界面南方也就是下面 addchessBoard//将面板对象添加到窗体上 setDefaultCloseOperationJFrame.EXIT_ON_CLOSE//设置界面关闭事件 //setSize800800 pack // 自适应大小 private class MyItemListener implements ActionListener //事件监听器内部类 public void actionPerformedActionEvent 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.exit0 // 结束应用程序 else if obj backMenuItem obj backButton // 悔棋 System.out.println悔棋... chessBoard.goback public static void mainString args StartChessJFrame f new StartChessJFrame // 创建主框架 f.setVisibletrue // 显示主框架 第二个文件: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 Pointint x int y Color color this.x x this.y y this.color color public int getX // 拿到棋盘中的 x 索引 return x public int getY // 拿到棋盘中的 Y 索引 return y public Color getColor //得到颜色 return color 第三个文件:import javax.swing.import
java.awt.import
java.awt.event.MouseListenerimport
java.awt.event.MouseMotionListenerimport
java.awt.event.MouseEvent/ 五子棋-棋盘类 /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 PointROWS 1 COLS 1 // 初始每个数组元素为 null boolean isBlack true//默然开始是黑棋先下 boolean gameOver false// 游戏是否结束 int chessCount // 当前棋盘的棋子个数 int xIndex yIndex // 当前刚下的棋子的索引 public ChessBoard setBackgroundColor.ORANGE//设置背景颜色为橘黄色 addMouseListenerthis// 添加监听器 addMouseMotionListenernew MouseMotionListener // 匿名内部类 public void mouseDraggedMouseEvent e public void mouseMovedMouseEvent e int x1 e.getX - MARGIN GRID_SPAN / 2 / GRID_SPAN // 将鼠标点击的坐标位置转换成网格索引。
int y1 e.getY - MARGIN GRID_SPAN / 2 / GRID_SPAN // 游戏已经结束,不能下 // 落在棋盘外,不能下 // xy 位置已经有棋子存在,不能下 if x1 0 x1 ROWS y1 0 y1 COLS gameOver findChessx1 y1 setCursornew CursorCursor.DEFAULT_CURSOR // 设置成默认形状 else setCursornew CursorCursor.HAND_CURSOR // 设置成手型 // 绘制 public void paintComponentGraphics g super.paintComponentg // 画棋盘 for int i 0 i COLS return // xy 位置已经有棋子存在,不能下 if findChessxIndex yIndex return Point ch new PointxIndex yIndex isBlack Color.black: Color.white chessListchessCount ch repaint // 通知系统重新绘制 if isWin // 给出胜利信息不能再继续下棋 String msg String.format恭喜,s 赢了! colorName JOptionPane.showMessageDialogthis msg gameOver true isBlack isBlack // 覆盖 MouseListener 的方法 public void mouseClickedMouseEvent e // 鼠标按键在组件上单击(按下并释放)时调用。
public void mouseEnteredMouseEvent e // 鼠标进入到组件上时调用。
public void mouseExitedMouseEvent e // 鼠标离开组件时调用。
public void mouseReleasedMouseEvent e // 鼠标按钮在组件上释放时调用。
// 在棋子数组中查找是否有索引为 xy 的棋子存在private boolean findChessint x int y for Point c : chessList if c null c.getX x c.getY y return true return falseprivate boolean isWin //判断那方赢 int continueCount 1 // 连续棋子的个数 // 横向向西寻找 for int x xIndex - 1 x 0 x-- Color c isBlack Color.black : Color.white if getChessx yIndex c null continueCount else break // 横向向东寻找 for int x xIndex 1 x 5 return true else continueCount 1 // 继续另一种情况的搜索: 纵向 // 纵向向上寻找 for int y yIndex - 1 y 0 y-- Color c isBlack Color.black : Color.white if getChessxIndex y c null continueCount else break// 纵向向下寻找for int y yIndex 1 y 5 return true else continueCount 1// 继续另一种情况的搜索: 斜向// 东北寻找for int x xIndex 1 y yIndex - 1 y 0 x 5 return true else continueCount 1// 继续另一种情况的搜索: 斜向// 西北寻找for int x xIndex - 1 y yIndex - 1 y 0 x 0 x-- y-- Color c isBlack Color.black : Color.white if getChessx y c null continueCount else break // 西南寻找 for int x xIndex 1 y yIndex 1 y 0 xIndex chessListchessCount - 1.getX yIndex chessListchessCount - 1.getY isBlack isBlack repaint // Dimension: 矩形 public Dimension getPreferredSize return new DimensionMARGIN 2 GRID_SPAN COLS MARGIN 2 GRID_SPAN ROWS