⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gamelistener.java

📁 一个用java-swing编写的连连看游戏例子
💻 JAVA
字号:
/*
 * 创建日期 2004-11-22
 *
 * TODO 要更改此生成的文件的模板,请转至
 * 窗口 - 首选项 - Java - 代码样式 - 代码模板
 */
package game;

import java.awt.event.*;
//import javax.swing.*;
import java.awt.*;
/**
 * @author Jerry703
 *
 * TODO 要更改此生成的类型注释的模板,请转至
 * 窗口 - 首选项 - Java - 代码样式 - 代码模板
 */
public class GameListener implements ActionListener{
	public GroupButton mypanel;//保存自己的监听对象的句柄
	//public MenuPanel menupanel;
	public boolean first = true;//是否为第一次按钮
	SingleButton choise;//当前选择按钮的句柄
	SingleButton before;//上次选择的按钮的句柄
	int x;//上次选择按钮所在的坐标x值
	int y;//通上的y值
	public boolean hasway;//是否存在通路
	SingleButton smallx;//判断前后2个按钮的相对位置
	SingleButton bigx;//判断前后2个按钮的相对位置
	SingleButton smally;//判断前后2个按钮的相对位置
	SingleButton bigy;//判断前后2个按钮的相对位置
	
	public GameListener(GroupButton temp){
		this.mypanel = temp;
	}
	/* (非 Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 * 监听器
	 */
	public void actionPerformed(ActionEvent e) {
		/*
		 * 如果监听对象为SingleButton的事例时
		 * 如果是在该面版按下的第一个按钮时调用getFirst
		 * 否则用doCheck方法检查本次按钮与上次按钮是否类型相同
		 * 如果相同,则再进入路线查找函数findWay
		 * 如果路线查找成功,将这2个按钮删除
		 */
		if (e.getSource() instanceof SingleButton){
			if (first){
				getFirst(e);
			}
			else{
				if (doCheck(e)){
					if (findWay(e)){
						deleteSame(e);
					}
					else{
						before.setBackground(Color.GRAY);
						getFirst(e);
					}
				}
				else{
					clearIt();
					getFirst(e);
				}
			}
		}
		return;
	}
	
