一个简单的拼图小游戏,供大家参考,具体内容如下
1.首先设计视图面板。
2.添加所需要的图片按钮。
3.最主要的是设计监听事件,添加图片的监听按钮,设定移动空白图片周围的按钮。
4.判断是否成功 。
package sxy;
import java.awt.Choice;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.util.Random;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class PintuGame {
public static void main(String args[]) {
new PintuFrame().StartFrame();
}
}
class PintuFrame extends JFrame {
private static final long serialVersionUID = 1L;
// 等级设置
private static int level = 3;
// 图片索引
private static int index = 0;
// 图片数量
private static int picCount = 2;
// 开始时间
private long startTime;
// 初始化小方块
private JButton[] buttons;
// 初始化空方块
private JPanel emptyPanel = new JPanel();
// 初始化监听类
private PintuListener listener = new PintuListener();
// 初始化Panel
private JPanel panel = new JPanel(null);
// 图片预览
private JLabel label;
private String[] imgpath = new String[picCount];
// 选图时的图片路径
String path;
public PintuFrame() {
for (int i = 0; i < picCount; i++) {
imgpath[i] = i + ".jpg";
System.out.println(imgpath[i]);
}
path = imgpath[index];
}
/**
* 开始窗体加载
*/```
public void StartFrame() {
panel.removeAll();
JButton start = new JButton("开始");// 开始按钮
JButton left = new JButton("
JButton right = new JButton(">");
JLabel selLevel = new JLabel("LV:");
label = new JLabel(getIcon());// 根据图标设置标签
final Choice choice = new Choice();// 创建选择器
choice.add("--初级--");// 添加列表项
choice.add("--中级--");
choice.add("--高级--");
selLevel.setBounds(5, 0, 20, 20);// 设置坐标
choice.setBounds(28, 0, 65, 20);
start.setBounds(93, 0, 85, 20);
left.setBounds(178, 0, 61, 20);
right.setBounds(239, 0, 61, 20);
label.setBounds(0, 22, 300, 300);// 设置标签的方位
panel.add(selLevel);
panel.add(choice);
panel.add(start);
panel.add(left);
panel.add(right);
panel.add(label);
panel.repaint();
add(panel);
setTitle("拼图游戏");
setBounds(450, 130, 300, 322);
setResizable(false);
// 添加关闭按钮
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
// 监听等级选择
start.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
level = choice.getSelectedIndex() + 3;
launchFrame();
}
});
// 监听选图按钮
left.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (index == 0) {
index = picCount - 1;
path = imgpath[index];
} else {
path = imgpath[--index];
}
panel.remove(label);
label = new JLabel(getIcon());
label.setBounds(0, 22, 300, 300);
panel.add(label);
panel.repaint();
}
});
// 监听选图按钮 ->
right.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (index == picCount - 1) {
index = 0;
path = imgpath[index];
} else {
path = imgpath[++index];
}
panel.remove(label);
label = new JLabel(getIcon());
label.setBounds(0, 22, 300, 300);
panel.add(label);
panel.repaint();
}
});
}
/**
* 拼图窗体加载
*/
public void launchFrame() {
startTime = System.currentTimeMillis();
panel.removeAll();
buttons = new JButton[level * level];
// 设置图标组
Icon[] icon = new PintuFrame().creatIcon(path);
// 小方块索引
int index = 0;
// 小方块坐标
int x = 0, y = 0;
// 设置小方块位置,图标,监听
for (int i = 0; i < level; i++) {
for (int j = 0; j < level; j++) {
// 添加图标
buttons[index] = new JButton(icon[index]);
// 添加监听
buttons[index].addMouseListener(listener);
// 设置位置
buttons[index].setBounds(x, y, 100, 100);
// 添加到panel
panel.add(buttons[index++]);
x += 100;
}
int nullDir_X = emptyPanel.r (int i = 0; i < level; i++) {
for (int j = 0; j < level; j++) {
// 从原图像上获取一个方形位置
ImageFilter cropFilter = new CropImageFilter(x, y, 100, 100);
// 截取方形图像
Image img = Toolkit.getDefaultToolkit()
.createImage(new FilteredImageSource(image.getSource(), cropFilter));
icon[index++] = new ImageIcon(img);
x += 100;
}
y += 100;
x = 0;
}
return icon;
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
本文标题: java实现拼图小游戏
本文地址: /ruanjian/java/299532.html