Java用GUI实现单机五子棋

 

 

MyPanel代码: 

package gui;

import java.awt.Image;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class MyPanel extends JPanel {

	Image imgBoard=new ImageIcon("imags\\ChessBoard.jpg").getImage();
	Image imgB=new ImageIcon("imags\\black.png").getImage();
	Image imgW=new ImageIcon("imags\\white.png").getImage();
	
	int iLeft=20;
	int iTop=20;
	int [][] chessData;
	/**
	 * Create the panel.
	 */
	public MyPanel(int [][]cData) {
		chessData=cData;
	}
	
	public void paint(Graphics gs) {
		super.paint(gs);
		gs.drawImage(imgBoard, 0, 0, null);
		for(int i=0;i<chessData.length;i++) {
			for(int j=0;j<chessData[0].length;j++) {
				
				int X=i*20+iLeft-10;
				int Y=j*20+iTop-10;
				if(chessData[i][j]==1) {
					gs.drawImage(imgB, X, Y, null);
				}
				else if(chessData[i][j]==2) {
					gs.drawImage(imgW, X, Y, null);
				}
				else if(chessData[i][j]!=0){
					return;
				}
			}
		}
		
		
	}
}

 chess代码:

package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class chess extends JFrame {

	private JPanel contentPane;
	public JPanel panel;
	public JButton btnNewButton;
	public JButton btnNewButton_1;
	int [][]chessData=new int [15][15];
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					chess frame = new chess();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public chess() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 566, 531);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
	    btnNewButton = new JButton("绘制图片");
	    btnNewButton.setBounds(109, 424, 93, 31);
		btnNewButton.addActionListener(new BtnNewButtonActionListener());
		contentPane.setLayout(null);
		contentPane.add(btnNewButton);
		
		btnNewButton_1 = new JButton("清屏");
		btnNewButton_1.setBounds(324, 424, 93, 29);
		btnNewButton_1.addActionListener(new BtnNewButton_1ActionListener());
		contentPane.add(btnNewButton_1);
		
		panel = new MyPanel(chessData);
		panel.setBounds(10, 10, 530, 396);
		panel.addMouseListener(new PanelMouseListener());
		contentPane.add(panel);
		panel.setLayout(null);
	}
	private class BtnNewButtonActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			//绘制图片
			chessData[2][3]=1;
			chessData[5][7]=2;
			chessData[0][3]=1;
			chessData[10][7]=2;
			panel.repaint();
		}
	}
	private class BtnNewButton_1ActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			for(int i=0;i<chessData.length;i++) {
				for(int j=0;j<chessData[0].length;j++) {
					chessData[i][j]=0;
				}
			}
			panel.repaint();
		}
	}
	
	int chessType=1;//0:无棋子  1:黑子  2:白子
	private class PanelMouseListener extends MouseAdapter {
		@Override
		public void mouseClicked(MouseEvent e) {
			int X=e.getX();
			int Y=e.getY();
			setTitle(X+"|"+Y);
			int iLeft=20;
			int iTop=20;
			int i=Math.round((X-iLeft)/20f);
			int j=Math.round((Y-iTop)/20f);
			
			if(i>=0&&i<15&&j>=0&&j<15) {//边界检测
				chessData[i][j]=chessType;
				
				
				panel.repaint();
				
					if(isWin(i,j,chessType)) {
						String str="";
						str=chessType==1?"黑子":"白子";
						JOptionPane.showMessageDialog(null, str+"赢了。");
					}
					
				
					chessType=chessType%2==0?1:2;
			}
			
			
			
		}
	}
	
	public boolean isWin(int x,int y,int chessType) {
		int count=1;
		int i=0;
		int j=0;
		//向左遍历
		for(i=x-1;i>0;i--) {
			if(chessData[i][y]==chessType) {
				count++;
				if(count>=5) {
					return true;
				}
			}
			else {
				break;
			}
		}//向右遍历
		
		for(i=x+1;i<=15;i++) {
			if(chessData[i][y]==chessType) {
				count++;
				if(count>=5) {
					return true;
				}
			}
			else {
				break;
			}
		}//垂直方向判断
		//垂直向上判断
		for(j=y-1;j>0;j--) {
			if(chessData[x][j]==chessType) {
				count++;
				if(count>=5) {
					return true;
				}
			}
			else {
				break;
			}
		}
		//垂直向下判断
		for(j=y+1;j<=15;j++) {
			if(chessData[x][j]==chessType) {
				count++;
				if(count>=5) {
					return true;
				}
			}
			else {
				break;
			}
		}
		
		//判断左上右下
		//先判断左边
		for(i=x-1,j=y-1;i>0&&j>0;i--,j--) {
			if(chessData[i][j]==chessType) {
				count++;
				if(count>=5) {
					count=1;
					return true;
				}
			}
			else {
				break;
			}
		}
		//判断右边的
		for(i=x+1,j=y+1;i<=15&&j<=15;i++,j++) {
			if(chessData[i][j]==chessType) {
				count++;
				if(count>=5) {
					count=1;
					return true;
				}
			}
			else {
				break;
			}
		}
		//判断右下左下
		//先判断左边
		for(i=x+1,j=y-1;i<=15&&j>0;i++,j--) {
			if(chessData[i][j]==chessType) {
				count++;
				if(count>=5) {
					return true;
				}
			}
			else {
				break;
			}
		}
		
		//判断右边的
		for(i=x-1,j=y+1;i>0&&j<=15;i--,j++) {
			if(chessData[i][j]==chessType) {
				count++;
				if(count>=5) {
					count=1;
					return true;
				}
			}
			else {
				break;
			}
		}
		
	
	return false;
	}

}

实现结果如图:

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值