怎么用Java实现拼图游戏,求思维和代码
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.GroupLayout.Alignment;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Jigsaw implements ActionListener {
private JFrame frame;
private JPanel panel;
private JButtonx[] button;
private JPanel panel_right;
private int level = 4;
private JMenuBar menubar;
private ImageIcon imageicon;
private Image image;
private BufferedImage bufferedimage;
private BufferedImage[] image_button;
private JLabel label;
private JPanel panel_left;
private ImageIcon[] icon_button;
private JMenu menu1;
private JMenuItem menuitem1_1;
private JMenu menu2;
private JCheckBoxMenuItem menuitem2_1;
private JCheckBoxMenuItem menuitem2_2;
private JCheckBoxMenuItem menuitem2_3;
private int x=400;
private int y=400;
private JMenuItem menuitem1_2;
private JFileChooser jfc;
private String name="src/pic/1.jpg"; //起始图片位置,请自行设置
private JPanel panel_menu;
private JLabel label1_1;
private JLabel label1_2;
private JLabel label1_3;
private JLabel label2_1;
private JLabel label2_2;
private JLabel label2_3;
public void reset() {
frame = new JFrame("拼图");
panel = new JPanel();
panel_left = new JPanel();
panel_right = new JPanel();
panel.setLayout(new GridLayout(0, 2));
menubar = new JMenuBar();
menubar.setLayout(new BorderLayout());
panel_menu=new JPanel();
panel_menu.setLayout(null);
menu1 = new JMenu("开始游戏");
menuitem1_1=new JMenuItem("重新开始");
menuitem1_2=new JMenuItem("选择图片");
menu2=new JMenu("等级选择");
menuitem2_1=new JCheckBoxMenuItem("低级(3)");
menuitem2_2=new JCheckBoxMenuItem("高级(4)");
menuitem2_3=new JCheckBoxMenuItem("自定义");
imageicon = new ImageIcon(name);
image = imageicon.getImage();
bufferedimage=new BufferedImage(252, 405, BufferedImage.TYPE_INT_RGB);
bufferedimage.getGraphics().drawImage(image,0,0,252,405,null);
imageicon.setImage(bufferedimage);
label=new JLabel(imageicon);
panel_left.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
panel_left.add(label);
panel.add(panel_left);
jfc=new JFileChooser();
label1_1=new JLabel("您已经走了");
label1_1.setBounds(30, 0, 80, 25);
label1_2=new JLabel("0");
label1_2.setForeground(Color.RED);
label1_2.setBounds(120, 0, 40, 25);
label1_2.setFont(new Font("黑体",Font.BOLD,20));
label1_3=new JLabel("步");
label1_3.setBounds(160, 0, 30, 25);
label2_1=new JLabel("时间");
label2_1.setBounds(220, 0, 50, 25);
label2_2=new JLabel("0");
label2_2.setForeground(Color.RED);
label2_2.setBounds(280, 0, 40, 25);
label2_2.setFont(new Font("黑体",Font.BOLD,20));
label2_3=new JLabel("秒");
label2_3.setBounds(330, 0, 30, 25);
panel_right.setLayout(new GridLayout(level, level));
button = new JButtonx[level * level];
image_button=new BufferedImage[level*level];
for (int i = 0; i < level*level/*-1*/; i++) {
image_button[i]=new BufferedImage(252/level, 405/level, BufferedImage.TYPE_INT_RGB);
image_button[i].getGraphics().drawImage(bufferedimage, 0,0,252/level,405/level,(i%level)*(252/level), (i/level)*(405/level), (i%level+1)*(252/level), (i/level+1)*(405/level),null);
}
icon_button=new ImageIcon[level * level];
for (int i = 0; i < level * level; i++) {
button[i] = new JButtonx();
icon_button[i]=new ImageIcon(image_button[i],i+"");
button[i].setIcon(icon_button[i]);
button[i].num=i;
button[i].addActionListener(this);
panel_right.add(button[i]);
}
button[level*level-1].setVisible(false);
panel.add(panel_right);
menuitem1_1.addActionListener(this);
menuitem1_2.addActionListener(this);
menuitem2_1.addActionListener(this);
menuitem2_2.addActionListener(this);
menuitem2_3.addActionListener(this);
menu1.add(menuitem1_1);
menu1.add(menuitem1_2);
menu2.add(menuitem2_1);
menu2.add(menuitem2_2);
menu2.add(menuitem2_3);
menubar.add(menu1,BorderLayout.WEST);
menubar.add(menu2,BorderLayout.EAST);
menubar.add(panel_menu);
panel_menu.add(label1_1);
panel_menu.add(label1_2);
panel_menu.add(label1_3);
panel_menu.add(label2_1);
panel_menu.add(label2_2);
panel_menu.add(label2_3);
frame.setJMenuBar(menubar);
frame.add(panel);
frame.setBounds(x, y, 508+4, 459+5);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==menuitem1_1)
{
for (int i = 0; i < level*level; i++)
button[i].setVisible(true);
int a[]=new int [level*level];
int index=level*level;
for (int i = 0; i < level*level; i++) {
a[i]=i;
}
for (int i = 0; i < level*level; i++) {
int x=(int)(Math.random()*index);
int temp=a[x];
a[x]=a[index-1];
a[index-1]=temp;
button[i].setIcon(icon_button[a[index-1]]);
if(a[index-1]==level*level-1)button[i].setVisible(false);
index--;
}
win();
}
else if(e.getSource()==menuitem1_2){if(jfc.showOpenDialog(panel)==0)name=jfc.getSelectedFile().getAbsolutePath();frame.setVisible(false);x=frame.getX();y=frame.getY();reset();}
else if(e.getSource()==menuitem2_1){level=4;frame.setVisible(false);x=frame.getX();y=frame.getY();reset();}
else if(e.getSource()==menuitem2_2){level=5;frame.setVisible(false);x=frame.getX();y=frame.getY();reset();}
else if(e.getSource()==menuitem2_3)try {level=Integer.parseInt(JOptionPane.showInputDialog(frame,"请输入行数:"));frame.setVisible(false);x=frame.getX();y=frame.getY();reset();} catch (Exception e2) {}
else
{
JButtonx but=(JButtonx)e.getSource();
if(but.isVisible())
{
if(but.num-level>=0)judgemengt(but.num,but.num-level);
if(but.num+level<=level*level-1)judgemengt(but.num,but.num+level);
if(but.num%level-1>=0)judgemengt(but.num,but.num-1);
if(but.num%level+1<=level-1)judgemengt(but.num,but.num+1);
}
win();
}
}
public void judgemengt(int i,int j)
{
if(button[j].isVisible()==false)
{
Icon icontemp_button=button[j].getIcon();
button[j].setIcon(button[i].getIcon());
button[i].setIcon(icontemp_button);
button[j].setVisible(true);
button[i].setVisible(false);
}
}
public void win()
{
int i;
for (i = 0; i < level*level-1; i++) {
if(button[i].num!=Integer.parseInt(button[i].getIcon().toString()))break;
}
if(i==level*level-1)
{
JOptionPane.showMessageDialog(frame,"YOU WIN");
}
}
public static void main(String[] args) {
Jigsaw a=new Jigsaw();
a.reset();
}
}
class JButtonx extends JButton
{
int num;
}