package A;
//13
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Collections;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class ControlGamePanel extends JPanel implements ActionListener
{
private static final long serialVersionUID = 1L;
GameWindow gameWin;
JButton buttonStart,buttonPreview,buttonSave,buttonDraw,buttonExit;
PuzzlePanel puzzlePanel;
int row,column;
File gradeFile;
ArrayList<Image> imageList;
Cell[][] cells;
Cell lastCell,noImageCell;
JPanel imagePanel;
Image[][] cellImages;
Image[][] imagesState;
JLabel playerName,usedStep;
int step;
JMenu menuImage;
JMenuItem oneGradeItem,twoGradeItem;
public ControlGamePanel(GameWindow gameWin)
{
this.gameWin =gameWin;
buttonStart=new JButton("开始新游戏");
buttonPreview=new JButton("预览全图");
buttonSave=new JButton("保存游戏");
buttonDraw=new JButton("提取游戏");
buttonExit=new JButton("结束当前游戏");
this.buttonStart .addActionListener(this);
this.buttonPreview .addActionListener(this);
this.buttonSave .addActionListener(this);
this.buttonDraw .addActionListener(this);
this.buttonExit .addActionListener(this);
add(this.buttonStart);
add(this.buttonPreview );
add(this.buttonSave );
add(this.buttonDraw );
add(this.buttonExit );
imagePanel=gameWin.getImagePanel();
usedStep=gameWin.getUsedStep();
step=Integer.parseInt(usedStep.getText());
menuImage=gameWin.getMenuImage();
oneGradeItem=gameWin.getOneGradeItem();
twoGradeItem=gameWin.getTwoGradeItem();
}
@Override
public void actionPerformed(ActionEvent e)
{
//“开始新游戏”
if(e.getSource()==buttonStart)
{
String name=JOptionPane.showInputDialog(null,"请输入玩家的姓名:","登录",JOptionPane.PLAIN_MESSAGE);
if(name==null)
return;
if(name.equalsIgnoreCase(""))
{
JOptionPane.showMessageDialog(null,"玩家姓名不能为空!");
return;
}
playerName=gameWin.getPlayerName();
playerName.setText(name);
buttonStart.setEnabled(false);
menuImage.setEnabled(false);
oneGradeItem.setEnabled(false);
twoGradeItem.setEnabled(false);
row=gameWin.getRow();
column=gameWin.getColumn();
puzzlePanel=gameWin.getPuzzlePanel();
cells=puzzlePanel.getCells();
cellImages=puzzlePanel.getCellImages();
imageList=puzzlePanel.getImageList();
//在“预览全图”中使用imagesState来恢复拼图面板界面
imagesState=new Image[row][column];
noImageCell=cells[row-1][column-1];
Image cellImagePanel=noImageCell.getButtonImage();//得到右下角按钮上的图片
//在右边的imagePanel面板上显示右下角的图片按钮
lastCell=new Cell(cellImagePanel);
lastCell.setPreferredSize(new Dimension(Cell.WIDTH ,Cell.HEIGHT ));
imagePanel.removeAll();
imagePanel.add(lastCell);
imagePanel.validate();
noImageCell.setButtonImage(null);
noImageCell.updateUI();
//打乱图片顺序
Collections.shuffle(imageList);
//根据打乱的图片顺序,重新刷新单元格的背景,右下角单元格无图案
MoveListener l=new MoveListener();
//给除了右下角的所有按钮重新显示打乱的图片,并给它们注册ActionListener监视器
int k=0;
for(int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
{
if(i==row-1&&j==column-1)
{
break;
}
cells[i][j].setButtonImage(imageList.get(k));
cells[i][j].repaint();
cells[i][j].updateUI();//重新设置其外观值,让其有边框,方便区别不同的单元格,方便拼图
cells[i][j].addMouseListener(l);
k++;
}
}// end of for(int i=0;i<row;i++)
//给右下角无图像的按钮注册动作监视器
noImageCell.addMouseListener(l);
puzzlePanel.validate();
}
//“预览全图”
else if(e.getSource()==buttonPreview)
{
//在游戏界面展示(预览),原图(完整的)
if(e.getActionCommand().equals("预览全图"))
{
//获取预览全图以前游戏的拼图完成情况,将图片顺序地排到二维数组imagesState中
for(int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
{
imagesState[i][j]=cells[i][j].getButtonImage();
}
}// end of for(int i=0;i<row;i++)
//预览原图
for(int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
{
cells[i][j].setButtonImage(cellImages[i][j]);
cells[i][j].repaint();
cells[i][j].setBorder(null);
}
}// end of for(int i=0;i<row;i++)
buttonPreview.setText("返回");
}
else if(e.getActionCommand().equals("返回"))
{
for(int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
{
cells[i][j].setButtonImage(imagesState[i][j]);
cells[i][j].repaint();
cells[i][j].updateUI();
}
}// end of for(int i=0;i<row;i++)
buttonPreview.setText("预览全图");
}
}// end of else-if(e.getSource()==buttonPreview)
//“保存游戏”
else if(e.getSource()==buttonSave)
{
/*
//保存当前游戏状态,供下次接着玩
//保存当前游戏的拼图完成状况,将图片顺序地排到二维数组imageIcons中;Image不能序列化;ImageIcon可以序列化
*/
ImageIcon[][] icons=new ImageIcon[row][column];
Point p=noImageCell.getButtonPoint();
int x=(int)p.getX();
int y=(int)p.getY();
for(int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
{
if(i==x&&j==y)
{
icons[i][j]=new ImageIcon();
continue;
}
icons[i][j]=new ImageIcon(cells[i][j].getButtonImage());
}
}// end of for(int i=0;i<row;i++)
try
{
FileOutputStream fos=new FileOutputStream("saveGame.txt");
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeUTF(playerName.getText());
oos.writeInt(step);
//noImageCell的Point
oos.writeInt(x);
oos.writeInt(y);
for(int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
{
oos.writeObject(icons[i][j]);
oos.flush();
}
}// end of for(int i=0;i<row;i++)
ImageIcon lastImageIcon=new ImageIcon(lastCell.getButtonImage());
oos.writeObject(lastImageIcon);
oos.flush();
///
boolean boo=false;
for(int i=0;i<row;i++)
{
for(int j=0;j<column;j++,boo=false)
{
for(int k=0;k<row;k++)
{
if(boo)
break;
for(int l=0;l<column;l++)
{
if(cells[i][j].getButtonImage()==cellImages[k][l])
{
oos.writeInt(k);
oos.writeInt(l);
oos.flush();
boo=true;
break;
}
}// end of for(int l=0;l<column;l++)
}
}// end of for(int j=0;j<column;j++,boo=false)
}// end of for(int i=0;i<row;i++)
///
oos.close();
}
catch (Exception e1)
{
e1.printStackTrace();
}
}// end of else-if(e.getSource()==buttonSave)
//“提取游戏”
else if(e.getSource()==buttonDraw)
{
//提取上次未完成的游戏界面,接着玩
//cells
ImageIcon[][] imageHistory=new ImageIcon[row][column];
cellImages=new Image[row][column];
try
{
FileInputStream fis=new FileInputStream("saveGame.txt");
ObjectInputStream ois=new ObjectInputStream(fis);
String name=ois.readUTF();
step=