java拼图游戏_Java编程制作拼图游戏

import java.awt.*;

import java.awt.event.*;

import java.awt.image.BufferedImage;

import java.io.*;

import javax.imageio.ImageIO;

import java.util.*;

import javax.swing.*;

public class Pintu extends JFrame{

private Image image, buff;             //原始图片,缓存图片

private Point point = new Point(2, 2);         //记录第九块图片位置

private int[][] map = {{0, 1, 2},{3, 4, 5}, {6, 7, 8}}; //给破碎图片标号

private int sx, sy;       // 分割后每一个图片的宽度和高度

private Canvas canvas;    //加载图片容器

private Graphics gs, gb;     //gs 画出Canvas ;gb画出buff图像

private boolean isRunning = false;   //游戏是否正在进行

private JButton start = new JButton("开始新的游戏"); // 按钮1

private JButton show = new JButton("显示正确图片"); //按钮2

private JTextArea showTime = new JTextArea("显示时间");

private JTextArea showStep = new JTextArea("显示步骤");

private JPanel panel = new JPanel(); //装在上面2个按钮

private int steps = 0;               // 记录移动的步骤

public Pintu(String title) {   //构造方法

super(title);

try {                     //异常抛出

image = ImageIO.read(new File("bg.jpg")); //装载图片

} catch (IOException ex) {

ex.printStackTrace();

}

initScreen();   //初始化canvas,由于canvas 初始化较复杂,所以单独写一个方法

buff = new BufferedImage(715, 381, BufferedImage.TYPE_INT_BGR);//三原色加载

gb = buff.getGraphics();

sx = image.getWidth(this) / 3;

sy = image.getHeight(this) / 3;

setLayout(new BorderLayout());

add(panel, BorderLayout.SOUTH);

add(canvas, BorderLayout.CENTER);

panel.setLayout(new GridLayout(1, 4));

panel.add(start);

panel.add(show);

panel.add(showTime);

panel.add(showStep);

showTime.setEditable(false);

showStep.setEditable(false);

showTime.setFont(new Font("黑体", Font.PLAIN, 16));

showStep.setFont(new Font("黑体", Font.PLAIN, 16));

setSize(720, 425);

setResizable(false);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

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