汇智学堂-Eclipse+WindowBuilder五子棋开发(菜鸟完整版)

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
*

  • 程序入口

*/

public class Gobang {
public static void main(String[] args) {
new DrawSee22();
}
}

class DrawSee22 extends JFrame {

private static final long serialVersionUID = 2L;

private Graphics jg;
private static final int w2=30;//格子的宽度
private static final int x1=100;//起点X坐标
private static final int y1=100;//起点Y坐标
private static final int x2=700;//终点X坐标
private static final int y2=700;//终点Y坐标

private int[][] position; // 存放游戏数据的二维数组

public Color c=Color.RED; 
private int isEnd2=0;

/**
 * DrawSee构造方法
 */
public DrawSee22() {
    
    setBounds(300, 20, 900, 800);//设置画布大小        
    this.setTitle("雷雷的井子游戏");       
    setVisible(true);
  
    //二维数组初始化
    
    position=new int[21][21];
    for(int i=0;i<21;i++){
    	for(int j=0;j<21;j++) {
    		position[i][j]=0;
    	}
    }     

    try {                      
        Thread.sleep(500);
    } catch (Exception e) {
        e.printStackTrace();
    }        

    // 获取专门用于在窗口界面上绘图的对象
    jg =  this.getGraphics();
    
    // 绘制游戏区域
    paintComponents(jg);
    
    // 添加鼠监听事件,当鼠标点击时触发
    this.addMouseListener(new MouseAdapter() {
        
        // 定义鼠标点击事件响应过程
        @Override
        public void mouseClicked(MouseEvent e) {
            
        	// 如果游戏结束,返回,不执行后面的代码                
            if(isEnd2==1) {                    
                JOptionPane.showMessageDialog(null,"游戏结束", "消息提示", JOptionPane.INFORMATION_MESSAGE);
                return;
            }   

            //获取鼠标点击的那一点的x,y坐标
            int x = e.getX(), y = e.getY();               
            int dx = (x - x1) / w2, dy = (y - y1) / w2;               
            int newx=dx*w2+x1,newy=dy*w2+y1;
            
            if(dx>=0&&dx<21&&dy<21&&dy>=0) {
            	paintComponents2(jg,newx-15,newy-15,30,30);//将棋子落在准确的位置
            }
         
           if(c==Color.RED) {
        	   position[dx][dy]=1;//记录落下的是红色的子
           }
           
           if(c==Color.green) {
        	   position[dx][dy]=2;//记录落下的是红色的子
           }
           
           upOrDown(dx,dy);//判断输赢  
           leftOrRight(dx,dy);
           Obliquedirection(dx,dy);
           Obliquedirection2(dx,dy);
           
         }
    });        
}

public void paintComponents(Graphics g) {
    try {            
        // 设置线条颜色为红色
        g.setColor(Color.RED);            
        
        // 绘制棋盘   
        
        for(int i=0;i<21;i++) {
        	g.drawLine(x1, y1+i*w2, x2, y1+i*w2);//画横线
        	g.drawLine(x1+i*w2, y1, x1+i*w2, y2);//画竖线
        }
     
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void paintComponents2(Graphics g,int x,int y,int width,int height) {
    try {            
        // 设置线条颜色为红色
    	
    	g.setColor(c);
    	
    	if(c==Color.RED) {
    		
    		c=Color.green;
    	}
    	else {        		
    		c=Color.red;
    	}
        // 绘制棋盘   
        g.fillOval(x, y, width, height);
     
    } catch (Exception e) {
        e.printStackTrace();
    }
}

//判断上下输赢
public void upOrDown(int x1,int y1) {    	
	 int sum=1;
    //向上判断 
	 for(int i=0;i<5;i++) {
		 
		 if(position[x1][y1]==0) {
			 return;
		 }    		 
		 if(position[x1][y1-i]==position[x1][y1-i-1]) {
			 sum+=1;
		 }
		 else {
			 break;
		 }
	 }
	 
	//向下判断 
	 for(int i=0;i<5;i++) {
		 
		 if(position[x1][y1]==0) {
			 return;
		 }    		 
		 if(position[x1][y1+i]==position[x1][y1+i+1]) {
			 sum+=1;
		 }
		 else {
			 break;
		 }
	 }
	 
	 if(sum>=5) {
		 JOptionPane.showMessageDialog(null,"游戏结束", "消息提示", JOptionPane.INFORMATION_MESSAGE);
	 }
	       
}

//判断左右输赢
public void leftOrRight(int x1,int y1) {
int sum=1;
//向左判断
for(int i=0;i<5;i++) {

		 if(position[x1][y1]==0) {
			 return;
		 }    		 
		 if(position[x1-i][y1]==position[x1-i-1][y1]) {
			 sum+=1;
		 }
		 else {
			 break;
		 }
	 }
	 
	//向右判断 
	 for(int i=0;i<5;i++) {
		 
		 if(position[x1][y1]==0) {
			 return;
		 } 
		 if(position[x1+i][y1]==position[x1+i+1][y1]) {
    			 sum+=1;
    	  }
		 else {
			 break;
		 }
	 }
	 
	 if(sum>=5) {
		 JOptionPane.showMessageDialog(null,"游戏结束", "消息提示", JOptionPane.INFORMATION_MESSAGE);
	 }
	       
}

//判断左上及右下输赢
public void Obliquedirection(int x1,int y1) {
int sum=1;
//向左上判断
for(int i=0;i<5;i++) {

		 if(position[x1][y1]==0) {
			 return;
		 }    		 
		 if(position[x1-i][y1-i]==position[x1-i-1][y1-i-1]) {
			 sum+=1;
		 }
		 else {
			 break;
		 }
	 }
	 
	//向右判断 
	 for(int i=0;i<5;i++) {
		 
		 if(position[x1][y1]==0) {
			 return;
		 }    		 
		 if(position[x1+i][y1+i]==position[x1+i+1][y1+i+1]) {
			 sum+=1;
		 }
		 else {
			 break;
		 }
	 }
	 
	 if(sum>=5) {
		 JOptionPane.showMessageDialog(null,"游戏结束", "消息提示", JOptionPane.INFORMATION_MESSAGE);
	 }
	       
}

//判断右上及左下输赢
public void Obliquedirection2(int x1,int y1) {
int sum=1;
//向右上判断
for(int i=0;i<5;i++) {

		 if(position[x1][y1]==0) {
			 return;
		 }    		 
		 if(position[x1+i][y1-i]==position[x1+i+1][y1-i-1]) {
			 sum+=1;
		 }
		 else {
			 break;
		 }
	 }
	 
	//向左下判断 
	 for(int i=0;i<5;i++) {
		 
		 if(position[x1][y1]==0) {
			 return;
		 }    		 
		 if(position[x1-i][y1+i]==position[x1-i-1][y1+i+1]) {
			 sum+=1;
		 }
		 else {
			 break;
		 }
	 }
	 
	 if(sum>=5) {
		 JOptionPane.showMessageDialog(null,"游戏结束", "消息提示", JOptionPane.INFORMATION_MESSAGE);
	 } 
}

}

你可能感兴趣的:(汇智学堂-Eclipse+WindowBuilder五子棋开发(菜鸟完整版))