import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.io.*;
import javax.sound.midi.*;
public class NumBoard extends JFrame//窗体
{
private JPanel panel1,panel2,panel3,panel4,panel5;
private JButton button1,button2,button3,button4,button5;
public JTextArea jTextArea,jTextArea1,jTextArea2;
private JLabel label,label1,label2;
//private Icon sign;
private Board numBoard;
private int steps = 0;
public boolean isbegin = false,ismanufacturer = false,reappear = false;
Icon questionmark = new ImageIcon("images//questionmark.gif");
public MyNode bestnode;
Timer timer = null,dongtaiTimer = null;
final int delay=1000;
final Date startTime=new Date(); //返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。
final SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss"); //以与语言环境有关的方式来格式化和解析日期的具体类
final Action taskPerformer = new AbstractAction()
{ //Action 接口提供 ActionListener 接口的一个有用扩展,以便若干控件访问相同的功能
public void actionPerformed(ActionEvent evt)
{
//显示时间
Date d=new Date(new Date().getTime()-startTime.getTime()-28800000); //8小时的时差
jTextArea1.setText(sdf.format(d));
}
};
int k =0;int[] num,numcheckreappear;
Sound sound = new Sound();//音乐
public NumBoard()
{
super("八数码游戏");
setLocation(200,150);
setSize(558,490);
setResizable(false);
setLayout(null);
sound.startSound();//音乐
//添加文件菜单
JMenu fileMenu = new JMenu("文件(F)");
fileMenu.setMnemonic('F');
//"新建"子菜单
JMenuItem newItem = new JMenuItem("新建(N)");
newItem.setMnemonic('N');
newItem.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
isbegin = true;
ismanufacturer = false;
if(isbegin == false)
numBoard.addListener();
startTime.setTime(new Date().getTime());
timer=new Timer(delay,taskPerformer);
timer.start();
if(dongtaiTimer!=null&&dongtaiTimer.isRunning())
dongtaiTimer.stop();
numBoard.restart();
numBoard.NodeStack.clearStack();
jTextArea.setText(instruction2);
}
}
);
fileMenu.add(newItem);
//"保存"子菜单
JMenuItem saveItem = new JMenuItem("保存(S)");
saveItem.setMnemonic('S');
saveItem.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
JOptionPane.showMessageDialog(null,
"保存失败","失败",JOptionPane.YES_NO_OPTION);
}
}
);
fileMenu.add(saveItem);
//"加载"子菜单
JMenuItem loadItem = new JMenuItem("加载(L)");
loadItem.setMnemonic('L');
loadItem.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
JOptionPane.showMessageDialog(null,
"加载失败","失败",JOptionPane.YES_NO_OPTION);
}
}
);
fileMenu.add(loadItem);
// "开音乐"子菜单
JMenuItem opensoundItem = new JMenuItem("开音乐(O)");
opensoundItem.setMnemonic('O');
opensoundItem.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
if(sound.sign == false)
{
sound.startSound();
sound.sign = true;
}
}
}
);
fileMenu.add(opensoundItem);
// "关音乐"子菜单
JMenuItem closesoundItem = new JMenuItem("关音乐(C)");
closesoundItem.setMnemonic('C');
closesoundItem.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
if(sound.sign == true)
{
sound.stopSound();
sound.sign = false;
}
}
}
);
fileMenu.add(closesoundItem); fileMenu.addSeparator();
// "退出"子菜单
JMenuItem exitItem = new JMenuItem("退出(X)");
exitItem.setMnemonic('X');
exitItem.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
int check = JOptionPane.showConfirmDialog(null,
"你真的要退出当前游戏吗?","询问",JOptionPane.YES_NO_OPTION);
if(check == JOptionPane.YES_OPTION)System.exit(0);
}
}
);
fileMenu.add(exitItem);
//添加高级菜单
JMenu gaojiMenu = new JMenu("高级(G)");
gaojiMenu.setMnemonic('G');
//手工布局
JMenuItem manufacturerItem = new JMenuItem("手工布局(M)");
manufacturerItem.setMnemonic('M');
manufacturerItem.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
if(dongtaiTimer!=null&&dongtaiTimer.isRunning())
dongtaiTimer.stop();
if(timer!=null&&timer.isRunning());
{
startTime.setTime(new Date().getTime());
timer=new Timer(delay,taskPerformer);
timer.stop();
}
jTextArea1.setText("00:00:00");
numBoard.NodeStack.clearStack();
steps=0;
jTextArea2.setText(steps+"");
k =0;
num = new int[9];
numcheckreappear = new int[9];
for(int i = 0;i < numcheckreappear.length;i++)
numcheckreappear[i] = -1;
ismanufacturer = true;
for(int j= 0;j<9;j++)
numBoard.buttons1[j].setIcon(questionmark);
if(isbegin != true)
numBoard.addListener();
isbegin = true;
}
}
);
gaojiMenu.add(manufacturerItem);
//添加帮助菜单
JMenu helpMenu = new JMenu("帮助(H)");
helpMenu.setMnemonic('H');
//"使用说明"子菜单
JMenuItem instructionItem = new JMenuItem("使用说明(I)");
instructionItem.setMnemonic('I');
instructionItem.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
JOptionPane.showMessageDialog(null,instruction1);
}
}
);
helpMenu.add(instructionItem);
//"关于"子菜单
JMenuItem aboutItem = new JMenuItem("关于(A)");
aboutItem.setMnemonic('A');
aboutItem.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
JOptionPane.showMessageDialog(null,about,"关于",
JOptionPane.INFORMATION_MESSAGE);
}
}
);
helpMenu.add(aboutItem);
JMenuBar bar = new JMenuBar();
setJMenuBar(bar);
bar.add(fileMenu);
bar.add(gaojiMenu);
bar.add(helpMenu);
//游戏控件
panel1 = new JPanel();
panel1.setLayout(new CardLayout());
panel1.setBounds(180,10,240,240);
numBoard = new Board(3);
panel1.add(numBoard,"3*3");
add(panel1);
//目标控件
panel2 = new JPanel();
panel2.setLayout(new FlowLayout());
panel2.setBounds(10,6,150,152);
Icon sign = new ImageIcon("images//sign.gif");
label = new JLabel(sign);
panel2.add(label);
add(panel2);
//按钮控件
panel3 = new JPanel();
panel3.setBounds(440,10,100,241);
panel3.setBorder(BorderFactory.createTitledBorder("功能按钮"));
GridLayout container1 = new GridLayout(5,1);
container1.setVgap(25);
panel3.setLayout(container1);
button1 = new JButton("新游戏");
panel3.add(button1);
button1.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
if(button1 == event.getSource())
{
isbegin = true;
ismanufacturer = false;
numBoard.addListener();
startTime.setTime(new Date().getTime());
timer=new Timer(delay,taskPerformer);
timer.start();
if(dongtaiTimer!=null&&dongtaiTimer.isRunning())
dongtaiTimer.stop();
numBoard.restart();
numBoard.NodeStack.clearStack();
jTextArea.setText(instruction2);
}
}
}
);
button2 = new JButton("重新开始");
panel3.add(button2);