怎么让同一个画布在Applet上时隐时现?

05-10-22 玉米

import java.awt.*;import java.awt.event.*;//本人想实现打时现时隐的兔子的效果,现在被时现时隐
import java.applet.*;                      //这步卡住了,望帮忙!谢谢!  
 public class Room116 extends Applet implements MouseListener
{
	TextField text1;
        Label label1;
        Button button1;
	static Image img;
	int n=0;int x=50,y=50;
 
 public void init()	
 {setSize(400,400);
 setLayout(null);setBackground(Color.pink);
        button1=new Button("开始");
 	add(button1);button1.setBounds(20,20,60,25);
        
 	label1=new Label("你打中的兔子为",Label.RIGHT);
        add(label1);label1.setBackground(Color.yellow);
        label1.setBounds(170,20,134,25);
 	text1=new TextField("0");
 	add(text1);text1.setBounds(304,20,76,25);
 	button1.addMouseListener(this);
        validate();
 	}
 
 public void start()
 {img=getImage(getCodeBase(),"gif002.gif");}
 		
 

public void mouseClicked(MouseEvent e)	
 {    Mycanvas canvas1=new Mycanvas(text1);       
 	//下面是想实现的画布时现时隐的效果,但是不行,请指教,谢谢拉!
           while(n<=100)
 		{int x=(int)Math.random()*350;
 		 int y=(int)Math.random()*300+23;
 		 this.add(canvas1);   
 		 canvas1.setBounds(x,y,40,40);
 		 try{Thread.sleep(500);}
 		 	catch(InterruptedException e)
 		 	{}
 		 	remove(canvase1);
 		 	n++;
 		}//到这行截止
            
 	label1.setText("游戏结束!你的成绩为:"); 
        button1.setEnabled(false);
 }
 

public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){} 
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}

 } 
 

 class Mycanvas extends Canvas implements MouseListener//画布的实现
 {TextField textfield1=null;
 	int count=0;
 	Mycanvas(TextField text1)
 	{setSize(40,40);
 	 textfield1=text1;
 	 addMouseListener(this);
 	}
 
 public void paint(Graphics g)
 {g.drawImage(Room116.img,0,0,40,40,this);
 	
 }
 
 public void mouseClicked(MouseEvent ee)
 {count++;
 	String s=String.valueOf(count);
 	textfield1.setText(s);
 }
 
  public void mousePressed(MouseEvent ee){}
  public void mouseReleased(MouseEvent ee){} 
  public void mouseEntered(MouseEvent ee){}
  public void mouseExited(MouseEvent ee){}

}	
<p>

猜你喜欢