您的位置:

java拼图游戏,java拼图游戏代码

本文目录一览:

如何用JAVA编写一个图片的拼图游戏

说说思路:

1 本身他不是一个完整的图片,应该被切分成数组,该数组为拼图成功的标准

2 将该数组复制,然后打散元素的顺序

3 初始化一个二维数组方格矩阵

4 允许用户通过鼠标事件将 备选图案中的某个元素放置到二维数组方格矩阵中,并记载其顺序

5 直到所有的方格被填满,与答案数组的元素顺序比较,给出结果

以上是实现拼图的关键思路,其他情况还要考虑,如允许从方格中撤出图像或者与其他图像调整位置,仅代表个人不成熟的想法,希望对你能有所帮助,祝你好运!

怎么用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;

}

java拼图游戏不成功为什么

从代码和错误提示来看

button 对象只有声明, 并没有赋值,默认是null .用一个null对象去调用方法,那么就会抛出异常.没有进行异常处理,程序就会崩溃

button.addActionListener(new ImgButtonAction());// 出错...因为button为null

解决办法: button对象先赋值(不能为null)再使用

建议你写代码,添加必要的注释,对每个方法的说明, 以及对每个比较难一眼看懂的算法进行说明.,会比较好.

以前也写过一个拼图,比较简单.思路跟你可能差不多:

1:用按钮去显示图片的一部分.并且每个按钮都有个value属性,从1,2,3,....9,0

(value为0按钮在最后面,并不显示图片) .想要胜利,就需要图片按钮的value属性能满足下面的二维数组

int[][] win = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 0 } };

2: 点击图片按钮,判断是否可以交换位置,如果旁边是value为0的按钮,那么可以交换位置,每次交换后,记得判断是否胜利

3: 打乱的图片按钮,不一定能求解, 如果不能求解,那么重新打乱

效果图大概是