九宫格拼图 我的java学习之路

来源:互联网 发布:淘宝必须用支付宝吗 编辑:程序博客网 时间:2024/05/17 08:28


package Jigsaw;


import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Date;
import java.util.Random;


import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JToolBar;


public class Jigsaw extends JFrame implements KeyListener {
private JLabel[] lb;
ImageIcon im=new ImageIcon();//用来记录要换的图像
private ImageIcon temp;
private ImageIcon images[],ImageIsTrue[];
int countLb=0;//记录空白的图
public Jigsaw(){
setTitle("湖南城市学院拼图");
setBounds(200, 50, 630, 650);
setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().setLayout(new GridLayout(3, 3, -32, -20));//设置网格布局,因为他们的间距为0,数据多次测试而得
lb=new JLabel[9];
images=new ImageIcon[9];
ImageIsTrue=new ImageIcon[9];
this.addKeyListener(this);
setPicture();//设置图片


//添加菜单

JMenuBar bar=new JMenuBar();
setJMenuBar(bar);
JMenu menuGame=new JMenu("游戏");
bar.add(menuGame);
JMenuItem itemPlay=new JMenuItem("开始游戏");
menuGame.add(itemPlay);
itemPlay.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
Jigsaw.this.StartGame();

}
});//加监听
JMenuItem itemExit=new JMenuItem("退出游戏");
menuGame.add(itemExit);
itemExit.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
setVisible(true);
}


private void setPicture() {//用标签设置图片
String dir="./src/Jigsaw/images/";//路径
String filenames[]=new String[lb.length];
for(int i=0;i<lb.length;i++){
if(i==lb.length-1){
filenames[i]=dir+"bg.png";
images[i]=new ImageIcon(filenames[i]);
im=images[i];
ImageIsTrue[i]=new ImageIcon(filenames[i]);
//lb[i].setVisible(false);
}else{
filenames[i]=dir+(i+1)+".png";
images[i]=new ImageIcon(filenames[i]);
ImageIsTrue[i]=new ImageIcon(filenames[i]);
ImageIsTrue[i]=images[i];
}
lb[i]=new JLabel(images[i]);
this.getContentPane().add(lb[i]);//添加到里面去
//lb[i].setHorizontalAlignment(JLabel.CENTER);//设置标签内容沿 Y 轴的对齐方式默认


}
}
private void StartGame() {
Random r = new Random((new Date()).getTime());
for(int i=0;i<lb.length;i++){//随机排画
int n = r.nextInt(9);
temp=new ImageIcon();//必须new否则是引用
temp=images[i];
images[i] = images[n];
images[n] = temp;
}
for(int i=0;i<lb.length;i++){
if(images[i].equals(im)){
countLb=i;
}
lb[i].setIcon(images[i]);
}

}


private void resetPicture() {
for(int i=0;i<lb.length;i++){
lb[i].setIcon(images[i]);
if(images[i].equals(im)){
countLb=i;
}
}
isWin();
}


public static void main(String[] args) {
new Jigsaw();
}


@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub


}


@Override
public void keyPressed(KeyEvent e) {
int key=e.getKeyCode();
switch(key){
case KeyEvent.VK_UP :
up();break;//remenber的break;
case KeyEvent.VK_DOWN :
down();break;
case KeyEvent.VK_RIGHT :
right();break;
case KeyEvent.VK_LEFT :
left();break;
default:
}


}


@Override
public void keyReleased(KeyEvent e) {



}


private void right() {
if (countLb!=0&&countLb!=3&&countLb!=6) {
temp = images[countLb];
images[countLb] = images[(countLb + 8) % 9];
images[(countLb + 8) % 9] = temp;
resetPicture();
}
}






private void isWin() {
boolean isWin=true;
// for(int i=0;i<lb.length;i++){
// System.out.println(images[i]);
// System.out.println(ImageIsTrue[i]);
// System.out.println();
// }//测试数据
for(int i=0;i<lb.length;i++){


if(!images[i].equals(ImageIsTrue[i])){
isWin=false;
break;
}else{
System.out.println("曹渊博");
}
}
if(isWin){
int i=JOptionPane.showConfirmDialog(this, "你赢了!是否继续???","恭喜",JOptionPane.OK_CANCEL_OPTION);//消息对话框
if(i==JOptionPane.OK_OPTION){

}else{

}
}
}


private void left() {
if (countLb!=2&&countLb!=5&&countLb!=8) {
temp = images[countLb];
images[countLb] = images[(countLb + 1) % 9];
images[(countLb + 1) % 9] = temp;
resetPicture();
}
}


private void up() {
if (countLb!=6&&countLb!=7&&countLb!=8) {
temp = images[countLb];
images[countLb] = images[(countLb + 3) % 9];
images[(countLb + 3) % 9] = temp;
resetPicture();
}
}


private void down() {
if (countLb!=0&&countLb!=1&&countLb!=2) {
temp = images[countLb];
images[countLb] = images[(countLb + 6) % 9];
images[(countLb + 6) % 9] = temp;
resetPicture();
}
}


}

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 听了鬼故事害怕怎么办 看完鬼片害怕睡不着怎么办 晚上看了鬼片怎么办 说话不经过大脑考虑怎么办 我太受欢迎了怎么办动漫结局 狗胃不好总呕吐怎么办 比格犬晚上叫怎么办 玻尿酸隆鼻变宽了怎么办 打玻尿酸鼻子变宽怎么办 鼻炎的人感冒了怎么办 小孩上幼儿园反复感冒怎么办 3岁宝宝感冒鼻炎怎么办 鼻炎犯了鼻子不通气怎么办 食物呛到鼻子里怎么办 胃疼引起的焦虑怎么办 泰迪犬发生口腔亏痒怎么办? 俩鼻子都堵了怎么办 2岁宝宝鼻甲肿大怎么办 感冒10多天不好怎么办 宝妈鼻炎犯了怎么办 鼻炎犯了好多黄鼻涕怎么办 空调吹多了上火怎么办 宝宝感冒鼻塞流鼻涕口臭怎么办 怀孕了有鼻息肉怎么办 孕晚期鼻炎犯了怎么办 孕妇鼻炎犯了头疼怎么办 一岁宝宝有鼻炎怎么办 蒸馒头的面酸了怎么办 dnf点券充错账号了怎么办 英雄联盟点券充错区了怎么办 文玩鼻烟壶盖子松了怎么办 文胸磨腋下的肉怎么办 荷兰在窝里拉屎怎么办 宝宝把泡沫吃了怎么办 荷兰猪一直叫该怎么办 人吃了缓冲泡沫怎么办 不小心吃了泡沫怎么办 不想养荷兰猪了怎么办 刚买的乌龟死了怎么办 剪猫指甲出血了怎么办 猫吃了酸性植物怎么办