java swing版拼图游戏

前两天,闲来无事 就写了个java拼图的游戏,只是半成品,(写完玩了下感觉太无聊了,还是算了写了:))根据需要时很容易扩展的。虽然是半成品,也希望对初学者有个帮助。代码如下:

容器类:

 

package cn.xy.imgspell; 
/* *author:a276202460 *crete date:2009-6-17 */

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FileDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class SpellFrame extends JFrame implements ActionListener {
    /** * */
    private static final long serialVersionUID = -274514186974401279L;
    private JMenu menu;
    private JMenuItem item;
    private Imgpanel panel;
    public static final String OPEN_FILE = "openimg";
    public static final String APP_EXIT = "exit";

    public SpellFrame() {
        super("拼图");
        setSize(500, 500);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        initdisplay();
        initlistener();
    }

    public void initlistener() {
        this.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent arg0) {
                panel.registpanelsize();
            }
        });
    }

    public void initdisplay() {
        JMenuBar menubar = new JMenuBar();
        this.setJMenuBar(menubar);
        menu = new JMenu("文件");
        menubar.add(menu);
        item = new JMenuItem("打开图片");
        item.setActionCommand(OPEN_FILE);
        item.addActionListener(this);
        menu.add(item);
        item = new JMenuItem("退出");
        item.setActionCommand(APP_EXIT);
        item.addActionListener(this);
        menu.add(item);
        menu.setInheritsPopupMenu(true);
        Container c = this.getContentPane();
        c.setLayout(new BorderLayout());
        panel = new Imgpanel();
        c.add(panel, BorderLayout.CENTER);
    }

    public void loaderimg() {
        FileDialog fd = new FileDialog((JFrame) null, "选择图片", FileDialog.LOAD);
        fd.setVisible(true);
        String filepath = fd.getDirectory() + fd.getFile();
        if (fd.getFile() != null && !fd.getFile().equals("")) {
            panel.setImgpath(filepath);
        }
    }

    public void actionPerformed(ActionEvent e) {
        String comm = e.getActionCommand();
        if (comm.equals(OPEN_FILE)) {
            loaderimg();
        } else if (comm.equals(APP_EXIT)) {
            System.exit(0);
        }
    }

    public static void main(String[] s) {
        SpellFrame sf = new SpellFrame();
        sf.setVisible(true);
        sf.panel.registpanelsize();
    }
}

 

 图片面板类:所有的操作事件都在此类中进行定义

 

package cn.xy.imgspell;
/* *author:a276202460 *crete date:2009-6-17 */

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Panel;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.net.URL;
import java.util.List;
import javax.swing.JPanel;

import cn.xy.imgspell.gamefactory.GameFactory;
import cn.xy.imgspell.gamefactory.Gameconsole;
import cn.xy.imgspell.imgloader.ImageLoader;

public class Imgpanel extends JPanel {
    private String imgpath;

    public Imgpanel() {
        initlistener();
    }

    public void paint(Graphics g) {
        super.paint(g);
        if (imgpath != null) {
            for (int i = 0; i < GameFactory.ycount; i++) {
                for (int j = 0; j < GameFactory.xcount; j++) {
                    if (GameFactory.pointsimage[i][j] != -1) {
                        g.drawImage((Image) GameFactory.imagelist.get(GameFactory.pointsimage[i][j]), GameFactory.gaphblock[i][j].x, GameFactory.gaphblock[i][j].y, GameFactory.gaphblock[i][j].width, GameFactory.gaphblock[i][j].height, this);
                    }
                }
            }
            if (Gameconsole.selectxindex != -1 && Gameconsole.selectyindex != -1) {
                g.drawRect(GameFactory.gaphblock[Gameconsole.selectyindex][Gameconsole.selectxindex].x, GameFactory.gaphblock[Gameconsole.selectyindex][Gameconsole.selectxindex].y, GameFactory.gaphblock[Gameconsole.selectyindex][Gameconsole.selectxindex].width, GameFactory.gaphblock[Gameconsole.selectyindex][Gameconsole.selectxindex].height);
            }
        }
    }

    public String getImgpath() {
        return imgpath;
    }

    public void setImgpath(String imgpath) {
        this.imgpath = imgpath;
        registpanelsize();
        GameFactory.loadimage(imgpath);
        Gameconsole.dispersesubimg();
        repaint();
    }

    public void registpanelsize() {
        GameFactory.panelwidth = getSize().width;
        GameFactory.panelheight = getSize().height;
        if (GameFactory.image != null) {
            GameFactory.initblocks();
            repaint();
        }
    }

    public void initlistener() {
        MouseAda ma = new MouseAda();
        this.addMouseListener(ma);
        MouseMv mv = new MouseMv();
        this.addMouseMotionListener(mv);
    }

    class MouseAda extends MouseAdapter {
        @Override
        public void mouseClicked(MouseEvent e) {
            subimgmv(e);
        }
    }

