Java实现拼图游戏
主要利用了Graphics中的
public abstract boolean drawImage(Image img,
int dx1,
int dy1,
int dx2,
int dy2,
int sx1,
int sy1,
int sx2,
int sy2,
Color bgcolor,
ImageObserver observer)
方法(Draws as much of the specified area of the specified image as is currently available, scaling it on the fly tofit inside the specified area of the destination drawable surface.)大意是:把 img 中由 (sx1, sy1)(sx2, sy2)指定的矩形区域画到 observer 中由(dx1, dy1)(dx2, dy2)指定的矩形区域内(如果两个指定的大小不同,可能会依目的大小为准进行拉伸或压缩,建议相同)。 有了上面的方法就可以进行构思拼图游戏的具体实现了,主要是计算上面的8个值。以下代码注释很详细(或说很罗嗦),有几点要指出: 1、图片为随机打乱。一个小子说8块图片怎么随机打乱都能拼成,结果有几次拼了多半个小时都不成(我不太擅长拼图),所以如果拼不出来,请重新开始一下。 2、最后的图片为拼图图片。存为 bg.jpg ,放至同一文件夹下即可,如要换图,要改变窗口大小,否则会变形。 3、一直另我很郁闷的事情。空白的一块始终颜色是黑色,无论我加多少 setcolor语句,还望各位大虾指教。 4、显示时间未实现。继承Runnable接口,实现并不难,但 显示正确图片-继续游戏 相当于一个暂停按钮,需要线程间的协作唤醒-等待,恩,不算太难,有需要的就实现,否则就掐表吧
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);
}
void initScreen() {
canvas = new Canvas() {
public void paint(Graphics g) { //覆写canvas的paint 方法
gs = getGraphics(); //得到Canvas的Graphics
if (isRunning) {
drawScreen();
} else {
g.drawImage(image, 0, 0, this);
g.setColor(Color.gray);
}
}
};
canvas.addMouseListener(new MouseAdapter() {
//覆写mousePressed 方法,实现鼠标时,图片移动
public void mousePressed(MouseEvent me) {
if (!isRunning) return; // 如果未运行,说明图片未分割,直接返回
int x = me.getX() / sx, y = me.getY() / sy;//判断鼠标的图片在map中的编号
int fx = (int) point.getX(), fy = (int) point.getY();//记录第九块图片(未显示图片)的位置
int canMove = Math.abs(fx - x) + Math.abs(fy - y); // 如鼠标位置与第九块图片(可移动的位置)相邻,则canMove 必为 1
if (canMove != 1 ) return; // canMove 不为1,不能移动图片
map[fx][fy] = map[x][y]; //图片坐标 赋给第九块图片
map[x][y] = 8; //第九块图片给掉 图片
point.setLocation(x, y); //point 坐标改变
drawScreen(); //重绘屏幕,实现图片移动
showStep.setText("移动步骤:" + ++steps); //步骤加一
}
});
//为Start按钮添加事件
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
initMap();
drawScreen();
isRunning = true;
steps = 0;
showStep.setText("移动步骤:" + steps);
show.setLabel("显示正确图片");
}
});
//为show按钮添加事件
show.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if (show.getLabel().equals("继续游戏")) {
drawScreen();
isRunning = true;
show.setLabel("显示正确图片");
} else {
gs.drawImage(image, 0, 0, canvas);
isRunning = false;
show.setLabel("继续游戏");
}
}
});
}
void initMap() {
long time = System.nanoTime();//得到系统当前时间 作为随机数种子
java.util.Random rnd = new java.util.Random(time);
int temp, x1, y1, x2, y2;
//随机交换图片
for (int i = 0; i 100; i++) {
x1 = rnd.nextInt(3);
x2 = rnd.nextInt(3);
y1 = rnd.nextInt(3);
y2 = rnd.nextInt(3);
temp = map[x1][y1];
map[x1][y1] = map[x2][y2];
map[x2][y2] = temp;
}
//标记8号图片(即空白图片)
outer:
for (int i = 0; i 3; i++)
for (int j = 0; j 3; j++)
if (map[i][j] 8) {
point.setLocation(i, j);
break outer;
}
}
void drawScreen() {
int sx1, sy1, sx2, sy2, dx1, dy1, dx2, dy2;
int t1, t2;
gb.setColor(Color.white);
gb.clearRect(0, 0, sx * 3, sy * 3);//清空整个缓冲去图片
for (int x = 0; x 3; x++)
for (int y = 0; y 3; y++)
if(map[x][y] != 8) {
// 目的地址
dx1 = x * sx; dy1 = y * sy; // 移动的左上角坐标
dx2 = dx1 + sx - 1; dy2 = dy1 + sy - 1; // 移动的右下角坐标
// 源地址
t1 = map[x][y] % 3 ; t2 = map[x][y] / 3 ;
sx1 = t1 * sx; sy1 = t2 * sy; // 移动的左上角图标
sx2 = sx1 + sx - 1; sy2 = sy1 + sy - 1; // 移动的右下角坐标
gb.drawImage(image, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, Color.white, canvas);//绘制缓冲区图片
}
gs.drawImage(buff, 0, 0, canvas);//将缓冲区图片绘制到 canvas 上
}
public static void main(String args) {
Pintu pintu = new Pintu("拼图");
new Thread(pintu).start();
pintu.setVisible(true);
}
}
| 广告合作:400-664-0084 全国热线:400-664-0084 Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号 珠峰网 版权所有 All Rights Reserved
|