九宫格代码

代码块

//主函数

import cn.ui.SudokuFrame;

public class MainApp {
    public static void main(String[] args) {
        SudokuFrame frame = new SudokuFrame();
        frame.setVisible(true);
    }
}

//九宫格主界面的代码

package cn.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

public class SudokuFrame extends JFrame {
    private PictureCanvas canvas;
    private PicturePreview preview;
    private JButton button;

    public SudokuFrame(){
        init();
        addCompont();
        addPreviewCompont();
        addListener();
    }
    //添加监听器
    private void addListener() {
        button.addActionListener(new ActionListener() {


            public void actionPerformed(ActionEvent e) {
                canvas.start();

            }
        });

    }

    private void addPreviewCompont() {
        //创建另一个面板
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(1,2));

        canvas = new PictureCanvas();
        canvas.setBorder(new TitledBorder("Canvas"));
        preview = new PicturePreview();
        preview.setBorder(new TitledBorder("Preview"));

        panel.add(canvas,BorderLayout.WEST);
        panel.add(preview,BorderLayout.EAST);
        this.add(panel,BorderLayout.CENTER);

    }

    private void addCompont() {
        //创建上方一个面板
        JPanel panel = new JPanel();
        //设置标题
        panel.setBorder(new TitledBorder("Button"));
        //设置背景色
        panel.setBackground(Color.GREEN);
        button = new JButton("Start");
        //设置按钮背景色
        button.setBackground(Color.YELLOW);

        panel.add(button,BorderLayout.WEST);
        this.add(panel,BorderLayout.NORTH);

    }

    private void init() {
        //标题
        this.setTitle("Sudoku");
        //大小
        this.setSize(1000,720);
        //位置
        this.setLocation(300,200);
        //固定大小
        this.setResizable(false);

        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
}

//将下面的一个控件右边的界面预览


import java.awt.Graphics;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import cn.ui.PictureCanvas;

public class PicturePreview extends JPanel {
    // 重写绘制组件方法,显示图片的额显示
        @Override
        protected void paintComponent(Graphics g) {

            super.paintComponent(g);
            // 指定图片的路径
            String filename = "picture\\"+PictureCanvas.pictureID+".jpg";
            // 通过图片的路径,获取到相应图片中的图像
            ImageIcon icon = new ImageIcon(filename);
            Image image = icon.getImage();

            // 把图像绘制在预览区的面板中
            g.drawImage(image, 20, 20, 450, 600, this);
        }

}

//下面空件左边的button区


