怎麼讓同一個畫布在Applet上時隱時現?

玉米發表於2005-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 class="indent">

相關文章