JavaSwing拼图游戏与源码


说明:参考网上视频《基于Java的拼图游戏》,并优化了诸如jcombox选择时的状态切换,cell交换等逻辑

实现效果

这里写图片描述


这里写图片描述


这里写图片描述

参考代码


此处仅仅列出canvals区的代码

package com.lsh.ui;

import java.awt.Color;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

@SuppressWarnings("serial")
public class PictureCanvas extends JPanel implements ActionListener {

    private Cell[] cell=new Cell[12];
    public static int pictureID=1;
    public static int stepNum=0;
    private JButton oldButton=null;
    private int addx=110;
    private int addy=110;
    /**
     * Create the panel.
     */
    public PictureCanvas() {
        setLayout(null);
        addx = 110;
        addy = 110;
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 3; j++) {
                ImageIcon icon=new ImageIcon(String.format("picture/p%d/p%d_%02d.jpg",PictureCanvas.pictureID,PictureCanvas.pictureID,i*3+j+1));
                cell[i*3+j]=new Cell(icon);
                cell[i*3+j].setBounds(5+j*addx, 5+i*addy,addx, addy);
                this.add(cell[i*3+j]);

            }
        }
    }
    public void reLoadPictureAddNumber(){
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 3; j++) {
                cell[i*3+j].setText(i*3+j+1+"");
            }
        }
    }

    public void reLoadPictureClearNumber(){
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 3; j++) {
                ImageIcon icon=new ImageIcon(String.format("picture/p%d/p%d_%02d.jpg",PictureCanvas.pictureID,PictureCanvas.pictureID,i*3+j+1));
                cell[i*3+j].setIcon(icon);
                cell[i*3+j].setText("");
            }
        }
    }
    public void reLoadImg(){
        if(oldButton!=null)
            oldButton.setBorder(new LineBorder(Color.BLUE));
        oldButton=null;
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 3; j++) {
                ImageIcon icon=new ImageIcon(String.format("picture/p%d/p%d_%02d.jpg",PictureCanvas.pictureID,PictureCanvas.pictureID,i*3+j+1));
                cell[i*3+j].setIcon(icon);
                cell[i*3+j].setBounds(5+j*addx, 5+i*addy,addx, addy);
                cell[i*3+j].removeActionListener(this);
            }
        }
    }
    public void start() {
        int size=cell.length;
        stepNum=0;
        for (int i = 0; i < size; i++) {
            Random random=new Random();
            int i1=random.nextInt(size);
            int i2=random.nextInt(size);
            switchCell(cell[i1], cell[i2]);
        }
        for (Cell cell2 : cell) {
            cell2.removeActionListener(this);
            cell2.addActionListener(this);
        }
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        JButton jButton=(JButton)e.getSource();
        if(oldButton==null){
            jButton.setBorder(new LineBorder(Color.RED));
            oldButton=jButton;
        }else{
            //if(!isClose(oldButton, jButton))return;
            switchCell(oldButton, jButton);
            stepNum++;
            PictureMainFrame.textField_1.setText("步数:"+stepNum);
            if(isFinish()){
                JOptionPane.showMessageDialog(this, "SUCCESS!");
                if(oldButton!=null)
                    oldButton.setBorder(new LineBorder(Color.BLUE));
                oldButton=null;
                for (Cell cell2 : cell) {
                    cell2.removeActionListener(this);
                }
            }       
        }
    }
    //是否紧邻
//  private boolean isClose(JButton oldButton, JButton jButton) {
//      Point point=oldButton.getLocation();
//      Point point2=jButton.getLocation();
//      if(Math.abs(point.x-point2.x)>110||Math.abs(point.y-point2.y)>110)//8格中移动
//          return false;
//      if(Math.abs(point.x-point2.x)==Math.abs(point.y-point2.y))//对角,4格中移动
//          return false;
//      return true;
//  }
    private boolean isFinish() {
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 3; j++) {
                Cell ce=cell[i*3+j];
                if(ce.getX()!=5+j*110||ce.getY()!=5+i*110){
                    return false;
                }
            }
        }
        return true;
    }
    private void switchCell(JButton cell1,JButton cell2){
        if(cell1==cell2)
            return;
        Point point=cell1.getLocation();
        cell1.setLocation(cell2.getLocation());
        cell2.setLocation(point);
    }
}


源码

下载链接

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值