	public void getFirst(ActionEvent e){
		/*
		 * 函数功能:
		 * 保存本次点击按钮的坐标,并且设置背景颜色
		 */
		first = false;
		choise = (SingleButton)e.getSource();
		x = choise.x;
		y = choise.y;
		choise.setBackground(Color.GREEN);
		System.out.println("选择了字母‘"+choise.type+"’,长度为"+choise.type);
	}
	public boolean doCheck(ActionEvent e){
		/*
		 * 检查本次点击与上次点击是否:同一按钮、同类按钮、不同按钮
		 * 返回boolean值
		 */
		before = choise;
		choise = (SingleButton)e.getSource();
		System.out.println("选择了字母‘"+choise.type+"’,长度为"+choise.type);
		if (choise.x == x && choise.y == y){
			clearIt();
			System.out.println("选择了同一个按钮");
			return false;
		}
		if (before.type == choise.type){
			choise.setBackground(Color.green);
			System.out.println("选择了字符相同的按钮");
			return true;
		}
		else{
			before.setBackground(Color.GRAY);
			x = choise.x;
			y = choise.y;
			choise.setBackground(Color.GREEN);
			System.out.println("选择不同的按钮");
			return false;
		}	
	}
	public boolean findWay(ActionEvent e){
		/*
		 * 根据前后两个按钮的坐标,判断2个按钮位置的相对关系
		 * 并且得出x轴与y轴的绝对值,对x轴和y轴进行依次查找
		 * 找出主轴后调用副函数hasXfindY,hasYfindX,查找支线路线
		 * 成功得到路线返回真
		 * 否则返回假
		 */
		try{
			System.out.println("进入路线查询");
			if (before.x<choise.x){
				smallx = before;
				bigx = choise;
			}
			else{
				smallx = choise;
				bigx = before;
			}
			if (before.y<choise.y){
				smally = before;
				bigy = choise;
			}
			else{
				smally = choise;
				bigy = before;
			}
			before.empty = true;
			choise.empty = true;
			int xstart;
			int xend;
			if (this.x < choise.x){
				xstart = this.x;
				xend = choise.x;
			}
			else{
				xstart = choise.x;
				xend = this.x;
			}
			int ystart;
			int yend;
			if (this.y < choise.y){
				ystart = this.y;
				yend = choise.y;
			}
			else{
				ystart = choise.y;
				yend = this.y;
			}
			//int xstart = x<choise.x?x:choise.x;
			//int xend = choise.x>x?choise.x:x;
			//int ystart = y<choise.y?y:choise.y;
			//int yend = choise.y>y?choise.y:y;
			if (!(Math.abs(choise.x - x) == 0)){
				System.out.println("X为主轴查找");
				int j;
				for (j=0;j<mypanel.MaxY;++j){
					hasway = true;
					for (int m=xstart;m<=xend;++m){
						if ( ! (mypanel.groupbutton[j][m] == null || mypanel.groupbutton[j][m].empty)){
							hasway = false;
							System.out.print(mypanel.groupbutton[j][m].empty + "  ");
							break;	
						}
						System.out.print(j+"时 "+m+"为空?:"+mypanel.groupbutton[j][m].empty + "  ");
					}
					if (hasway && hasXfindY(j,smallx.y,xstart) && hasXfindY(j,bigx.y,xend)){
						return true;
					}
					else
						hasway = false;
				}
			}
			if (!(Math.abs(choise.y - y) == 0)){
				System.out.println("以Y主轴查找");
				int i;
				for (i=0;i<mypanel.MaxX;++i){
					hasway = true;
					for (int n=ystart;n<=yend;++n){
						if (!(mypanel.groupbutton[n][i] == null || mypanel.groupbutton[n][i].empty)){
							hasway = false;
							break;
						}
					}
					if (hasway && hasYfindX(i,smally.x,ystart) && hasYfindX(i,bigy.x,yend)){
						return true;
					}
					else
						hasway = false;
				}
			}
			return false;
		}catch(Exception error){
			error.printStackTrace();
			return false;
		}
		finally{
			before.empty = false;
			choise.empty = false;
		}
	}
	public boolean hasXfindY(int start,int end,int x){
		/*
		 * 找到x轴主线后,查找Y轴的2条支线
		 * 2条支线均成功返回真
		 * 否则返回假
		 */
		if (start == end){
			System.out.println("找到一条连线");
			return true;
		}
		if (start>end){
			int temp;
			temp = start;
			start = end;
			end = temp;
		}
		for (int i=start;i<=end;++i){
			if ( ! (mypanel.groupbutton[i][x] == null || mypanel.groupbutton[i][x].empty)){
				hasway = false;
				System.out.println("连线失败");
				return false;
			}
		}
		System.out.println("找到一条连线");
		return true;
	}
	public boolean hasYfindX(int start,int end,int y){
		/*
		 * 找到y轴主线后,查找x轴的2条支线
		 * 2条支线均成功返回真
		 * 否则返回假
		 */
		if (start == end){
			System.out.println("可直接连线");
			return true;
		}
		if (start>end){
			int temp;
			temp = start;
			start = end;
			end = temp;
		}
		for (int i=start;i<=end;++i){
			if (! (mypanel.groupbutton[y][i] == null || mypanel.groupbutton[y][i].empty)){
				hasway = false;
				System.out.println("连线失败");
				return false;
			}
		}
		System.out.println("找到一条连线");
		return true;
	}
	public void deleteSame(ActionEvent e){
		/*
		 * 将2个按钮的empty属性设为true
		 * 删除这2个按钮
		 * 调用状态清空函数,清除所有属性
		 */
		before.empty = true;
		choise.empty = true;
		mypanel.groupbutton[before.y][before.x].hide();
		//mypanel.remove(mypanel.groupbutton[before.y][before.x]);
		mypanel.repaint();
		mypanel.groupbutton[choise.y][choise.x].hide();
		//mypanel.remove(mypanel.groupbutton[choise.y][choise.x]);
		mypanel.repaint();
		mypanel.count -= 2;
		this.clearIt();
	}
	public void clearIt(){
		/*
		 * 清空状态
		 */
		if (choise != null){
			choise.setBackground(Color.GRAY);
			choise = null;
		}
		if (before != null){
			before.setBackground(Color.GRAY);
			before = null;
		}
		this.x = -1;
		this.y = -1;
		this.before = null;
		this.choise = null;
		this.first = true;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -