博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
拼图游戏
阅读量:5344 次
发布时间:2019-06-15

本文共 4085 字,大约阅读时间需要 13 分钟。

package cn.hncu.gui.swing;

import java.awt.Dialog;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class LowLow extends JFrame implements ActionListener, MouseListener {

String pic1 = "pictures\\fff-";//被分割的小图的路径和名字
String pic2 = "pictures\\lll";//完整图的路径和名字
int MouseCount = 0;//玩家的移动次数
int number =200;//系统之前的移动次数,一定程度上可以控制图片乱序程度
String DaAn = null;
int brun = 15;
MyJButton[] b = new MyJButton[16];
MessageDialog dlg = new MessageDialog(this);
Icon[] image = new Icon[16];
int VisDong = -1;
public LowLow(String str){
super(str);
setBounds(600,200,400,400);
setResizable(false);
getContentPane().setLayout(new GridLayout(4, 4));
int[] Arr = Arr();
for(int i =0 ;i<16;i++){
image[i] = new ImageIcon(pic1+i+".png");
}
for(int i=0;i<16;i++){
b[i]=new MyJButton(image[i],i);
b[i].setSize(100, 100);
getContentPane().add(b[i]);
b[i].addActionListener(this);
b[i].addMouseListener(this);
}
run();
setVisible(true);
}
public void Rrun(int i){
MyJButton bb = new MyJButton();
bb.setIcon(b[brun].getIcon());bb.setZhi(b[brun].getZhi());
b[brun].setIcon(b[brun+i].getIcon());b[brun].setZhi(b[brun+i].getZhi());
b[brun+i].setIcon(bb.getIcon());b[brun+i].setZhi(bb.getZhi());
brun+=i;
}
private void run() {
int[] Arr = Arr();
for(int i =0;i<number;i++){
if(Arr[i]==-4&&brun-4>=0){//上
Rrun(Arr[i]);
DaAn = "下"+DaAn;
}
if(Arr[i]==4&&brun+4<=15){//下
Rrun(Arr[i]);
DaAn = "上"+DaAn;
}
if(Arr[i]==-1&&brun%4!=0){//左
Rrun(Arr[i]);
DaAn = "右"+DaAn;
}
if(Arr[i]==1&&(brun+1)%4!=0){//右
Rrun(Arr[i]);
DaAn = "左"+DaAn;
}
}
System.out.println("必胜:"+DaAn);
}

public static void main (String[] args){

new LowLow("低级拼图");
}
//产生随机数组
public int[] Arr(){
int[] t = {-1,1,-4,4};
int[] a = new int[number];
for(int i=0;i<number;i++){
int temp = (int) (Math.random()*10)%4;
a[i] = t[temp];
}
return a;
}
//判断是否移动
public boolean Mo(int i){
VisDong = -1;
for(int j=0;j<16;j++){
if(b[j].p==15){
VisDong=j;
break;
}
}
if(i+1==VisDong||i-1==VisDong||i+4==VisDong||i-4==VisDong){
return true;
}
return false;
}
//判断是否结束
public boolean gameOver(){
for(int i =0;i<16;i++){
if(b[i].p!=i)return false;
}
return true;
}
class MyJButton extends JButton{
private int p;
private Icon image;
public MyJButton(){
}
public MyJButton(Icon image,int p){
super(image);
this.p = p;
}
public int getZhi(){
return p;
}
public void setZhi(int p){
this.p=p;
}
}
@Override
public void actionPerformed(ActionEvent e) {
MouseCount++;
for(int i=0;i<16;i++){
if(e.getSource()==b[i]&&Mo(i)){
MyJButton bb = new MyJButton();
bb.setIcon(b[i].getIcon());bb.setZhi(b[i].getZhi());
b[i].setIcon(b[VisDong].getIcon());b[i].setZhi(b[VisDong].getZhi());
b[VisDong].setIcon(bb.getIcon());b[VisDong].setZhi(bb.getZhi());
}
}
if(gameOver()){
JOptionPane.showMessageDialog(this, "恭喜您赢了!!\r\n移动次数:"+MouseCount);
System.out.println("恭喜您赢了!!");
System.out.println("移动次数:"+MouseCount);
}
}

@Override

public void mouseClicked(MouseEvent e) {
}

@Override

public void mousePressed(MouseEvent e) {
for(int i=0;i<16;i++){
if(e.getSource()==b[i]&&b[i].p==15){
dlg.show("");
}
}
}
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
}
//自定义的对话框类
class MessageDialog extends JDialog{
private JFrame jframe;
private JLabel label;
public MessageDialog(JFrame jframe){
super(jframe,"原图",true);
setBounds(150, 200, 400, 400);
setDefaultCloseOperation(HIDE_ON_CLOSE);
setResizable(false);
label = new JLabel(new ImageIcon(pic2+".png"));
getContentPane().add(label);
}
public void show(String str){
label.setText(str);
setVisible(true);
}
}
}

转载于:https://www.cnblogs.com/1314wamm/p/5603703.html

你可能感兴趣的文章
[HIHO1184]连通性二·边的双连通分量(双连通分量)
查看>>
Codeforces Round #178 (Div. 2) B. Shaass and Bookshelf 【动态规划】0-1背包
查看>>
SparkStreaming 源码分析
查看>>
【算法】—— 随机音乐的播放算法
查看>>
mysql asyn 示例
查看>>
DataGrid 点击 获取 行 ID
查看>>
git 使用
查看>>
边框圆角方法
查看>>
asp.net WebApi自定义权限验证消息返回
查看>>
php中eval函数的危害与正确禁用方法
查看>>
20172315 2017-2018-2 《程序设计与数据结构》第十一周学习总结
查看>>
MySQL添加、修改、撤销用户数据库操作权限的一些记录
查看>>
关于谷歌浏览器Chrome正在处理请求的问题解决
查看>>
Git核心技术:在Ubuntu下部署Gitolite服务端
查看>>
平面波展开法总结
查看>>
建造者模式
查看>>
ArraySort--冒泡排序、选择排序、插入排序工具类demo
查看>>
composer 安装laravel
查看>>
8-EasyNetQ之Send & Receive
查看>>
Android反编译教程
查看>>