package shixi3;
import java.awt.*;
import javax.swing.JFrame;
public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame();//新建窗口
int width = Toolkit.getDefaultToolkit().getScreenSize().width;// 取得屏幕宽度
int height = Toolkit.getDefaultToolkit().getScreenSize().height;// 取得屏幕高度
frame.setSize(600, 600);// 设置窗体大小
frame.setLocation((width - 600) / 2, (height - 600) / 2);// 设置窗体出现大小
frame.setResizable(false);// 设置窗体大小不可变
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置窗体关闭方式
frame.add(new Panel());
frame.setFocusable(true);//为屏幕添加焦点
frame.setVisible(true);// 设置窗体可视
frame.requestFocus();//获取焦点
}
}
package shixi3;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.*;
public class Panel extends JPanel implements MouseListener, KeyListener {
Maze M = new Maze();//定义一个Maze类对象,生成地图
AStart A = new AStart();//定义一个AStart类,画出迷宫路径
ArrayList<Integer[]> arrayList = new ArrayList();
private JPanel jp = new JPanel();
private JButton answer = new JButton("画出路径");
private JButton hide = new JButton("隐藏路径");
private JButton reset = new JButton("重置地图");
private JButton exit = new JButton("退出游戏");
private JButton start = new JButton("开始游戏");
int myx = 1;// 定义角色横坐标并初始化
int myy = 1;// 定义角色纵坐标
int endx;// 定义终点横纵坐标
int endy;
boolean isStarted = false;
boolean isVictory = false;
boolean ans = false;// 用于显示路径
//添加素材
BackGround backGround = new BackGround();
ImageIcon a = backGround.bj;
ImageIcon b = backGround.wall;
ImageIcon c = backGround.my;
ImageIcon d = backGround.sl;
ImageIcon e = backGround.foot;
//画按钮
public Panel() {
this.setName("迷宫");// 设置标题
this.setLayout(null);
answer.setBounds(470, 130, 90, 30);
answer.setForeground(Color.yellow);
answer.setBackground(Color.pink);
hide.setBounds(470, 210, 90, 30);
hide.setForeground(Color.yellow);
hide.setBackground(Color.pink);
reset.setBounds(470, 290, 90, 30);
reset.setForeground(Color.yellow);
reset.setBackground(Color.pink);
exit.setBounds(470, 370, 90, 30);
exit.setForeground(Color.yellow);
exit.setBackground(Color.pink);
start.setBounds(470, 450, 90, 30);
start.setForeground(Color.yellow);
start.setBackground(Color.pink);
answer.addMouseListener(this);
hide.addMouseListener(this);
reset.addMouseListener(this);
exit.addMouseListener(this);
start.addMouseListener(this);
start.addKeyListener(this);
this.add(jp);
this.add(start);
this.add(answer);
this.add(hide);
this.add(reset);
this.add(exit);
}
// 画组件
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillRect(20, 80, 420, 420);
g.drawImage(a.getImage(), 0, 0, this);
g.setColor(Color.yellow);
g.setFont(new Font("楷体", Font.BOLD, 30));
g.drawString("走迷宫", 200, 50);
// 画迷宫
for (int i = 0; i < M.LabId.length; i++) { //LabId:存放迷宫的数组,迷宫单元数组
for (int j = 0; j < M.LabId[0]