    public void subimgmv(MouseEvent e) {
        if (GameFactory.gaphblock == null) {
            return;
        }
        if (Gameconsole.movesubimg(e.getPoint())) {
            if (Gameconsole.issuccess()) {
                GameFactory.pointsimage[GameFactory.ycount - 1][GameFactory.xcount - 1] = GameFactory.ycount * GameFactory.xcount - 1;
            }
            repaint();
        }
    }

    class MouseMv extends MouseMotionAdapter {
        @Override
        public void mouseMoved(MouseEvent e) {
            Point p = e.getPoint();
            if (GameFactory.gaphblock == null) {
                return;
            }
            if (Gameconsole.selectsubimg(p)) {
                repaint();
            }
        }
    }
}

拼图工厂类:

package cn.xy.imgspell.gamefactory; /* *author:a276202460 *crete date:2009-6-17 */

import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;

public class GameFactory {
    public static Image image = null;
    public static int[][] pointsimage;
    public static Rectangle[][] gaphblock;
    public static int xcount = 3;
    public static int ycount = 3;
    public static int imagewidth = -1;
    public static int imageheight = -1;
    public static int panelwidth = -1;
    public static int panelheight = -1;
    public static List imagelist = new ArrayList();
    public static int gap = 1;

    public static void registerImage(Image image) {
        GameFactory.image = image;
    }

    public static void registerApparr(int[][] pointsimage) {
        GameFactory.pointsimage = pointsimage;
    }

    public static void registGraphicsBlocks(Rectangle[][] gaphblock) {
        GameFactory.gaphblock = gaphblock;
    }

    public static void setdefaultselect() {
        for (int i = 0; i < pointsimage.length; i++) {
            for (int j = 0; j < pointsimage[i].length; j++) {
                if (pointsimage[i][j] == -1) {
                    if ((i - 1) >= 0) {
                        Gameconsole.registselectblock(j, i - 1);
                    } else if ((j - 1) >= 0) {
                        Gameconsole.registselectblock(j - 1, i);
                    } else if ((i + 1) < pointsimage.length) {
                        Gameconsole.registselectblock(j, i + 1);
                    } else {
                        Gameconsole.registselectblock(j + 1, i);
                    }
                }
            }
        }
    }

    public static void diviedimage(Image img) {
        imagewidth = ((BufferedImage) img).getWidth();
        imageheight = ((BufferedImage) img).getHeight();
        xcount = 3;
        ycount = 3;
        int xsp = imagewidth / xcount;
        int ysp = imageheight / ycount;
        int[][] pointsimage = new int[ycount][xcount];
        int index = 0;
        for (int i = 0; i < ycount; i++) {
            for (int j = 0; j < xcount; j++) {
                imagelist.add(((BufferedImage) img).getSubimage(xsp * j, ysp * i, xsp, ysp));
                pointsimage[i][j] = index;
                index++;
            }
        }
        registerApparr(pointsimage);
        initblocks();
        pointsimage[ycount - 1][xcount - 1] = -1;
    }

    public static void initblocks() {
        int xpanelsp = panelwidth / xcount;
        int ypanelsp = panelheight / ycount;
        Rectangle[][] gaphblock = new Rectangle[ycount][xcount];
        for (int i = 0; i < ycount; i++) {
            for (int j = 0; j < xcount; j++) {
                gaphblock[i][j] = new Rectangle(xpanelsp * j, ypanelsp * i, xpanelsp - 1, ypanelsp - 1);
            }
        }
        registGraphicsBlocks(gaphblock);
    }

