1. /*
2. * To change this template, choose Tools | Templates
3. * and open the template in the editor.
4. */
5.
6. package
7.
8. import
9. import
10. import
11. class ChessPanel extends
12. private int space=20;
13. //网格距离
14. private int grids=30;
15. //网格数.
16. private int radios=space/2;
17. //棋的半径。
18. private int[][] chesses=new int[grids+1][grids+1];
19. private int currcolor=1;
20. private JMenuBar chessmenubar=new
21. private JMenu optmenu=new JMenu("文件");
22. private JMenuItem startmenuitem=new JMenuItem("开始");
23. private JMenuItem exitmenuitem=new JMenuItem("退出");
24. private ActionListener starthandeler=new
25. public void
26. cleargrids();
27. 1;
28. repaint();
29. }
30. };
31. private ActionListener exithandeler=new
32. public void
33. 0);
34.
35. }
36. };
37. private MouseListener playchesshaneler=new
38. public void
39. int
40. int
41. if(x<grids*space && x>=0 && y<grids*space && y>=0){
42. if(chesses[round(x)][round(y)]==0){
43. chesses[round(x)][round(y)]=currcolor;
44. 1?2:1;
45. repaint();
46. }
47. }
48. }
49. };
50. public int round(float
51. float
52. return
53. }
54. public ChessPanel(int space,int
55. this.space=space;
56. this.grids=grids;
57. this.radios=space/2;
58. setBackground(Color.YELLOW);
59. setSize(space*grids,space*grids);
60. startmenuitem.addActionListener(starthandeler);
61. exitmenuitem.addActionListener(exithandeler);
62. addMouseListener(playchesshaneler);
63. chessmenubar.add(optmenu);
64. optmenu.add(startmenuitem);
65. optmenu.add(exitmenuitem);
66. }
67. public
68. return
69. }
70. private void drawchess(Graphics g,int x,int y,int
71. 1?Color.WHITE:Color.BLACK);
72. 2, radios*2);
73. }
74. public void
75. g.setColor(Color.DARK_GRAY);
76. for(int i=0;i<=grids;i++){
77. 0, i*space, grids*space, i*space);
78. 0, i*space, grids*space);
79.
80. }
81. }
82. public void
83. for(int i=0;i<grids;i++)
84. for(int j=0;j<=grids;j++)
85. 0;
86. }
87. public void
88. super.paintComponent(g);
89. drawGirds(g);
90. for(int i=0;i<=grids;i++)
91. for(int j=0;j<=grids;j++)
92. if(chesses[i][j]!=0)
93. drawchess(g,i,j,chesses[i][j]);
94.
95.
96. }
97.
98.
99. }
100. public class ChessPlayer extends
101. private ChessPanel chesspanel=new ChessPanel(20,30);
102. public
103. super(title);
104. Container contentpane=getContentPane();
105. contentpane.add(chesspanel);
106. setJMenuBar(chesspanel.getMenuBar());
107. setSize(600,600);
108. setVisible(true);
109. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
110.
111.
112. }
113. public static void
114. new ChessPlayer("五子棋");
115. }
116.
117. }
五子棋 java代码 java五子棋完整代码
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
HTML+CSS+JS全手写五子棋
利用grid布局实现棋盘样式并通过js实现落子盘胜负等
五子棋 二维数组 判定胜负 grid布局