多的不说,直接上代码,就是一个简单的迷宫小游戏。
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Random;
import java.util.Stack;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
class Lattice {
static final int INTREE = 1;
static final int NOTINTREE = 0;
private int x = -1;
private int y = -1;
private int flag = NOTINTREE;
private Lattice father = null;
public Lattice(int xx, int yy) {
x = xx;
y = yy;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getFlag() {
return flag;
}
public Lattice getFather() {
return father;
}
public void setFather(Lattice f) {
father = f;
}
public void setFlag(int f) {
flag = f;
}
public String toString() {
return new String("(" + x + "," + y + ")\n");
}
}
public class Maze extends JPanel {
private static final long serialVersionUID = -8300339045454852626L;
private int NUM, width, padding;// width 每个格子的宽度和高度
private Lattice[][] maze;
private int ballX, ballY;
private boolean drawPath = false;
Maze(int m, int wi, int p) {
NUM = m;
width = wi;
padding = p;
maze = new Lattice[NUM][NUM];
for (int i = 0; i <= NUM - 1; i++)
for (int j = 0; j <= NUM - 1; j++)
maze[i][j] = new Lattice(i, j);
createMaze();
setKeyListener();
this.setFocusable(true);
}
private void init() {
for (int i = 0; i <= NUM - 1; i++)
for (int j = 0; j <= NUM - 1; j++) {
maze[i][j].setFather(null);
maze[i][<