import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class PictureCanvas extends JPanel implements MouseListener {
    public static int pictureID = 1;
    private Cell[] cell;
    private boolean hasAddActionListener = false;
    private Rectangle nullCell;

    // 空参数构造
    public PictureCanvas() {
        // 设置拼图区的布局
        this.setLayout(null);
        // 创建12个图片的小方格,添加到拼图区
        cell = new Cell[12];
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 3; j++) {
                // 加载图片
                int num = i * 3 + j;
                ImageIcon icon = new ImageIcon("picture\\1_" + (num + 1)
                        + ".gif");
                // 创建图片小方格
                cell[num] = new Cell(icon);
                // 指定图片小方格显示的位置
                cell[num].setLocation(j * 150 + 20, i * 150 + 20);
                // 把图片小方格添加到拼图区显示
                this.add(cell[num]);
            }
        }

        // 删除第12个 图片的小方格
        this.remove(cell[11]);
        nullCell = new Rectangle(320, 470, 150, 150);
    }

    public void start() {
        // 如果要是没有给小方格添加监听的话,添加监听
        if (!hasAddActionListener) {
            // 添加监听
            for (int i = 0; i < 11; i++) {
                cell[i].addMouseListener(this);
            }
            // 更新鼠标监听 
            hasAddActionListener = true;

        }
        // 判断当前第一个小方格距离左上角的较劲的时,进行方格与空方格互换
        // 如果一个小方格在左上角的四个方格位置内,就不断地循环,进行方格与空方格的互换
        while (cell[0].getBounds().x < 170 && cell[0].getBounds().y < 170) {
            // 获取空方格的位置
            int nullX = nullCell.getBounds().x;
            int nullY = nullCell.getBounds().y;
            // 随机产生一个方向,进行空方格与小方法的互换
            // 产生0-3的随机数,对应空方格的上下左右移动
            int direction = (int) (Math.random() * 4);
            switch (direction) {
            case 0:// 空方格向左移动,与左边的方格进行互换位置,左侧方格要向右移动
                nullX -= 150;
                cellMove(nullX, nullY, "RIGHT");
                break;
            case 1:// 空方格向右移动,与右边的方格进行互换位置,右侧方格要向左移动
                nullX += 150;
                cellMove(nullX, nullY, "LEFT");
                break;
            case 2:// 空方格向上移动,与上边的方格进行互换位置,上侧方格要向下移动
                nullY -= 150;
                cellMove(nullX, nullY, "DOWN");
                break;
            case 3:// 空方格向下移动,与下边的方格进行互换位置,下侧方格要向上移动
                nullY += 150;
                cellMove(nullX, nullY, "UP");
                break;
            default:
                break;
            }
        }

    }

    /** * * @param nullX * 空方格的x轴坐标 * @param nullY * 空方格的y轴坐标 * @param string * 方格要移动的方向 */

    private void cellMove(int nullX, int nullY, String direction) {
        for (int i = 0; i < 11; i++) {
            // 获取与空方格位置相同的小方格
            if (cell[i].getBounds().x == nullX
                    && cell[i].getBounds().y == nullY) {
                // 当前方格的移动
                cell[i].move(direction);
                // 空方格的移动
                nullCell.setLocation(nullX, nullY);
                // 交换位置后,结束循环
                break;
            }
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent e) {
        // 获取到当前所点击的小方格
        Cell button = (Cell) e.getSource();
        // 获取到当前所
        int clickX = button.getBounds().x;
        int clickY = button.getBounds().y;

        int nullX = nullCell.getBounds().x;
        int nullY = nullCell.getBounds().y;

        // 进行比较
        if (clickX == nullX && clickY - nullY == 150) {
            // 点击的为空方格下面的方格
            button.move("UP");
        } else if (clickX == nullX && clickY - nullY == -150) {
            button.move("DOWN");
        } else if (clickX - nullX == 150 && clickY == nullY) {
            button.move("LEFT");
        } else if (clickX - nullX == -150 && clickY == nullY) {
            button.move("RIGHT");
        } else{
            return;
        }
        //空方格移动
        nullCell.setLocation(clickX, clickY);
        //拼图区的重新绘制
        this.repaint();
        //将游戏状态区的步数更新
        //判断当前游戏是否完成,若完成,给玩家一个友好提示
        if(this.isFinish()){
            JOptionPane.showMessageDialog(this, "厉害了!\n 所用步:");
            //撤销每一个小方格的鼠标点击监听,让鼠标点击小方格不在作用
            for(int i =0;i<11;i++){
                cell[i].removeMouseListener(this);
            }
            hasAddActionListener = false;
        }
    }
    private boolean isFinish(){
        for(int i=0;i<11;i++){
            int x = cell[i].getBounds().x;
            int y = cell[i].getBounds().y;
            if((y-20)/150*3 +(x-20)/150 !=i){

                return false;
            }
        }
        return true;
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }

}

//鼠标监听区

package cn.ui;

import javax.swing.Icon;
import javax.swing.JButton;

public class Cell extends JButton {


    public Cell(Icon icon) {
        super(icon);
        this.setSize(150, 150);
    }
    public void move (String direction){
        switch(direction){
        case"UP":
            this.setLocation(this.getBounds().x, this.getBounds().y-150);
            break;
        case"DOWN":
            this.setLocation(this.getBounds().x, this.getBounds().y+150);
            break;
        case"LEFT":
            this.setLocation(this.getBounds().x-150, this.getBounds().y);
            break;
        case"RIGHT":
            this.setLocation(this.getBounds().x+150, this.getBounds().y);
            break;
        default:
            break;

        }
    }

}
    原文作者:九宫格问题
    原文地址: https://blog.csdn.net/qq420007900/article/details/71402965
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