public void initImageAgain(int nImgNo)
{
//nImgNo为要装载的图像是第几个图像。
if(nImgNo>m_nNumOfImg)
{
showStatus("你要的图片不存在!!");
return;
}
// System.out.println(param);
MediaTracker mediaTracker=new MediaTracker(this);
//建立一个监视器
// for(int i=0;i<9;i++)
// {
// int nTemp=i+1;
// m_Image[i]=getImage(getDocumentBase(),"img/pintu.jpg");
m_ImgAll=getImage(getDocumentBase(),"img/"+m_sImgName[nImgNo]);
//装载总的大图片
// m_Image[i]=getImage(getDocumentBase(),"img/"+nTemp+".JPG");
// mediaTracker.addImage(m_Image[i],i);
mediaTracker.addImage(m_ImgAll,1);
// }
try
{
mediaTracker.waitForAll();
}
catch(Exception e)
{
System.out.println("图片装载出错");
}
for(int i=0;i<9;i++)
{
m_Image[i]=createImage(IMAGE_WIDTH,IMAGE_HEIGHT);
Graphics g=m_Image[i].getGraphics();
int nRow=i%3;
int nCol=i/3;
g.drawImage(m_ImgAll,0,0,IMAGE_WIDTH,IMAGE_HEIGHT,nRow*IMAGE_WIDTH,nCol*IMAGE_HEIGHT,(nRow+1)*IMAGE_WIDTH,(nCol+1)*IMAGE_HEIGHT,this);
System.out.println("dfsdfdfdsdf"+i);
// m_ImgAll.getGraphics().clipRect(1,1,120,120);
// g.copyArea(1,1,120,120,0,0);
}
}
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics g)
{
g.setColor(Color.white);
//将当前颜色变为白色
g.fillRect(0,0,DELTAX,IMAGE_HEIGHT*3);
//填充左边的提示信息区域
g.setFont(new Font("宋体",Font.PLAIN,15));
//设置字体
g.setColor(Color.blue);
//设置颜色
g.drawString("步数:"+nStep,5,20);
g.drawString("现有图片"+m_nNumOfImg+"张",5,60);
g.drawString("请按1-"+m_nNumOfImg+"键改变图片",5,100);
//在坐标(10,20)画出字串,来显示现在走了多少步。
g.setColor(Color.white);
if(bOnShowAll)
{
int x=DELTAX;
int y=0;
g.drawImage(m_ImgAll,x,y,this);
return;
}
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
int x=i*IMAGE_WIDTH+DELTAX;
int y=j*IMAGE_HEIGHT; if(m_nImageNo[i][j]==NO_IMAGE)
g.fill3DRect(x,y,IMAGE_WIDTH,IMAGE_HEIGHT,true);
else
{
g.drawImage(m_Image[m_nImageNo[i][j]],x,y,this);
g.drawRect(x,y,IMAGE_WIDTH,IMAGE_HEIGHT);
}
}
}
checkStatus();
if(bWantStartNewGame)
{
//如果游戏结束,玩家将拼图的顺序排对之后
nScore=1000-nStep*10-nTime;
g.setColor(Color.blue);
g.drawString("请按任意键重新开始",5,140);
g.setColor(Color.red);
// g.setColor(Color.blue);
g.setFont(new Font("宋体",Font.PLAIN,40));
g.drawString("你赢了"+nScore+"分",70+DELTAX,160);
g.drawString("祝贺你!",110+DELTAX,210);
transferScore(nScore);
}
}
public pintu()
{
}
public void initData()
{
int[] nHasDistrib=new int[9];
for(int i=0;i<9;i++)nHasDistrib[i]=0;
for(int j=0;j<3;j++)
{
for(int i=0;i<3;i++)
{
int nCount=j*3+i;
int nImgNo=-1;
do
{
nImgNo=(int)(Math.random()*9);
}while(nHasDistrib[nImgNo]==1);
//1代表已经分配了这张图片
m_nImageNo[i][j]=nImgNo;
nHasDistrib[nImgNo]=1;
System.out.println("test..");
}
}
m_nImageNo[(int)(Math.random()*3)][(int)(Math.random()*3)]=NO_IMAGE;
nStep=0;
nTime=0;
//清空计时器
} public int directionCanMove(int nCol,int nRow)
{
if((nCol-1)>=0)
if(m_nImageNo[nRow][nCol-1]==NO_IMAGE)
return DIRECTION_UP;
if((nCol+1)<=2)
if(m_nImageNo[nRow][nCol+1]==NO_IMAGE)
return DIRECTION_DOWN;
if((nRow-1)>=0)
if(m_nImageNo[nRow-1][nCol]==NO_IMAGE)
return DIRECTION_LEFT;
if((nRow+1)<=2)
if(m_nImageNo[nRow+1][nCol]==NO_IMAGE)
return DIRECTION_RIGHT;
return DIRECTION_NONE;
}
public void move(int nCol,int nRow,int nDirection)
{
switch(nDirection)
{
case DIRECTION_UP:
m_nImageNo[nRow][nCol-1]=m_nImageNo[nRow][nCol];
m_nImageNo[nRow][nCol]=NO_IMAGE;
break;
case DIRECTION_DOWN:
m_nImageNo[nRow][nCol+1]=m_nImageNo[nRow][nCol];
m_nImageNo[nRow][nCol]=NO_IMAGE;
break;
case DIRECTION_LEFT:
m_nImageNo[nRow-1][nCol]=m_nImageNo[nRow][nCol];
m_nImageNo[nRow][nCol]=NO_IMAGE;
break;
case DIRECTION_RIGHT:
m_nImageNo[nRow+1][nCol]=m_nImageNo[nRow][nCol];
m_nImageNo[nRow][nCol]=NO_IMAGE;
break;
}
}
public boolean move(int nDirection)
{
int nNoImageCol=-1;
int nNoImageRow=-1;
int i=0;
int j=0;
while (i<3 && nNoImageRow==-1)
{
while (j<3 && nNoImageCol==-1)
{
if(m_nImageNo[i][j]==NO_IMAGE)
{
nNoImageRow=i;
nNoImageCol=j;
}
j++;
}
j=0;
i++;
}
//以上判断哪个拼图可以往方向nDirection移动
//可以移动的拼图的位置为第nNoImageCol行,第nNoImageRow列。 System.out.println(nNoImageCol+",,"+nNoImageRow);
switch(nDirection)
{
case DIRECTION_UP:
if(nNoImageCol==3)return false;
m_nImageNo[nNoImageRow][nNoImageCol]=m_nImageNo[nNoImageRow][nNoImageCol+1];
m_nImageNo[nNoImageRow][nNoImageCol+1]=NO_IMAGE;
break;
case DIRECTION_DOWN:
if(nNoImageCol==0)return false;
m_nImageNo[nNoImageRow][nNoImageCol]=m_nImageNo[nNoImageRow][nNoImageCol-1];
m_nImageNo[nNoImageRow][nNoImageCol-1]=NO_IMAGE;
break;
case DIRECTION_LEFT:
if(nNoImageRow==3)return false;
m_nImageNo[nNoImageRow][nNoImageCol]=m_nImageNo[nNoImageRow+1][nNoImageCol];
m_nImageNo[nNoImageRow+1][nNoImageCol]=NO_IMAGE;
break;
case DIRECTION_RIGHT:
if(nNoImageRow==0)return false;
m_nImageNo[nNoImageRow][nNoImageCol]=m_nImageNo[nNoImageRow-1][nNoImageCol];
m_nImageNo[nNoImageRow-1][nNoImageCol]=NO_IMAGE;
break;
}
return true;
}
public void mouseClicked(MouseEvent e)
{
//Invoked when the mouse has been clicked on a component.
if(bOnShowAll)return;
if(bWantStartNewGame)
{
initData();
repaint();
bWantStartNewGame=false;
return;
} int nX=e.getX()-DELTAX;
int nY=e.getY();
int nCol=nY/IMAGE_HEIGHT;
int nRow=nX/IMAGE_WIDTH;
System.out.println("col:"+nCol+" row:"+nRow);
int nDirection=directionCanMove(nCol,nRow);
if(nDirection!=DIRECTION_NONE)
{
move(nCol,nRow,nDirection);
nStep++;
m_audioClip1.play();
repaint();
}
else
{
m_audioClip2.play();
} }
public void mouseEntered(MouseEvent e)
{
//Invoked when the mouse has been clicked on a component.
}
public void mouseExited(MouseEvent e)
{
//Invoked when the mouse has been clicked on a component.
}
public void mousePressed(MouseEvent e)
{
//Invoked when the mouse has been clicked on a component.
}
public void mouseReleased(MouseEvent e)
{
//Invoked when the mouse has been clicked on a component.
}
{
//nImgNo为要装载的图像是第几个图像。
if(nImgNo>m_nNumOfImg)
{
showStatus("你要的图片不存在!!");
return;
}
// System.out.println(param);
MediaTracker mediaTracker=new MediaTracker(this);
//建立一个监视器
// for(int i=0;i<9;i++)
// {
// int nTemp=i+1;
// m_Image[i]=getImage(getDocumentBase(),"img/pintu.jpg");
m_ImgAll=getImage(getDocumentBase(),"img/"+m_sImgName[nImgNo]);
//装载总的大图片
// m_Image[i]=getImage(getDocumentBase(),"img/"+nTemp+".JPG");
// mediaTracker.addImage(m_Image[i],i);
mediaTracker.addImage(m_ImgAll,1);
// }
try
{
mediaTracker.waitForAll();
}
catch(Exception e)
{
System.out.println("图片装载出错");
}
for(int i=0;i<9;i++)
{
m_Image[i]=createImage(IMAGE_WIDTH,IMAGE_HEIGHT);
Graphics g=m_Image[i].getGraphics();
int nRow=i%3;
int nCol=i/3;
g.drawImage(m_ImgAll,0,0,IMAGE_WIDTH,IMAGE_HEIGHT,nRow*IMAGE_WIDTH,nCol*IMAGE_HEIGHT,(nRow+1)*IMAGE_WIDTH,(nCol+1)*IMAGE_HEIGHT,this);
System.out.println("dfsdfdfdsdf"+i);
// m_ImgAll.getGraphics().clipRect(1,1,120,120);
// g.copyArea(1,1,120,120,0,0);
}
}
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics g)
{
g.setColor(Color.white);
//将当前颜色变为白色
g.fillRect(0,0,DELTAX,IMAGE_HEIGHT*3);
//填充左边的提示信息区域
g.setFont(new Font("宋体",Font.PLAIN,15));
//设置字体
g.setColor(Color.blue);
//设置颜色
g.drawString("步数:"+nStep,5,20);
g.drawString("现有图片"+m_nNumOfImg+"张",5,60);
g.drawString("请按1-"+m_nNumOfImg+"键改变图片",5,100);
//在坐标(10,20)画出字串,来显示现在走了多少步。
g.setColor(Color.white);
if(bOnShowAll)
{
int x=DELTAX;
int y=0;
g.drawImage(m_ImgAll,x,y,this);
return;
}
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
int x=i*IMAGE_WIDTH+DELTAX;
int y=j*IMAGE_HEIGHT; if(m_nImageNo[i][j]==NO_IMAGE)
g.fill3DRect(x,y,IMAGE_WIDTH,IMAGE_HEIGHT,true);
else
{
g.drawImage(m_Image[m_nImageNo[i][j]],x,y,this);
g.drawRect(x,y,IMAGE_WIDTH,IMAGE_HEIGHT);
}
}
}
checkStatus();
if(bWantStartNewGame)
{
//如果游戏结束,玩家将拼图的顺序排对之后
nScore=1000-nStep*10-nTime;
g.setColor(Color.blue);
g.drawString("请按任意键重新开始",5,140);
g.setColor(Color.red);
// g.setColor(Color.blue);
g.setFont(new Font("宋体",Font.PLAIN,40));
g.drawString("你赢了"+nScore+"分",70+DELTAX,160);
g.drawString("祝贺你!",110+DELTAX,210);
transferScore(nScore);
}
}
public pintu()
{
}
public void initData()
{
int[] nHasDistrib=new int[9];
for(int i=0;i<9;i++)nHasDistrib[i]=0;
for(int j=0;j<3;j++)
{
for(int i=0;i<3;i++)
{
int nCount=j*3+i;
int nImgNo=-1;
do
{
nImgNo=(int)(Math.random()*9);
}while(nHasDistrib[nImgNo]==1);
//1代表已经分配了这张图片
m_nImageNo[i][j]=nImgNo;
nHasDistrib[nImgNo]=1;
System.out.println("test..");
}
}
m_nImageNo[(int)(Math.random()*3)][(int)(Math.random()*3)]=NO_IMAGE;
nStep=0;
nTime=0;
//清空计时器
} public int directionCanMove(int nCol,int nRow)
{
if((nCol-1)>=0)
if(m_nImageNo[nRow][nCol-1]==NO_IMAGE)
return DIRECTION_UP;
if((nCol+1)<=2)
if(m_nImageNo[nRow][nCol+1]==NO_IMAGE)
return DIRECTION_DOWN;
if((nRow-1)>=0)
if(m_nImageNo[nRow-1][nCol]==NO_IMAGE)
return DIRECTION_LEFT;
if((nRow+1)<=2)
if(m_nImageNo[nRow+1][nCol]==NO_IMAGE)
return DIRECTION_RIGHT;
return DIRECTION_NONE;
}
public void move(int nCol,int nRow,int nDirection)
{
switch(nDirection)
{
case DIRECTION_UP:
m_nImageNo[nRow][nCol-1]=m_nImageNo[nRow][nCol];
m_nImageNo[nRow][nCol]=NO_IMAGE;
break;
case DIRECTION_DOWN:
m_nImageNo[nRow][nCol+1]=m_nImageNo[nRow][nCol];
m_nImageNo[nRow][nCol]=NO_IMAGE;
break;
case DIRECTION_LEFT:
m_nImageNo[nRow-1][nCol]=m_nImageNo[nRow][nCol];
m_nImageNo[nRow][nCol]=NO_IMAGE;
break;
case DIRECTION_RIGHT:
m_nImageNo[nRow+1][nCol]=m_nImageNo[nRow][nCol];
m_nImageNo[nRow][nCol]=NO_IMAGE;
break;
}
}
public boolean move(int nDirection)
{
int nNoImageCol=-1;
int nNoImageRow=-1;
int i=0;
int j=0;
while (i<3 && nNoImageRow==-1)
{
while (j<3 && nNoImageCol==-1)
{
if(m_nImageNo[i][j]==NO_IMAGE)
{
nNoImageRow=i;
nNoImageCol=j;
}
j++;
}
j=0;
i++;
}
//以上判断哪个拼图可以往方向nDirection移动
//可以移动的拼图的位置为第nNoImageCol行,第nNoImageRow列。 System.out.println(nNoImageCol+",,"+nNoImageRow);
switch(nDirection)
{
case DIRECTION_UP:
if(nNoImageCol==3)return false;
m_nImageNo[nNoImageRow][nNoImageCol]=m_nImageNo[nNoImageRow][nNoImageCol+1];
m_nImageNo[nNoImageRow][nNoImageCol+1]=NO_IMAGE;
break;
case DIRECTION_DOWN:
if(nNoImageCol==0)return false;
m_nImageNo[nNoImageRow][nNoImageCol]=m_nImageNo[nNoImageRow][nNoImageCol-1];
m_nImageNo[nNoImageRow][nNoImageCol-1]=NO_IMAGE;
break;
case DIRECTION_LEFT:
if(nNoImageRow==3)return false;
m_nImageNo[nNoImageRow][nNoImageCol]=m_nImageNo[nNoImageRow+1][nNoImageCol];
m_nImageNo[nNoImageRow+1][nNoImageCol]=NO_IMAGE;
break;
case DIRECTION_RIGHT:
if(nNoImageRow==0)return false;
m_nImageNo[nNoImageRow][nNoImageCol]=m_nImageNo[nNoImageRow-1][nNoImageCol];
m_nImageNo[nNoImageRow-1][nNoImageCol]=NO_IMAGE;
break;
}
return true;
}
public void mouseClicked(MouseEvent e)
{
//Invoked when the mouse has been clicked on a component.
if(bOnShowAll)return;
if(bWantStartNewGame)
{
initData();
repaint();
bWantStartNewGame=false;
return;
} int nX=e.getX()-DELTAX;
int nY=e.getY();
int nCol=nY/IMAGE_HEIGHT;
int nRow=nX/IMAGE_WIDTH;
System.out.println("col:"+nCol+" row:"+nRow);
int nDirection=directionCanMove(nCol,nRow);
if(nDirection!=DIRECTION_NONE)
{
move(nCol,nRow,nDirection);
nStep++;
m_audioClip1.play();
repaint();
}
else
{
m_audioClip2.play();
} }
public void mouseEntered(MouseEvent e)
{
//Invoked when the mouse has been clicked on a component.
}
public void mouseExited(MouseEvent e)
{
//Invoked when the mouse has been clicked on a component.
}
public void mousePressed(MouseEvent e)
{
//Invoked when the mouse has been clicked on a component.
}
public void mouseReleased(MouseEvent e)
{
//Invoked when the mouse has been clicked on a component.
}
{
//Invoked when a key has been pressed.
if(bOnShowAll)
{
if(e.getKeyCode()==KeyEvent.VK_Y)
{
bOnShowAll=false;
repaint();
}
return;
}
System.out.println("press key"+e.getKeyCode()+" "
+e.getKeyText(e.getKeyCode()));
System.out.println(KeyEvent.VK_LEFT);
if(bWantStartNewGame)
{
initData();
bWantStartNewGame=false;
repaint();
return;
}
int nDirection=DIRECTION_NONE;
switch(e.getKeyCode())
{
case KeyEvent.VK_DOWN:
nDirection=DIRECTION_DOWN;
break;
case KeyEvent.VK_UP:
nDirection=DIRECTION_UP;
break;
case KeyEvent.VK_LEFT:
System.out.println("left111...");
nDirection=DIRECTION_LEFT;
break;
case KeyEvent.VK_RIGHT:
System.out.println("left...");
nDirection=DIRECTION_RIGHT;
break;
case KeyEvent.VK_F1:
//F1键按下,重新开始游戏
initData();
//init();
repaint();
return;
case KeyEvent.VK_1:
case KeyEvent.VK_2:
case KeyEvent.VK_3:
case KeyEvent.VK_4:
case KeyEvent.VK_5:
case KeyEvent.VK_6:
case KeyEvent.VK_7:
case KeyEvent.VK_8:
case KeyEvent.VK_9:
int nImgNo=e.getKeyCode()-KeyEvent.VK_1;
if(nImgNo<m_nNumOfImg)
{
System.out.println(nImgNo);
initImageAgain(nImgNo);
initData();
repaint();
}
return;
case KeyEvent.VK_Y:
if(bOnShowAll)
bOnShowAll=false;
else
bOnShowAll=true;
repaint();
return;
default:
return;
}
boolean bCanMove=move(nDirection);
if(bCanMove)
{
nStep++;
m_audioClip1.play();
repaint();
}
else
{
m_audioClip2.play();
}
}
}
public void keyReleased(KeyEvent e)
{
//Invoked when a key has been pressed.
}
public void keyTyped(KeyEvent e)
{
//Invoked when a key has been pressed.
}
public void checkStatus()
{
boolean bWin=true;
//定义成员,默认值为真
int nCorrectNum=0;
for(int j=0;j<3;j++)
{
for(int i=0;i<3;i++)
{
if(m_nImageNo[i][j]!=nCorrectNum && m_nImageNo[i][j]!=NO_IMAGE)
bWin=false;
nCorrectNum++;
}
}
//比较拼图是否都放到了正确的位置上,如果,有一个没有放到正确位置上,则游戏就不能结束。
if(bWin)bWantStartNewGame=true;
}
public static void main(String[] args)
{
pintu pintu1=new pintu();
pintu1.init();
pintu1.start();
}
public void run()
{
while(Thread.currentThread()==thTimer)
{
try
{
thTimer.sleep(990);
String sTemp="你玩了"+nTime+"秒的时间,";
if(nTime>200)sTemp=sTemp+"时间用的很长了,你可要加油啦!";
else sTemp=sTemp+"别紧张,慢慢来。";
showStatus(sTemp);
if(!bWantStartNewGame)nTime++;
}
catch(Exception e)
{
}
}
}
public void transferScore(int nScore)
{
String sLocation="saveScore.asp?score=";
nScore=(nScore/2)*10+nScore%4;
sLocation=sLocation+nScore;
URL url=null;
try
{
url=new URL(sLocation);
}
catch(Exception e){}
getAppletContext().showDocument(url);
}
}
threads from your applicaion main thread2) Synchronization.3) Your mouse's condition.
System.out.println("clicked");
看点击是是否打印。
你mouseClicked()方法中非正常退出太多了,可能会造成不响应。仔细看
程序逻辑。而且mouseClicked()方法和keyPressed()条件判断不一致。
package wincontrol;import java.awt.*;
import java.awt.event.*;
import java.applet.*;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/public class Control extends Applet implements MouseListener,KeyListener{
private boolean isStandalone = false;
//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 Control() {
}
//Initialize the applet
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception { addMouseListener(this);
addKeyListener(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;
}
//Main method
public static void main(String[] args) {
Control applet = new Control();
applet.isStandalone = true;
Frame frame;
frame = new Frame() {
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
public synchronized void setTitle(String title) {
super.setTitle(title);
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
}
};
frame.setTitle("Applet Frame");
frame.add(applet, BorderLayout.CENTER);
applet.init();
applet.start();
frame.setSize(400,320);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
frame.setVisible(true);
}
public void keyPressed(KeyEvent e)
{
//Invoked when a key has been pressed.
System.out.println("press key"+e.getKeyCode()+" "
+e.getKeyText(e.getKeyCode()));
}
public void keyReleased(KeyEvent e)
{
//Invoked when a key has been pressed.
}
public void keyTyped(KeyEvent e)
{
//Invoked when a key has been pressed.
}
public void mouseClicked(MouseEvent e)
{
//Invoked when the mouse has been clicked on a component. int nX=e.getX();
int nY=e.getY();
System.out.println("col:"+nX+" row:"+nY);
}
public void mouseEntered(MouseEvent e)
{
//Invoked when the mouse has been clicked on a component.
}
public void mouseExited(MouseEvent e)
{
//Invoked when the mouse has been clicked on a component.
}
public void mousePressed(MouseEvent e)
{
//Invoked when the mouse has been clicked on a component.
}
public void mouseReleased(MouseEvent e)
{
//Invoked when the mouse has been clicked on a component.
}}