    public static void loadimage(String imagepath) {
        try {
            image = ImageIO.read(new File(imagepath));
            registerImage(image);
            imagelist.clear();
            diviedimage(image);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 拼图控制台:

package cn.xy.imgspell.gamefactory; /* *author:a276202460 *crete date:2009-6-17 */

import java.awt.Point;

import cn.xy.imgspell.imgloader.ImageLoader;

public class Gameconsole {
    public static int opimgexindex = -1;
    public static int opimgeyindex = -1;
    public static int selectxindex = -1;
    public static int selectyindex = -1;
    public static final int MOVE_UP = 0;
    public static final int MOVE_LEFT = 1;
    public static final int MOVE_DOWN = 2;
    public static final int MOVE_RIGHT = 3;

    public static void registselectblock(int x, int y) {
        selectxindex = x;
        selectyindex = y;
    }

    public static void registopblock(int x, int y) {
        opimgexindex = x;
        opimgeyindex = y;
    }

    //打乱图片顺序

    public static void dispersesubimg() {
        int x;
        int y;
        int ox, oy;
        for (int i = 0; i < 50; i++) {
            x = (int) (Math.random() * GameFactory.xcount);
            y = (int) (Math.random() * GameFactory.ycount);
            ox = (int) (Math.random() * GameFactory.xcount);
            oy = (int) (Math.random() * GameFactory.ycount);
            if (ox != x || oy != y) {
                wrapimg(ox, oy, x, y);
            }
        }
        if (issuccess()) {
            dispersesubimg();
        }
    }

   // 选择图片移动

    public static boolean moveselectblock(int position) {
        if (position == MOVE_UP) {
            return moveselectup();
        } else if (position == MOVE_LEFT) {
            return moveselectleft();
        } else if (position == MOVE_DOWN) {
            return moveselectdown();
        } else if (position == MOVE_RIGHT) {
            return moveselectright();
        }
        return false;
    }

    public static boolean moveselectleft() {
        if (opimgexindex - 1 >= 0) {
            opimgexindex = opimgexindex - 1;
            return true;
        }
        return false;
    }

    public static boolean moveselectright() {
        if (opimgexindex + 1 < GameFactory.xcount) {
            opimgexindex = opimgexindex + 1;
            return true;
        }
        return false;
    }

    public static boolean moveselectup() {
        if (opimgeyindex - 1 >= 0) {
            opimgeyindex = opimgeyindex - 1;
            return true;
        }
        return false;
    }

    public static boolean moveselectdown() {
        if (opimgeyindex + 1 < GameFactory.ycount) {
            opimgeyindex = opimgeyindex + 1;
            return true;
        }
        return false;
    }

    public static boolean movesubimg(int position) {
        if (position == MOVE_UP) {
            return moveimgup();
        } else if (position == MOVE_LEFT) {
            return moveimgleft();
        } else if (position == MOVE_DOWN) {
            return moveimgdown();
        } else if (position == MOVE_RIGHT) {
            return moveimgright();
        }
        return false;
    }

    public static boolean movesubimg(Point p) {
        opimgeyindex = selectyindex;
        opimgexindex = selectxindex;
        if (opimgeyindex == -1 || opimgexindex == -1) {
            int[] points = initpoint(p);
            opimgexindex = points[0];
            opimgeyindex = points[1];
            if (opimgeyindex == -1 || opimgexindex == -1) {
                return false;
            }
        }
        if (GameFactory.pointsimage[opimgeyindex][opimgexindex] == -1) {
            return false;
        }
        boolean rs = movesubimg(MOVE_UP) ? true : movesubimg(MOVE_LEFT) ? true : movesubimg(MOVE_DOWN) ? true : movesubimg(MOVE_RIGHT);
        return rs;
    }

    public static void wrapimg(int oldx, int oldy, int x, int y) {
        int tmp = GameFactory.pointsimage[oldy][oldx];
        GameFactory.pointsimage[oldy][oldx] = GameFactory.pointsimage[y][x];
        GameFactory.pointsimage[y][x] = tmp;
    }

    public static boolean moveimgleft() {
        if (opimgexindex - 1 >= 0 && GameFactory.pointsimage[opimgeyindex][opimgexindex - 1] == -1) {
            wrapimg(opimgexindex, opimgeyindex, opimgexindex - 1, opimgeyindex);
            opimgexindex = opimgexindex - 1;
            return true;
        }
        return false;
    }

    public static boolean moveimgright() {
        if (opimgexindex + 1 < GameFactory.xcount && GameFactory.pointsimage[opimgeyindex][opimgexindex + 1] == -1) {
            wrapimg(opimgexindex, opimgeyindex, opimgexindex + 1, opimgeyindex);
            opimgexindex = opimgexindex + 1;
            return true;
        }
        return false;
    }

    public static boolean moveimgup() {
        if (opimgeyindex - 1 >= 0 && GameFactory.pointsimage[opimgeyindex - 1][opimgexindex] == -1) {
            wrapimg(opimgexindex, opimgeyindex, opimgexindex, opimgeyindex - 1);
            opimgeyindex = opimgeyindex - 1;
            return true;
        }
        return false;
    }

    public static boolean moveimgdown() {
        if (opimgeyindex + 1 < GameFactory.ycount && GameFactory.pointsimage[opimgeyindex + 1][opimgexindex] == -1) {
            wrapimg(opimgexindex, opimgeyindex, opimgexindex, opimgeyindex + 1);
            opimgeyindex = opimgeyindex + 1;
            return true;
        }
        return false;
    }

    public static void setdefaultselect() {
    }

    public static boolean issuccess() {
        boolean rs = true;
        int i = 0;
        for (int yindex = 0; yindex < GameFactory.pointsimage.length; yindex++) {
            for (int xindex = 0; xindex < GameFactory.pointsimage[yindex].length; xindex++) {
                if (GameFactory.pointsimage[yindex][xindex] != i) {
                    if (yindex != GameFactory.ycount - 1 || xindex != GameFactory.xcount - 1) return false;
                }
                i++;
            }
        }
        return rs;
    }

    public static boolean selectsubimg(Point p) {
        int[] points = initpoint(p);
        if (selectxindex != points[0] || selectyindex != points[1]) {
            selectxindex = points[0];
            selectyindex = points[1];
            return true;
        }
        return false;
    }

    public static int[] initpoint(Point p) {
        int[] points = new int[2];
        points[0] = -1;
        points[1] = -1;
        for (int i = 0; i < GameFactory.gaphblock.length; i++) {
            for (int j = 0; j < GameFactory.gaphblock[i].length; j++) {
                if (GameFactory.gaphblock[i][j].contains(p)) {
                    points[0] = j;
                    points[1] = i;
                }
            }
        }
        return points;
    }
}

效果截图:

 java swing版拼图游戏_第1张图片

你可能感兴趣的:(J2SE)