文件名称:
五子棋java编写的,有点残,如果有意可以改改
开发工具:
文件大小: 7kb
下载次数: 0
上传时间: 2009-12-14
详细说明: 可以下下package jdk; import javax.swing.*; import java.awt.*; import java.awt.event.*; class ChessPanel extends JPanel{ private int space=20; private int grids=30; private int radius=space/2; private int[][]chesses=new int[grids+1][grids+1]; private int currColor=1; private JMenuBar chessMenuBar=new JMenuBar(); private JMenu optMenu=new JMenu("操作"); private JMenuItem startMenuItem=new JMenuItem("开始"); private JMenuItem exitMenuItem=new JMenuItem("退出"); private ActionListener startHand ler=new ActionListener(){ public void actionPerformed(ActionEvent e){ clearGrids(); currColor=1; repaint(); } }; private ActionListener exitHandler=new ActionListener(){ public void actionPerformed(ActionEvent e){ System.exit(0); } }; private MouseListener playChessHandler=new MouseAdapter(){ public void mouseClicked(MouseEvent e){ int x=e.getX(); int y=e.getY(); if(x<=grids*space&&x>=0&&y<=grids*space&&y>=0) if(chesses[round(x)][round(y)]==0){ chesses[round(x)][round(y)]=currColor; currColor=currColor==1?2:1; repaint(); } } }; public int round(float a){ float f=a/space; return Math.round(f); } public ChessPanel(int space,int grids){ this.space=space; this.grids=grids; this.radius=space/2; setBackground(Color.YELLOW); setSize(space*grids,space*grids); startMenuItem.addActionListener(startHandler); exitMenuItem.addActionListener(exitHandler); addMouseListener(playChessHandler); chessMenuBar.add(optMenu); optMenu.add(startMenuItem); optMenu.add(exitMenuItem); } public JMenuBar getMenuBar(){ return chessMenuBar; } private void drawChess(Graphics g,int x,int y,int color){ g.setColor(color==1?Color.WHITE:Color.BLACK); g.fillOval(x*space-radius, y*space-radius, radius*2, radius*2); } public void drawGrids(Graphics g){ g.setColor(Color.DARK_GRAY); for(int i=0;i<=grids;i++) { g.drawLine(0, i*space, grids*space, i*space); g.drawLine(i*space, 0, i*space,grids*space); } } private void clearGrids(){ for(int i=0;i<=grids;i++) for(int j=0;j<=grids;j++) chesses[i][j]=0; } public void paintComponent(Graphics g){ super.paintComponent(g); drawGrids(g); for(int i=0;i<=grids;i++) for(int j=0;j<=grids;j++) if(chesses[i][j]!=0) drawChess(g,i,j,chesses[i][j]); } } public class ChessPlayer extends JFrame{ private ChessPanel chessPanel=new ChessPanel(20,30); public ChessPlayer(String title){ super(title); Container contentPane=getContentPane(); contentPane.add(chessPanel); setJMenuBar(chessPanel.getMenuBar()); setSize(600,600); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[]args){ new ChessPlayer("五子棋"); } } ...展开收缩
(系统自动生成,下载前可以参看下载内容)
下载文件列表
相关说明
- 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
- 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度。
- 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
- 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
- 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
- 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.