java智力拼图小游戏

不啰嗦,直接上代码啦!

package gyb;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
// import java.awt.event.MouseMotionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

public class Picture extends JFrame{
    JMenuItem shuffle;
    JMenuItem sort;
    private int sm;
    private final int NUM=9;
    public Picture() {
        init();
    }
    JPanel pane;
    JLabel label;
    private void init() {
        setSize(458,515);
        JMenuBar bar=bar();
        setJMenuBar(bar);
        label=new JLabel("Number:"+sm);
        setLayout(new BorderLayout());
        pane=create();
        add(BorderLayout.NORTH,label);
        add(BorderLayout.CENTER,pane);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);
        setVisible(true);
        validate();
        shuffle.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                    shuffle();
            }
        });
        sort.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                    sort();
            }
        });

    }
    public void addListen(){
        for(int i=0;i<NUM;i++){
            pict[i].addMouseListener(new MouseAdapter() {
                public void mousePressed(MouseEvent e){
                    commute(e.getComponent().getName());
                    System.out.println(isWin());
                    sm++;
                }
            });
        }
    }
    private JMenuBar bar() {
        JMenuBar bar=new JMenuBar();
        JMenu menu=menu();
        bar.add(menu);
        return bar;
    }

    private JMenu menu() {
        JMenu menu=new JMenu("Game");
        shuffle=new JMenuItem("乱序");
        sort=new JMenuItem("排序");
        menu.add(shuffle);
        menu.add(sort);
        return menu;
    }
    Icon image=new ImageIcon(getClass().getResource("image/10.png"));
    JLabel[] pict=new JLabel[9];
    Integer num[]={0,1,2,3,4,5,6,7,8};
    ArrayList<Integer> list=new ArrayList<Integer>();
    ArrayList<ImageIcon> imagelist=new ArrayList<ImageIcon>();
    private JPanel create() {
        JPanel pane=new JPanel();
        pane.setLayout(new GridLayout(3,3));
        getList();
        getImage();
        for(int i=0;i<NUM;i++){
            pict[i]=new JLabel();
            pane.add(pict[i]);
            pict[i].setName(""+i);
            pict[i].setIcon(imagelist.get(i));
        }
        pict[NUM-1].setIcon(image);
        return pane;
    }
    private void commute(String name) {
        int idx=Integer.parseInt(name);
        int id=Integer.parseInt(getBlankName());
        if(((Math.abs(id-idx)==1)&&idx+id!=11&&idx+id!=5)||Math.abs(id-idx)==3){
            Collections.swap(list, idx, id);
        }
        for(int i=0;i<NUM;i++){
            pict[i].setIcon(imagelist.get(list.get(i)));
        }
    }
    public void getList(){
        for(Integer n:num){
            list.add(n);
        }
    }
    public void getImage(){ 
        imagelist.add(new ImageIcon(getClass().getResource("image/1.png")));
        imagelist.add(new ImageIcon(getClass().getResource("image/4.png")));
        imagelist.add(new ImageIcon(getClass().getResource("image/7.png")));
        imagelist.add(new ImageIcon(getClass().getResource("image/2.png")));
        imagelist.add(new ImageIcon(getClass().getResource("image/5.png")));
        imagelist.add(new ImageIcon(getClass().getResource("image/8.png")));
        imagelist.add(new ImageIcon(getClass().getResource("image/3.png")));
        imagelist.add(new ImageIcon(getClass().getResource("image/6.png")));
        imagelist.add(new ImageIcon(getClass().getResource("image/9.png")));
    }
    public void shuffle() {
        Collections.shuffle(list);
        for(int i=0;i<NUM;i++){
            pict[i].setIcon(imagelist.get(list.get(i)));
        }
        addListen();
        sort.setEnabled(false);
    }
    public void sort(){
        Collections.sort(list);
        for(int i=0;i<NUM;i++){
            pict[i].setIcon(imagelist.get(list.get(i)));
        }
        pict[NUM-1].setIcon(image);
    }
    public String getBlankName(){
        String str=null;
        for(int i=0;i<NUM;i++){
            if(list.get(i)==8){
                str=pict[i].getName();
            }
        }
        return str;
    }
    public boolean isWin(){
        int i=0;
        for(;i<NUM;i++){
            if(list.get(i)!=i){
                return false;
            }
        }
        return i==NUM;
    }
    public static void main(String[] args) throws IOException {
        new Picture();
    }
}

乱序前的效果:
java智力拼图小游戏_第1张图片

乱序后的效果:
java智力拼图小游戏_第2张图片

欢迎使用哈!

你可能感兴趣的:(java,游戏)