?? gamelistener.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 + -