📄 llkjm.java
字号:
buttonArray[i][j].setSelected(true);
buttonArray[i][j].setIcon(pictures[i - 1][j - 1]);
}
}
public void actionPerformed(ActionEvent e){
/**启动线程*/
Thread thread = new Thread();
thread.start();
g = getGraphics();
Graphics2D g2 = (Graphics2D)g;
BasicStroke f = new BasicStroke(3);
g2.setColor(Color.red);
g2.setStroke(f);
if((x1 == -1 && y1 == -1)){
((JButton)(e.getSource())).setBorderPainted(true);
((JButton)(e.getSource())).setBorder(new LineBorder(Color.blue,2));
x1 = getX(buttonArray,(JButton)e.getSource());
y1 = getY(buttonArray,(JButton)e.getSource());
formerButton = (JButton)e.getSource();
}
else{
x2 = getX(buttonArray,(JButton)e.getSource());
y2 = getY(buttonArray,(JButton)e.getSource());
if(canDelete(x1,y1,x2,y2,randomNumber,false)){
try{
repaint();
((JButton)(e.getSource())).setBorderPainted(true);
((JButton)(e.getSource())).setBorder(new LineBorder(Color.blue,2));
switch (swerveNum){
case 0: int[] x ={jbtW * y1 + jbtW / 2,jbtW * y2 + jbtW / 2};
int[] y ={jbtH * x1 + jbtH / 2,jbtH * x2 + jbtH / 2};
distance = Math.max(Math.abs(x1-x2),Math.abs(y1-y2));
g2.drawPolyline(x,y,x.length);
break;
case 1: int[] yo ={jbtH * x1 + jbtH / 2,jbtH * middlePoints[2] + jbtH / 2,jbtH * x2 + jbtH / 2};
int[] xo ={jbtW * y1 + jbtW / 2,jbtW * middlePoints[3] + jbtW / 2,jbtW * y2 + jbtW / 2};
distance = Math.abs(x1-x2) + Math.abs(y1-y2);
g2.drawPolyline(xo,yo,xo.length);
break;
case 2: int[] yt = {jbtH * x1 + jbtH / 2,jbtH * middlePoints[0] + jbtH / 2,
jbtH * middlePoints[2] + jbtH / 2,jbtH * x2 + jbtH / 2};
int[] xt = {jbtW * y1 + jbtW / 2,jbtW * middlePoints[1] + jbtW / 2,
jbtW * middlePoints[3] + jbtW / 2,jbtW * y2 + jbtW / 2};
distance = Math.max(Math.abs(x1-middlePoints[0]),Math.abs(y1-middlePoints[1]))
+ Math.abs(middlePoints[0]-x2) + Math.abs(middlePoints[1]-y2);
g2.drawPolyline(xt,yt,xt.length);
break;
}
Thread.sleep(300);
if (count == 2) toLeft(x1,y1,x2,y2,randomNumber,buttonArray);
if (count == 3) up(x1,y1,x2,y2,randomNumber,buttonArray);
if (count == 1){
formerButton.setVisible(false);
formerButton.setSelected(false);
formerButton.setIcon(null);
((JButton)(e.getSource())).setVisible(false);
((JButton)(e.getSource())).setSelected(false);
((JButton)(e.getSource())).setIcon(null);
randomNumber[x1][y1] = -1;
randomNumber[x2][y2] = -1;
}
setScore(distance,count);
// 当消完时
if (isNull = isNull(randomNumber))
if (count < 3){
newBoard();
Llkjm.timeRestrict.restart();
}
else
try {
Llkjm.timeRestrict.suspend();
PaiHang.newScore();
Llkjm.drawLabelPanel.suspend();
PaiHang.showPaiHang();
score = 0;
Llkjm.jmiEnd.doClick();
return;
} catch (IOException ex) {
ex.printStackTrace();
}
hint(randomNumber,buttonArray,true);
}catch(InterruptedException ex){
}
}
else{
((JButton)(e.getSource())).setBorderPainted(false);
((JButton)(e.getSource())).setBorder(null);
formerButton.setBorderPainted(false);
formerButton.setBorder(null);
}
x1 = -1;
y1 = -1;
}
validate();
}
public int getX(JButton[][] buttonArray,JButton button){
int x = 0;
for (int i = 1; i < buttonArray.length - 1; i++)
for (int j = 1 ; j < buttonArray[0].length - 1; j++)
if (buttonArray[i][j] == button) x = i;
return x;
}
public int getY(JButton[][] buttonArray,JButton button){
int y = 0;
for (int i = 1; i < buttonArray.length - 1; i++)
for (int j = 1 ; j < buttonArray[0].length - 1; j++)
if (buttonArray[i][j] == button) y = j;
return y;
}
//随机图片
public Icon[][] FormButton(int lengthOfx, int lengthOfy) {
Icon[][] pictures = new Icon[lengthOfx][lengthOfy];
randomNumber = randomNum(lengthOfx, lengthOfy);
for (int i = 0; i < lengthOfx; i++)
for (int j = 0; j < lengthOfy; j++){
Image img = ResourseReader.getImageFromJar("/small-pic/renwu (" + randomNumber[i+1][j+1] + ").gif", this.getClass());
pictures[i][j] = new ImageIcon(img);
}
return pictures;
}
public int[][] randomNum(int row,int line){
int i,k = 0;
int[] cm = new int[IMGNUM];
for (i = 0; i < IMGNUM; i++)
cm[i] = i;
/** 把随机选择编号成双放入store中 */
int[] store = new int[row * line];
int j = 0;
for (i = 0; i < row * line - 1; i += 2){
k = (int)(Math.random()*(IMGNUM - j));
store[i] = store[i + 1] = cm[k];//一次存两个。
/** 如果K是最后一个,先判断是否已存遍,
*是,则再存,直至存满store.
*不是,则继续 */
if (k == IMGNUM - j - 1) {
if (j == IMGNUM - 1) {
for (int n = 0; n < IMGNUM; n++)
cm[n] = n;
j = 0;
}
else j++;
}
/** 将最后个覆盖掉被存的 */
else {cm[k] = cm[IMGNUM - j - 1]; j++;}
}
int[][] random = new int[row+2][line+2];
int x,y;
for (x = 0; x < row+2; x++){
random[x][0] = -1;
random[x][line+1] = -1;
}
for (y = 0; y < line+2; y++){
random[0][y] = -1;
random[row+1][y] = -1;
}
j = 0;
for (x = 1; x <= row; x++)
for (y = 1; y <= line; y++){
k = (int)(Math.random()*(row * line - j));
random[x][y] = store[k];
if (k == row * line - j - 1) j++;
else {store[k] = store[row * line - j - 1]; j++;}
}
return random;
}
public boolean canDelete(int x1,int y1,int x2,int y2,int[][] chessBoard,boolean isHint){
if (chessBoard[x1][y1] != chessBoard[x2][y2] || (x1 == x2 && y1 == y2)) return false;
boolean past = false;
/** 在没有转弯的的情况下可不可以连? */
if (x1 == x2) past = noTurnx(x1,y1,x2,y2,chessBoard);
if (y1 == y2) past = noTurny(x1,y1,x2,y2,chessBoard);
/** 如果可以连,相当于第一类棋子 */
if (past) {swerveNum = 0; return true;}
else {
/** 在一个转弯的情况下可不可以连? */
past = oneTurn(x1,y1,x2,y2,chessBoard,isHint);
/** 如果可以连,相当于第一类棋子 */
if (past) {swerveNum = 1; return true;}
else {
/** 在两个转弯的情况下可不可以连? */
if (twoTurn(x1,y1,x2,y2,chessBoard,isHint)) {swerveNum = 2;return true;}
else return false;
}
}
}
/** 判断在没有转弯的的情况下可不可以连 */
/** 判断在同一行可不可以连 */
public boolean noTurnx(int x1,int y1,int x2,int y2,int[][] chessBoard){
int temp;
/** 使棋子从左往右 */
if (y1 > y2) {temp = y1;y1 = y2;y2 = temp;}
/** 相邻棋子可以连 */
for (int k = y1 + 1;k < y2;k++)
/** 中间有棋子,返回false */
if (chessBoard[x1][k] >= 0)
return false;
return true;
}
/** 判断在同一列可不可以连 */
public boolean noTurny(int x1,int y1,int x2,int y2,int[][] chessBoard){
int temp;
/** 使棋子从上往下 */
if (x1 > x2) {temp = x1;x1 = x2;x2 = temp;}
for (int k = x1 + 1;k < x2; k++)
/** 中间有棋子,返回false */
if (chessBoard[k][y1] >= 0)
return false;
return true;
}
/** 判断在一个转弯的情况下可不可以连 */
public boolean oneTurn(int x1,int y1,int x2,int y2,int[][] chessBoard,boolean isHint){
/** 判断转弯处有无棋子 */
if (chessBoard[x1][y2] < 0)
if (noTurnx(x1,y1,x1,y2,chessBoard) && noTurny(x1,y2,x2,y2,chessBoard)){
if (!isHint){
middlePoints[2] = x1;
middlePoints[3] = y2;
}
return true;
}
if (chessBoard[x2][y1] < 0)
if (noTurnx(x2,y1,x2,y2,chessBoard) && noTurny(x1,y1,x2,y1,chessBoard)){
if (!isHint){
middlePoints[2] = x2;
middlePoints[3] = y1;
}
return true;
}
return false;
}
/** 判断在两个转弯的情况下可不可以连 */
public boolean twoTurn(int x1,int y1,int x2,int y2,int[][] chessBoard,boolean isHint){
int i,j;
/** 棋子向上开始连 */
for (i = x1 - 1;i >= 0;i--)
if (chessBoard[i][y1] < 0)
if (oneTurn(i,y1,x2,y2,chessBoard,isHint)){
middlePoints[0] = i;
middlePoints[1] = y1;
return true;
}
else continue;
else break;
/** 棋子向下开始连 */
for (i = x1 + 1;i <= lengthOfx + 1;i++)
if (chessBoard[i][y1] < 0)
if (oneTurn(i,y1,x2,y2,chessBoard,isHint)){
middlePoints[0] = i;
middlePoints[1] = y1;
return true;
}
else continue;
else break;
/** 棋子向左开始连 */
for (j = y1 - 1;j >= 0;j--)
if (chessBoard[x1][j] < 0)
if (oneTurn(x1,j,x2,y2,chessBoard,isHint)){
middlePoints[0] = x1;
middlePoints[1] = j;
return true;
}
else continue;
else break;
/** 棋子向右开始连 */
for (j = y1 + 1;j <= lengthOfy + 1;j++)
if (chessBoard[x1][j] < 0)
if (oneTurn(x1,j,x2,y2,chessBoard,isHint)){
middlePoints[0] = x1;
middlePoints[1] = j;
return true;
}
else continue;
else break;
return false;
}
// 自定义方法hint来提示用户可以消除的牌的坐标
public void hint(int[][] array,JButton[][] buttonArray,boolean isReset){
int x, y;
while (true){
for (x = 1;x <= lengthOfx;x++)
for (y = 1;y <= lengthOfy;y++)
if (array[x][y] >= 0)
/** 向后找相同的 */
for (int a = x;a <= lengthOfx;a++){
int b;
if (a == x) b = y + 1;
else b = 1;
for (;b <= lengthOfy;b++)
/** 把相同的棋子坐标存入test[i][]中 */
if (array[x][y] == array[a][b])
if (canDelete(x,y,a,b,array,true)){
if (!isReset){
buttonArray[x][y].setBorderPainted(true);
buttonArray[x][y].setBorder(new LineBorder(Color.red,2));
buttonArray[a][b].setBorderPainted(true);
buttonArray[a][b].setBorder(new LineBorder(Color.red,2));
}
return;
}
}
reset(array,buttonArray);
}
}
//重排500次
public void reset(int[][] array,JButton[][] buttonArray){
for (int i = 0; i < 500; i++){
int a = 1 + (int)(Math.random()*(lengthOfx));
int b = 1 + (int)(Math.random()*(lengthOfy));
int m = 1 + (int)(Math.random()*(lengthOfx));
int n = 1 + (int)(Math.random()*(lengthOfy));
if (buttonArray[a][b].getIcon() == null || buttonArray[m][n].getIcon() == null)
continue;
int temp = array[a][b];
array[a][b] = array[m][n];
array[m][n] = temp;
JButton jbtTemp = new JButton();
jbtTemp.setIcon(buttonArray[a][b].getIcon());
buttonArray[a][b].setIcon(buttonArray[m][n].getIcon());
buttonArray[m][n].setIcon(jbtTemp.getIcon());
}
}
//第二关向左
public void toLeft(int x1,int y1,int x2,int y2,int[][] array,JButton[][] buttonArray){
buttonArray[x1][y1].setBorderPainted(false);
buttonArray[x1][y1].setBorder(null);
buttonArray[x2][y2].setBorderPainted(false);
buttonArray[x2][y2].setBorder(null);
int j,i;
if (x1 == x2) {
for (i = Math.min(y1,y2); i < Math.max(y1,y2) - 1; i++){
array[x1][i] = array[x1][i+1];
buttonArray[x1][i].setIcon(buttonArray[x1][i+1].getIcon());
}
j = Math.max(y1,y2)+1;
while(array[x1][j] >= 0){
array[x1][j-2] = array[x1][j];
buttonArray[x1][j-2].setIcon(buttonArray[x1][j].getIcon());
j++;
}
array[x1][j-2] = -1;
array[x1][j-1] = -1;
buttonArray[x1][j-2].setVisible(false);
buttonArray[x1][j-2].setSelected(false);
buttonArray[x1][j-2].setIcon(null);
buttonArray[x1][j-1].setVisible(false);
buttonArray[x1][j-1].setSelected(false);
buttonArray[x1][j-1].setIcon(null);
}
else {
j = y1;
while (array[x1][j] >= 0){
array[x1][j] = array[x1][++j];
buttonArray[x1][j-1].setIcon(buttonArray[x1][j].getIcon());
}
array[x1][j-1] = -1;
buttonArray[x1][j-1].setVisible(false);
buttonArray[x1][j-1].setSelected(false);
buttonArray[x1][j-1].setIcon(null);
j = y2;
while (array[x2][j] >= 0){
array[x2][j] = array[x2][++j];
buttonArray[x2][j-1].setIcon(buttonArray[x2][j].getIcon());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -