public int iconbuttonx;
public int iconbuttony;
public int upiconbuttonx;
public int upiconbuttony;
for(int i=0;i<39;i++)
icon[i] = new ImageIcon(getImage("images/"+(i+1)+".gif"));
for(int i=0;i<row*clomu;i++){
iconbutton[i] = new JButton();
iconbutton[i].setBorder(BorderFactory.createRaisedBevelBorder());
}
int n=0; //图片的位置生成
for(int j=0;j<clomu;j++){
for(int i=0;i<row;i++){
iconbutton[n].setBounds(new Rectangle(5+(35*i),57+(38*j),31,34));
panel.add(iconbutton[n]);
iconbutton[n].addActionListener(this); //对应的iconbutton进行监听
iconbutton[n].setVisible(false);
iconbutton[n].setOpaque(false);
n++;
}
}
start.addActionListener(new ActionListener(){ //开始按钮的设置
public void actionPerformed(ActionEvent e)
{
refreshiconnum=map.iconnum;
System.out.println(refreshiconnum);
isEmp=true;
yanshi(1000);
top10.setEnabled(false); //设置该按钮的不可用
xianshi.setText("图片个数:"+map.iconnum+"你应该在"+map.iconnum*2+"秒内完成!!");
for(int i=0;i<row*clomu;i++){
iconbutton[i].setVisible(false); //隐藏当前窗体
iconbutton[i].setBorder(BorderFactory.createRaisedBevelBorder());
}
map.chushihua(); //map设置
map.buttonblack(map.iconnum);
map.iconblack(map.iconnum);
for(int i=0;i<map.iconnum;i++){
iconbutton[map.buttonblack[i]].setVisible(true); //显示当前窗体
iconbutton[map.buttonblack[i]].setIcon(icon[map.iconblack[i]]);//选择默认图标并进行相应位置赋予
}
chushihuablack();
score=0; //时间分数的显示设置
scorelb.setText("分数:"+score);
Time time = new Time();
time.start(time);
isEmp=false;
}});
//为开始游戏进行操作的提示
refresh.addActionListener(new ActionListener(){ //重置进行监听
public void actionPerformed(ActionEvent arg0) {
if(refreshiconnum==0){
JOptionPane.showMessageDialog(null,"你还没开始游戏呢!");
}
else{
sound.refresh();
kongzhi=0;
for(int i=0;i<row*clomu;i++){ //再次随进分配图片位置
iconbutton[i].setVisible(false);
iconbutton[i].setBorder(BorderFactory.createRaisedBevelBorder());
}
map.chushihua();
map.buttonblack(refreshiconnum);
map.iconblack(refreshiconnum);
System.out.println(refreshiconnum);
for(int i=0;i<refreshiconnum;i++){
iconbutton[map.buttonblack[i]].setVisible(true);
iconbutton[map.buttonblack[i]].setIcon(icon[map.iconblack[i]]);
}
chushihuablack();
}
}});
//用监听机制对错误操作进行提示
hint.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(!tishi.chazhao(false,true))
JOptionPane.showMessageDialog(null,"不能提示了,请刷新!!");
}});
bomb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(tishi.chazhao(true,false))
sound.bomb();
else
JOptionPane.showMessageDialog(null,"炸弹用不了,请刷新!!");
}});
demo.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(refreshiconnum==0){
JOptionPane.showMessageDialog(null,"请先开始游戏,在点演示!!");
}
else
tishi.start(tishi);
}});
help.addActionListener(this);
setup.addActionListener(this);
about.addActionListener(this);
top10.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String arg[]){
JFrame.setDefaultLookAndFeelDecorated(true); //显示java风格引用两种语法进行相同的功能
JDialog dialog = new JDialog();
dialog.setUndecorated(true);
dialog.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
MainFrame main = new MainFrame();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); //规格设置(主框架跟从做空间的比例调和)
Dimension mainSize = main.getSize();
main.setLocation((screenSize.width - mainSize.width) / 2,(screenSize.height - mainSize.height) / 2);
main.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
chushihuablack();
if(e.getSource()==help){ //监听得到帮助的情况
HelpDialog helpdialog = new HelpDialog(this,"帮助");
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension mainSize = helpdialog.getSize();
helpdialog.setLocation((screenSize.width - mainSize.width) / 2,(screenSize.height - mainSize.height) / 2);
helpdialog.setResizable(false);
helpdialog.setVisible(true);
}
else if(e.getSource()==setup){ //监听得到设置的情况
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension mainSize = setupdialog.getSize();
setupdialog.setLocation((screenSize.width - mainSize.width) / 2,(screenSize.height - mainSize.height) / 2);
setupdialog.setResizable(false);
setupdialog.setVisible(true);
}
else if(e.getSource()==about){ //监听得到关于的的情况
AboutDialog aboutdialog = new AboutDialog(this,"关于");
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension mainSize = aboutdialog.getSize();
aboutdialog.setLocation((screenSize.width - mainSize.width) / 2,(screenSize.height - mainSize.height) / 2);
aboutdialog.setResizable(false);
aboutdialog.setVisible(true);
}
else if(e.getSource()==top10){ //监听得到上传的信息
Top10 top10dialog = new Top10(this,"top10");
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension mainSize = top10dialog.getSize();
top10dialog.setLocation((screenSize.width - mainSize.width) / 2, (screenSize.height - mainSize.height) / 2);
top10dialog.setResizable(false);
top10dialog.setVisible(true);
}
else{
sound.start();
if(kongzhi==0){
buttonxinxi1(e.getSource());
kongzhi=1;
return;
}
if(kongzhi==1){
buttonxinxi2(e.getSource());
if(Compare1(upiconbuttonx,upiconbuttony,iconbuttonx,iconbuttony)){ //坐标
xianshi.setText("两次选择的点相同,取消选择!!");
iconbutton[map.buttonblack[n1]].setBorder(BorderFactory.createRaisedBevelBorder());//设置边界
upiconbuttonx=-1;
upiconbuttony=-1;
iconbuttonx=-1;
iconbuttony=-1;
kongzhi=0;
return; //再次循环
}
else{
if(Compare2(uptupianzhi,tupianzhi)){
iconbutton[map.buttonblack[n1]].setVisible(false);
iconbutton[map.buttonblack[n2]].setVisible(false);
sound.earse();
map.iconblack[n1]=-1;
map.iconblack[n2]=-1;
upiconbuttonx=0;
upiconbuttony=0;
iconbuttonx=0;
iconbuttony=0;
refreshiconnum-=2;
chushihuablack();
score=score+2; //选择正确一组得2分
scorelb.setText("分数:"+score); //显示分数
kongzhi=0;
if(EndGame()){
xianshi.setText("点击 GO TO TOP10 查看你的排名!!!");
isEmp=true;
top10.setEnabled(true); //启发
}
return;
}
else {
xianshi.setText("这次选择的点与上个点无解!改变选择为当前点!!");//在显示框中显示相应内容
score=score-1;
scorelb.setText("分数:"+score); //选择错误减掉一分并且显示当前分数
iconbutton[map.buttonblack[n1]].setBorder(BorderFactory.createRaisedBevelBorder());
if(n1!=n2){ //如果选择失败选择点停留在第二次选择处
iconbutton[map.buttonblack[n2]].setBorder(BorderFactory.createLineBorder(Color.RED,2));
n1=n2;
upiconbuttonx=iconbuttonx;
upiconbuttony=iconbuttony;
uptupianzhi=tupianzhi;
} //选择失败
kongzhi=1; //还kongzhi的值重新等待用户选择
}
}
}
}
}
public static void chushihuablack(){
for(int j=0;j<clomu;j++){ //初始化清空
for(int i=0;i<row;i++){
black[i][j]=0;
}
}
int n=0;
for(int j=0;j<clomu;j++){
for(int i=0;i<row;i++)
{
if(iconbutton[n].isVisible()) //isVisible只是元件是否可见
black[i][j]=1;
n++;
}
}
}
public void buttonxinxi1(Object object){
int i;
for(i=0;i<map.iconnum;i++){
if(object==iconbutton[map.buttonblack[i]]){
n1=i;
upiconbuttony=map.buttonblack[i]/17+1;
upiconbuttonx=map.buttonblack[i]-(upiconbuttony-1)*17+1;
uptupianzhi=map.iconblack[i]+1;
iconbutton[map.buttonblack[i]].setBorder(BorderFactory.createLineBorder(Color.RED,2));
xianshi.setText("当前点的坐标:"+upiconbuttonx+","+upiconbuttony+"值为:"+uptupianzhi+"位移:"+(map.buttonblack[n1]+1));
}
}
}
//消除
public void buttonxinxi2(Object object){
int i;
for(i=0;i<map.iconnum;i++){
if(object==iconbutton[map.buttonblack[i]]){
n2=i;
iconbuttony=map.buttonblack[i]/17+1;
iconbuttonx=map.buttonblack[i]-(iconbuttony-1)*17+1;
tupianzhi=map.iconblack[i]+1;
iconbutton[map.buttonblack[i]].setBorder(BorderFactory.createLineBorder(Color.RED,2));
xianshi.setText("当前点的坐标:"+iconbuttonx+","+iconbuttony+"值为:"+tupianzhi+"位移:"+(map.buttonblack[n2]+1));
}
}
}
public boolean Compare1(int upiconbuttonx,int upiconbuttony,int iconbuttonx,int iconbuttony){
if(upiconbuttonx==iconbuttonx&&iconbuttony==upiconbuttony)
return(true);
else return(false);
}
public boolean Compare2(int uptupianzhi,int tupianzhi){
int direction=0;
if(uptupianzhi==tupianzhi){
direction=map.diretion(upiconbuttonx,iconbuttonx,upiconbuttony,iconbuttony);
if(map.det(direction,upiconbuttonx,iconbuttonx,upiconbuttony,iconbuttony,false)){
xianshi.setText("两点有解,消除成功!");
return(true);
}
else return(false);
}
else
return(false);
}
public static boolean EndGame(){
int isDead=0;
for(int i=0;i<row*clomu;i++){
if(iconbutton[i].isVisible()){
isDead=1;
break;
}
}
if(isDead==1)return(false);
else return(true);
}
public void yanshi(int shijian){
try {
Robot ne=new Robot();
ne.delay(shijian);
} catch (AWTException en) {
en.printStackTrace();
}
}
Image getImage(String filename) {
URLClassLoader urlLoader = (URLClassLoader)this.getClass().
getClassLoader();
URL url = null;
Image image = null;
url = urlLoader.findResource(filename);
image = Toolkit.getDefaultToolkit().getImage(url);
MediaTracker mediatracker = new MediaTracker(this);
try {
mediatracker.addImage(image, 0);
mediatracker.waitForID(0);
}
catch (InterruptedException _ex) {
image = null;
}
if (mediatracker.isErrorID(0)) {
image = null;
}
return image;
}
}
class Time extends Thread{
public Time(){
MainFrame.map.time=0;
}
public void run() {
while((!MainFrame.isEmp)){
MainFrame.timelb.setText("时间:"+MainFrame.map.time);
yanshi(1000);
MainFrame.map.time++;
MainFrame.timelb.setText("时间:"+MainFrame.map.time);
}
if(MainFrame.isEmp){
if(MainFrame.map.time>=MainFrame.map.iconnum*2){
MainFrame.score=MainFrame.score-2*(MainFrame.map.time-(MainFrame.map.iconnum*2));
}else {
MainFrame.score=MainFrame.score+2*((MainFrame.map.iconnum*2)-MainFrame.map.time);
}
MainFrame.scorelb.setText("分数:"+MainFrame.score);
}
}
public void start(Time thread){
MainFrame.map.time=0;
thread.start();
}
public void yanshi(int shijian){
try {
Robot ne=new Robot();
ne.delay(shijian);
} catch (AWTException en) {
en.printStackTrace();
}
}
}