package pintugame3;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class Applet1 extends Applet implements KeyListener, Runnable, MouseListener {
private boolean isStandalone = false;
Image[] smallImage=new Image[9];//定义9个小图像的图像数组
Image bigImage;//定义大图片
int arrange[][]=new int[3][3];//9个小图像的排列位置
int noImage=-1;//设置空的图像位置为0
int width=90;//设置小图像宽度为90
int height=90;//设置小图像的高度为90
int up=1;//上
int down=2;//下
int left=3;//左
int right=4;//右
boolean rePlay=false;//标记是否重新排列
boolean isShowBig=false;//是否显示大图像
int startx=0;
int starty=0;
int steps=0;//计算所用的步数
int playSeconds=0;//花费的时间
Thread timer;//定义一个时间线程
//Get a parameter value
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}
//Construct the applet
public Applet1() {
}
//Initialize the applet
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
String name="pintu.jpg";//设置图片名称
bigImage=getImage(getDocumentBase(),"image/"+name);//设置图片目录
MediaTracker mediaTracker=new MediaTracker(this);//定义一个监听器
mediaTracker.addImage(bigImage,1);
try{//查看图像是否装载成功
mediaTracker.waitForID(1);
}catch(Exception e){
System.out.println("无法装载图片");
}
for(int i=0;i<9;i++){
smallImage[i]=createImage(width,height);//创建小图像
Graphics g=smallImage[i].getGraphics();//获得Graphics对象
int j=i%3;
int k=i/3;
//将大图像的某一区域画到小图像上
g.drawImage(bigImage,0,0,width,height,j*width,k*height,(j+1)*width,(k+1)*height,this);
}
arrangeImage();//随机排列小图像
timer=new Thread(this);//定义线程
timer.start();//开始线程
addKeyListener(this);//添加键盘监听器
addMouseListener(this);//添加鼠标监听器
}
//Start the applet
public void start() {
}
//Stop the applet
public void stop() {
}
//Destroy the applet
public void destroy() {
}
//Get Applet information
public String getAppletInfo() {
return "Applet Information";
}
//Get parameter info
public String[][] getParameterInfo() {
return null;
}
public void arrangeImage(){
int[] arr=new int[9];
for(int i=0;i<9;i++)
arr[i]=0;
//随机排列各个小图像的位置
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
int k=-1;
do{
k=(int)(9*Math.random());
}while(arr[k]==1);
arrange[i][j]=k;
arr[k]=1;
}
}
//随机设置一个小图像不显示
arrange[(int)(Math.random()*3)][(int)(Math.random()*3)]=noImage;
}
public void update(Graphics g){
paint(g);
}
public void paint(Graphics g){
if(isShowBig){//是否显示大图像,如是则显示大图像,返回
g.drawImage(bigImage,0,0,this);
isShowBig=false;
return;
}
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
int x=i*width;
int y=j*height;
int z=arrange[i][j];
if(z==-1){
g.fill3DRect(x,y,width,height,true);//将小图像设置为黑色
}else{
g.drawImage(smallImage[z],x,y,this);//绘制小图像
g.drawRect(x,y,width,height);
}
}
}
g.setColor(Color.WHITE);
g.fillRect(0,3*height,3*width,height);
g.setColor(Color.BLACK);
g.setFont(new Font("宋体",Font.PLAIN,16));
g.drawString("你已走了"+steps+"步",0,20+3*height);
if(isFinish()){
g.drawString("你成功了!",0,40+3*width);
}
}
public void moveImage(int dire){
int m=-1;
int n=-1;
boolean isRepaint=false;//查看小图像是否移动
for(int i=0;i<3;i++){//找出空区域的坐标
for(int j=0;j<3;j++){
if(arrange[i][j]==-1){
m=i;
n=j;
break;
}
}
}
switch (dire){//响应键盘事件
case 1:
if(n==2){//如果空区域在最上面,则无法上移,退出
break;
}
arrange[m][n]=arrange[m][n+1];
arrange[m][n+1]=noImage;//空区域和移动的小图像交换
isRepaint=true;//设置变量为真,重画
break;
case 2:
if(n==0){
break;
}
arrange[m][n]=arrange[m][n-1];
arrange[m][n-1]=noImage;
isRepaint=true;
break;
case 3:
if(m==2){
break;
}
arrange[m][n]=arrange[m+1][n];
arrange[m+1][n]=noImage;
isRepaint=true;
break;
case 4:
if(m==0){
break;
}
arrange[m][n]=arrange[m-1][n];
arrange[m-1][n]=noImage;
isRepaint=true;
break;
default:
break;
}
//如果isRepaint为真,表示小图像移动,重新绘制
if(isRepaint){
steps++;
repaint();//重新绘制图像
}
}
public void moveImage(int m,int n,int direction){
switch (direction){
case 1:
arrange[n][m-1]=arrange[n][m];
arrange[n][m]=-1;
break;
case 2:
arrange[n][m+1]=arrange[n][m];
arrange[n][m]=-1;
break;
case 3:
arrange[n-1][m]=arrange[n][m];
arrange[n][m]=-1;
break;
case 4:
arrange[n+1][m]=arrange[n][m];
arrange[n][m]=-1;
break;
default:
break;
}
}
//Main method
public static void main(String[] args) {
Applet1 applet = new Applet1();
applet.isStandalone = true;
Frame frame;
frame = new Frame();
frame.setTitle("Applet Frame");
frame.add(applet, BorderLayout.CENTER);
applet.init();
applet.start();
frame.setSize(270,300);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
frame.setVisible(true);
}
public void keyTyped(KeyEvent e) {
/**@todo Implement this java.awt.event.KeyListener method*/
}
public void keyPressed(KeyEvent e) {
/**@todo Implement this java.awt.event.KeyListener method*/
//如果显示大图像,则重新绘制并返回
if(isShowBig){
if(e.getKeyChar()==KeyEvent.VK_S){
isShowBig=false;
steps=0;
playSeconds=0;
repaint();
}
return;
}
//如果重新开始,则调用arrangeImage方法,重新绘制图像并返回
if(rePlay){
arrangeImage();
rePlay=false;
steps=0;
playSeconds=0;
repaint();
return;
}
switch(e.getKeyCode()){//得到键盘按键信息
case 38:
moveImage(up);//上移
break;
case 40:
moveImage(down);//下移
break;
case 39:
moveImage(right);//右移
break;
case 37:
moveImage(left);//左移
break;
case KeyEvent.VK_R://重新开始
arrangeImage();
steps=0;
repaint();
return;
case KeyEvent.VK_S://显示大图像
isShowBig=true;
playSeconds=0;
steps=0;
repaint();
return;
default:
return;
}
}
public void keyReleased(KeyEvent e) {
/**@todo Implement this java.awt.event.KeyListener method*/
}
public void run() {
/**@todo Implement this java.lang.Runnable method*/
while(Thread.currentThread()==timer){
try{
timer.sleep(1000);//线程睡眠1分钟
String str="你玩了"+playSeconds+"秒的时间,";
showStatus(str);//显示所费的时间
playSeconds++;
}catch(Exception e){
}
}
}
public boolean confirmDir(int row,int col){
if(col>=1)
if(arrange[col-1][row]==-1){
arrange[col-1][row] = arrange[col][row];
arrange[col][row]=-1;
return true;
}
if(col<=1)