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();
}
}
}