实例介绍
【实例截图】

【核心代码】
package net.xsoftlab.baike;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class ImagePieceTogether extends JFrame implements ActionListener {// 操作实现拼图的游戏的类
PanelOfImage imagePanel;// 声明图片面板
JPanel panelOfSouth, panelOfLook;// //声明南侧面板和查看面板
Button startButton;// 声明开始按钮
Button lookButton;// 声明查看按钮
Button chooseButton;// 选择按钮
Container container;// 容器,得到内容面板
public ImagePieceTogether() {// 构造方法进行初始化
container = this.getContentPane();// 获得内容面板
startButton = new Button("开始");// 创建开始按钮
startButton.addActionListener(this);// 添加监听事件
lookButton = new Button("查看");
lookButton.addActionListener(this);
chooseButton = new Button("选择");
chooseButton.addActionListener(this);
panelOfLook = new JPanel();// 创建查看面板
panelOfLook.setLayout(null);// 设置布局
Icon icon = new ImageIcon("picture/pic_" PanelOfImage.currentPID ".jpg");// 创建图标
JLabel label = new JLabel(icon);// 创建图标标签
label.setBounds(0, 0, 300, 300);// 设置标签的位置
panelOfLook.add(label);// 添加标签
panelOfSouth = new JPanel();// 创建南侧面板
panelOfSouth.setBackground(Color.red);// 设置背景颜色
panelOfSouth.add(startButton);// 添加开始按钮
panelOfSouth.add(lookButton);// 添加查看按钮
panelOfSouth.add(chooseButton);// 添加选择按钮
imagePanel = new PanelOfImage();// 创建图片面板
container.add(imagePanel, BorderLayout.CENTER);
container.add(panelOfSouth, BorderLayout.SOUTH);
this.setTitle("拼图游戏");// 设置标题
this.setLocation(300, 200);// 设置位置
this.setSize(308, 365);// 设置大小
this.setResizable(false);// 设置是否可以通过某个用户操作调整
this.setVisible(true);// 设置可视
this.setDefaultCloseOperation(3);// 设置默认关闭操作
}
@Override
public void actionPerformed(ActionEvent event) {// 按钮触发的事件
Button button = (Button) event.getSource();// 获得事件按钮源
if (button == startButton) {// 如果是开始按钮
imagePanel.breakRank();// 调用图片方格打乱方法
} else if (button == lookButton) {// 如果是查看事件
if (button.getLabel() == "查看") {// 如果按钮标签为"查看"
container.remove(imagePanel);// 容器移除图片面板
container.add(panelOfLook);// 容器添加查看标签
panelOfLook.updateUI();// 不用调整大小就可以出现新增删的组件
container.repaint();// 重绘
button.setLabel("返回");// 设置按钮标签
} else {
container.remove(panelOfLook);// 容器移除查看面板
container.add(imagePanel);// 容器添加图片面板
container.repaint();// 重绘
button.setLabel("查看");
}
} else if (button == chooseButton) {// 如果是选择按钮
Choice choice = new Choice();// 创建选择器
choice.add("--小猫--");// 添加列表项
choice.add("--QQ--");
int i = JOptionPane.showConfirmDialog(this, choice, "选择图片", JOptionPane.OK_CANCEL_OPTION);// 弹出对话框
if (i == JOptionPane.YES_OPTION) {// 选择对话框的确定按钮
PanelOfImage.currentPID = choice.getSelectedIndex() 1;// 获得列表项的编号
imagePanel.reLoadPictrue();// 图片重载
Icon icon = new ImageIcon("picture/pic_" PanelOfImage.currentPID ".jpg");// 获得图片图标
JLabel label = new JLabel(icon);// 根据图标设置标签
label.setBounds(0, 0, 300, 300);// 设置标签的方位
panelOfLook.removeAll();
panelOfLook.add(label);
panelOfLook.repaint();
}
}
}
public static void main(String[] args) {// java程序主入口处
new ImagePieceTogether();// 实例化对象
}
}
package net.xsoftlab.baike;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
class PaneButton extends JButton {// 继承按钮类实现加图片的方格
PaneButton(Icon icon) {// 构造方法进行初始化,设置图标
super(icon);
this.setSize(100, 100);// 设置每个方格的大小
}
public void move(String direction, int sleep) {// 方格的移动
if (direction == "UP") {// 方格向上移动
this.setLocation(this.getBounds().x, this.getBounds().y - 100);
} else if (direction == "DOWN") {// 方格向下移动
this.setLocation(this.getBounds().x, this.getBounds().y 100);
} else if (direction == "LEFT") {// 方格向左移动
this.setLocation(this.getBounds().x - 100, this.getBounds().y);
} else {// 方格向右移动
this.setLocation(this.getBounds().x 100, this.getBounds().y);
}
}
}
public class PanelOfImage extends JPanel implements MouseListener {// 图片面板加载方格对象
boolean hasAddActionListener = false;// 设置方格动作监听器的标识
PaneButton pane[];// 声明方格
Rectangle nullPanel;// 声明空方格,没有添图片
public static int currentPID = 1;// 当前选择的图片编号
public PanelOfImage() {// 构造方法进行初始化
this.setLayout(null);// 设置面板的布局为空
this.setSize(400, 400);// 设置面板的大小
nullPanel = new Rectangle(200, 200, 100, 100);// 设置空方格的位置
pane = new PaneButton[9];// 创建九个方格
Icon icon;// 声明图标
for (int i = 0; i < 3; i ) {// 循环为每个方格加载图片
for (int j = 0; j < 3; j ) {// 循环列
icon = new ImageIcon("picture/pic_" currentPID "_"
(i * 3 j 1) ".jpg");// 创建图标
pane[i * 3 j] = new PaneButton(icon);// 创建方格在方格中加载图片
pane[i * 3 j].setLocation(j * 100, i * 100);// 设置方格的位置
this.add(pane[i * 3 j]);// 面板添加方格
}
}
this.remove(pane[8]);// 移除多余的方格
}
public boolean isFinish() {// 判断是否拼凑成功
for (int i = 0; i < 8; i ) {
int x = pane[i].getBounds().x;
int y = pane[i].getBounds().y;
if (y / 100 * 3 x / 100 != i)
return false;
}
return true;
}
public void reLoadPictrue() {// 重新加载图片在重新选择图片时
Icon icon;
for (int i = 0; i < 3; i ) {// 循环为每个方格加载图片
for (int j = 0; j < 3; j ) {
icon = new ImageIcon("picture/pic_" currentPID "_"
(i * 3 j 1) ".jpg");
pane[i * 3 j].setIcon(icon);
}
}
}
public void breakRank() {// 方格打乱重新排序
while (pane[0].getBounds().x <= 100 && pane[0].getBounds().y <= 100) {// 当第一个方格距左上角近时
int x = nullPanel.getBounds().x;
int y = nullPanel.getBounds().y;
int direction = (int) (Math.random() * 4);// 随机产生一个数字对应空方格的上下左右移动
if (direction == 0) {// 空方格左移动,与左侧方格互换位置,左侧方格右移动
x -= 100;// 空主格左移
if (test(x, y)) {
for (int j = 0; j < 8; j ) {// 循环寻打左侧的按钮
if ((pane[j].getBounds().x == x)
&& (pane[j].getBounds().y == y)) {// 依次寻找左侧的按钮
pane[j].move("RIGHT", 100);// 方格向右移动一格
nullPanel.setLocation(x, y);// 重新设置空方格的位置
break;// 跳出循环
}
}
}
} else if (direction == 1) {// 空方格右移动
x = 100;
if (test(x, y)) {
for (int j = 0; j < 8; j ) {
if ((pane[j].getBounds().x == x)
&& (pane[j].getBounds().y == y)) {
pane[j].move("LEFT", 100);// 方格向左移动一格
nullPanel.setLocation(x, y);
break;
}
}
}
} else if (direction == 2) {// 空方格上移动
y -= 100;
if (test(x, y)) {
for (int j = 0; j < 8; j ) {
if ((pane[j].getBounds().x == x)
&& (pane[j].getBounds().y == y)) {
pane[j].move("DOWN", 100);// 方格向下移动一格
nullPanel.setLocation(x, y);
break;
}
}
}
} else {// 空方格下移动
y = 100;
if (test(x, y)) {
for (int j = 0; j < 8; j ) {
if ((pane[j].getBounds().x == x)
&& (pane[j].getBounds().y == y)) {
pane[j].move("UP", 100);// 方格向上移动一格
nullPanel.setLocation(x, y);
break;
}
}
}
}
}
if (!hasAddActionListener)// 判断是否添加动作事件
for (int i = 0; i < 8; i ) {// 循环为每个方格添加动作事件
pane[i].addMouseListener(this);
}
hasAddActionListener = true;
}
private boolean test(int x, int y) {// 检测方格是否在指定的范围内移动
if ((x >= 0 && x <= 200) || (y >= 0 && y <= 200))
return true;
else
return false;
}
public void mouseClicked(MouseEvent arg0) {// 鼠标点击时调用
}
public void mouseEntered(MouseEvent arg0) {// 鼠标进入组件区域时调用
}
public void mouseExited(MouseEvent arg0) {// 控制鼠标不能移动出面板的范围
}
public void mouseReleased(MouseEvent arg0) {// 鼠标按键在组件上释放时调用
}
public void mousePressed(MouseEvent event) {// 鼠标按下时调用
PaneButton button = (PaneButton) event.getSource();// 获得鼠标按的方格按钮
int x1 = button.getBounds().x;// 获得该方格按钮的横坐标
int y1 = button.getBounds().y;// 获得该方格按钮的纵坐标
int nullDir_X = nullPanel.getBounds().x;// 得到空方格的横坐标
int nullDir_Y = nullPanel.getBounds().y;// 得到空方格的纵坐标
if (x1 == nullDir_X && y1 - nullDir_Y == 100)// 进行比较果满足条件则交换
button.move("UP", 100);// 方格向上移动
else if (x1 == nullDir_X && y1 - nullDir_Y == -100)
button.move("DOWN", 100);// 方格向下移动
else if (x1 - nullDir_X == 100 & y1 == nullDir_Y)
button.move("LEFT", 100);// 方格向左移动
else if (x1 - nullDir_X == -100 && y1 == nullDir_Y)
button.move("RIGHT", 100);// 方格向右移动
else
return;
nullPanel.setLocation(x1, y1);// 重新设置空方格的位置
this.repaint();// 重新加载
if (this.isFinish()) {// 进行是否完成的判断
JOptionPane.showMessageDialog(this, "恭喜你,完成拼图");
for (int i = 0; i < 8; i ) {// 循环撤消鼠标事件
pane[i].removeMouseListener(this);
}
hasAddActionListener = false;
}
}
}
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论