java五子棋游戏 代码

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package game;
  6. import javax.swing.*;
  7. import java.awt.*;
  8. import java.awt.event.*;
  9. class ChessPanel extends JPanel{
  10.     private int space=20;
  11.     //网格距离
  12.     private int grids=30;
  13.     //网格数.
  14.     private int radios=space/2;
  15.     //棋的半径。
  16.     private int[][] chesses=new int[grids+1][grids+1];
  17.     private int currcolor=1;
  18.     private JMenuBar chessmenubar=new JMenuBar();
  19.     private JMenu optmenu=new JMenu("文件");
  20.     private JMenuItem startmenuitem=new JMenuItem("开始");
  21.     private JMenuItem exitmenuitem=new JMenuItem("退出");
  22.     private ActionListener starthandeler=new ActionListener(){
  23.     public void actionPerformed(ActionEvent e){
  24.     cleargrids();
  25.     currcolor=1;
  26.     repaint();
  27.     }
  28.     };
  29.     private ActionListener exithandeler=new ActionListener(){
  30.     public void actionPerformed(ActionEvent e){
  31.     System.exit(0);
  32.     
  33.     }
  34.     };
  35.    private MouseListener playchesshaneler=new MouseAdapter(){
  36.     public void MouseClicked(MouseEvent e){
  37.         int x=e.getX();
  38.         int y=e.getY();
  39.         if(x<grids*space && x>=0 && y<grids*space && y>=0){
  40.             if(chesses[round(x)][round(y)]==0){
  41.                 chesses[round(x)][round(y)]=currcolor;
  42.                 currcolor=currcolor==1?2:1;
  43.                 repaint();
  44.             }
  45.         }
  46.     }
  47.     };
  48.     public int round(float a){
  49.     float f=a/space;
  50.     return Math.round(f);
  51.     }
  52.     public ChessPanel(int space,int grids){
  53.     this.space=space;
  54.     this.grids=grids;
  55.     this.radios=space/2;
  56.     setBackground(Color.YELLOW);
  57.     setSize(space*grids,space*grids);
  58.     startmenuitem.addActionListener(starthandeler);
  59.     exitmenuitem.addActionListener(exithandeler);
  60.     addMouseListener(playchesshaneler);
  61.     chessmenubar.add(optmenu);
  62.     optmenu.add(startmenuitem);
  63.     optmenu.add(exitmenuitem);
  64.     }
  65.     public JMenuBar getMenuBar(){
  66.     return chessmenubar;
  67.     }
  68.     private void drawchess(Graphics g,int x,int y,int color){
  69.         g.setColor(color==1?Color.WHITE:Color.BLACK);
  70.         g.fillOval(x*space-radios, y*space-radios, radios*2, radios*2);
  71.         }
  72.     public void drawGirds(Graphics g){
  73.     g.setColor(Color.DARK_GRAY);
  74.     for(int i=0;i<=grids;i++){
  75.     g.drawLine(0, i*space, grids*space, i*space);
  76.     g.drawLine(i*space, 0, i*space, grids*space);
  77.     
  78.     }
  79.     }
  80.     public void cleargrids(){
  81.         for(int i=0;i<grids;i++)
  82.             for(int j=0;j<=grids;j++)
  83.                 chesses[i][j]=0;
  84.     }
  85.     public void paintComponent(Graphics g){
  86.         super.paintComponent(g);
  87.         drawGirds(g);
  88.         for(int i=0;i<=grids;i++)
  89.             for(int j=0;j<=grids;j++)
  90.                 if(chesses[i][j]!=0)
  91.                     drawchess(g,i,j,chesses[i][j]);
  92.                 
  93.     
  94.     }
  95.     
  96. }
  97. public class ChessPlayer extends JFrame{
  98. private ChessPanel chesspanel=new ChessPanel(20,30);
  99. public ChessPlayer(String title){
  100. super(title);
  101. Container contentpane=getContentPane();
  102. contentpane.add(chesspanel);
  103. setJMenuBar(chesspanel.getMenuBar());
  104. setSize(600,600);
  105. setVisible(true);
  106. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  107. }
  108. public static void main(String args[]){
  109.     new ChessPlayer("五子棋");
  110. }
  111. }
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值