【实例简介】
【实例截图】
【核心代码】
package pintu;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
public class pintu extends JFrame implements ActionListener
{
JMenuBar menuBar = new JMenuBar();
JMenu opera = new JMenu("游戏");
JMenu opera1=new JMenu("难度");
JMenuItem itemStart = new JMenuItem("开始");
JMenuItem itemtime = new JMenuItem("倒计时");
JMenuItem itemScore = new JMenuItem("排行");
JMenuItem itemHelp = new JMenuItem("帮助");
JMenuItem itemAbout = new JMenuItem("关于");
JMenuItem itemExit = new JMenuItem("退出");
JMenuItem item33=new JMenuItem("简单 3*3");
JMenuItem item44=new JMenuItem("困难 4*4");
JButton[] btn = new JButton[9];
ImageIcon[] img = new ImageIcon[9];
int[][] map =
{
{0,1,2},
{3,4,5},
{6,7,8}
};
Random r=new Random();
int step=0;
long start=0;
long end=0;
int cishu=0;
public pintu()
{
this.setTitle("拼图游戏");
Container con = this.getContentPane();
con.setLayout(new GridLayout(3, 3, 0, 0));
menuBar.add(opera);
menuBar.add(opera1);
opera.add(itemStart);
opera.add(itemScore);
opera.add(itemHelp);
opera.add(itemAbout);
opera.add(itemtime);
opera.add(itemExit);
opera1.add(item33);
opera1.add(item44);
itemExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, ActionEvent.CTRL_MASK));
item44.addActionListener(this);
itemtime.addActionListener(this);
itemStart.addActionListener(this);
this.setJMenuBar(menuBar);
for (int i = 0; i < 9; i )
{
btn[i] = new JButton();
img[i] = new ImageIcon("images\\img" i ".png");
con.add(btn[i]);
btn[i].addActionListener(this);
}
daluan();
setButtonImage();
itemExit.addActionListener(this);
itemtime.addActionListener(this);
this.setBounds(100, 100, 500, 552);
this.setVisible(true);
start=System.currentTimeMillis();
}
private void daluan()
{
int kongh=2;
int kongl=2;
for (int i = 0; i < 1000; i )
{
int dir=r.nextInt(4);
if (dir==0)
{
if(kongh-1>=0)
{
map[kongh][kongl]=map[kongh-1][kongl];
map[kongh-1][kongl]=8;
kongh--;
}
}
if (dir==1)
{
if(kongh 1<=2)
{
map[kongh][kongl]=map[kongh 1][kongl];
map[kongh 1][kongl]=8;
kongh ;
}
}
if (dir==2)
{
if(kongl-1>=0)
{
map[kongh][kongl]=map[kongh][kongl-1];
map[kongh][kongl-1]=8;
kongl--;
}
}
if (dir==3)
{
if(kongl 1<=2)
{
map[kongh][kongl]=map[kongh][kongl 1];
map[kongh][kongl 1]=8;
kongl ;
}
}
}
}
public void setButtonImage()
{
for (int i = 0; i < 9; i )
{
int hang=i/3;
int lie=i%3;
int value=map[hang][lie];
btn[i].setIcon(img[value]);
btn[i].setSize(160, 160);
btn[i].setBorderPainted(false);
}
}
private void fire(int i)
{
int hang = i / 3;
int lie = i % 3;
if (map[hang][lie] != 8)
{
if (hang - 1 >= 0 && map[hang - 1][lie] == 8)
{
map[hang - 1][lie] = map[hang][lie];
map[hang][lie] = 8;
step ;
}
if (hang 1 <= 2 && map[hang 1][lie] == 8)
{
map[hang 1][lie] = map[hang][lie];
map[hang][lie] = 8;
step ;
}
if (lie - 1 >= 0 && map[hang][lie - 1] == 8)
{
map[hang][lie - 1] = map[hang][lie];
map[hang][lie] = 8;
step ;
}
if (lie 1 <= 2 && map[hang][lie 1] == 8)
{
map[hang][lie 1] = map[hang][lie];
map[hang][lie] = 8;
step ;
}
}
System.out.println("步数" step);
setButtonImage();
}
private boolean IsGameOver()
{
for (int i = 0; i < 3; i )
{
for (int j = 0; j < 3; j )
{
if (map[i][j]!=i*3 j)
{
return false;
}
}
}
return true;
}
@Override
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("退出"))
{
System.exit(0);
}
if (e.getSource()==item44)
{
dispose();
pintu1 pintu=new pintu1();
}
if (e.getSource()==itemStart)
{
if(IsGameOver())
{
step=0;
start=0;
cishu=0;
setButtonImage();
daluan();
}
}
if(e.getSource()==itemtime)
{
jishiqi n=new jishiqi();
}
for (int i=0;i<9;i )
{
if (e.getSource() == btn[i])
{
if (IsGameOver())
{
if (cishu==0)
{
end=System.currentTimeMillis();
}
cishu ;
JOptionPane.showMessageDialog(null, "恭喜你" step "步" (end-start)/1000 "秒通关");
return;
}
fire(i);
}
}
}
}