?? tansuo.java
字號:
package Lily;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Tansuo extends Applet implements MouseListener,Runnable {
ZuJian z1=new ZuJian(0,0);
Tansuozhe tan=new Tansuozhe(30,30);
int w=900;
int h=600;
int x,y;
int dx,dy;
int gex,gey;
int nowx=1;
int nowy=1;
boolean biaozhi[][]=new boolean[30][20];
boolean biaozhi1[][]=new boolean[30][20];
Stack jilu=new Stack();
Thread newThread;
// ArrayList b=new ArrayList();
public void init(){
setLayout(null);
setSize(w,h);
setBackground(Color.white);
add(tan); biaozhi[1][1]=true;biaozhi1[1][1]=true;
tan.setLocation(30,30);
addMouseListener(this);
}
public void start(){
newThread=new Thread(this);
newThread.start();
}
public void stop(){
newThread=null;
}
public void run(){
while(newThread!=null){
if(biaozhi1[nowx+1][nowy]==false){
int j[]=new int[2];
j[0]=nowx;j[1]=nowy;
jilu.push(j);
nowx=nowx+1;
tan.setLocation(30*nowx,30*nowy);biaozhi1[nowx][nowy]=true;
try{
newThread.sleep(300);
}catch(InterruptedException e){}
}
else if(biaozhi1[nowx][nowy+1]==false){
int j[]=new int[2];
j[0]=nowx;j[1]=nowy;
jilu.push(j);
nowy=nowy+1;
tan.setLocation(30*nowx,30*nowy);biaozhi1[nowx][nowy]=true;
try{
newThread.sleep(300);
}catch(InterruptedException e){}
}
else if(biaozhi1[nowx-1][nowy]==false){
int j[]=new int[2];
j[0]=nowx;j[1]=nowy;
jilu.push(j);
nowx=nowx-1;
tan.setLocation(30*nowx,30*nowy);biaozhi1[nowx][nowy]=true;
try{
newThread.sleep(300);
}catch(InterruptedException e){}
}
else if(biaozhi1[nowx][nowy-1]==false){
int j[]=new int[2];
j[0]=nowx;j[1]=nowy;
jilu.push(j);
nowy=nowy-1;
tan.setLocation(30*nowx,30*nowy);biaozhi1[nowx][nowy]=true;
try{
newThread.sleep(300);
}catch(InterruptedException e){}
}
else {
int k[]=new int[2];
k=(int[])jilu.pop();
nowx=k[0];
nowy=k[1];
tan.setLocation(30*nowx,30*nowy);
try{
newThread.sleep(20);
}
catch(InterruptedException e){}
}
}
}
public void mousePressed(MouseEvent e){
while(jilu.empty()!=true){
jilu.pop();
}
for(int i=0;i<30;i++){
for(int j=0;j<20;j++){
biaozhi1[i][j]=false;
}
}
for(int i=0;i<30;i++){
for(int j=0;j<20;j++){
biaozhi1[i][j]=biaozhi[i][j];
}
}
x=e.getX();y=e.getY();
ZuJian p=new ZuJian(x,y);
dx=x%30;dy=y%30;
int u=x-dx;int v=y-dy;
gex=u/30;gey=v/30; //System.out.println("baiozhi["+gex+"]"+"["+gey+"]="+biaozhi[gex][gey]);
if(biaozhi[gex][gey]==false){
add(p);
p.setLocation(u,v);
// b.add(p);
biaozhi[gex][gey]=true; biaozhi1[gex][gey]=true; // System.out.println("baiozhi["+gex+"]"+"["+gey+"]="+biaozhi[gex][gey]);
}
/* int k=getComponentCount();
Component[] xilie=getComponents();
for(int i=0;i<k;i++) {
ZuJian ppp=(ZuJian)xilie[i];
if(ppp.click==true) {
remove(ppp) ;
biaozhi[gex][gey]=false; }
}*/
}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
}
class ZuJian extends Button {// implements MouseListener{
int w=30;
int h=30;
int x,y;
//boolean click=false;
ZuJian(int x,int y ){
setSize(w,h);
setBackground(Color.red);
this.x=x;this.y=y;
// addMouseListener(this);
}
/*public void mousePressed(MouseEvent e){
if( click==true)
click=false;
else
click=true;
}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}*/
}
class Tansuozhe extends Button{
int w=30;int h=30;
int x,y;
Tansuozhe(int x,int y){
super("?&*%#");
setSize(w,h);
setBackground(Color.blue);
this.x=x;this.y=y;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -