⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tree.java

📁 这是《Java 2 简明教程(第2版)》一书配套的源代码。
💻 JAVA
字号:
import java.awt.*;
import java.applet.*;
import java.awt.image.*;

public class Tree extends Applet implements Runnable
{
   Thread animate;
   Image img,buffer;
   Graphics gContext;
   int width,height;
 
   public void init()
   {
     img=getImage(getCodeBase(),"1.jpg");
     MediaTracker tracker=new MediaTracker(this);
     tracker.addImage(img,0);
     try{
       tracker.waitForID(0);
     }catch(InterruptedException e){}
      
     width=img.getWidth(this);
     height=img.getHeight(this)/2;
     
     buffer=createImage(3*width,height);
     gContext=buffer.getGraphics();
     gContext.drawImage(img,0,-height,this);
     
     for(int i=0;i<height;i++)
      gContext.copyArea(0,i,width,1,width,(height-1)-2*i);
     gContext.clearRect(0,0,width,height);
    }
 
    public void start()
    {
      if(animate==null)
      {
        animate=new Thread(this);
        animate.start();
      }
    }

     public void stop()
     {
       if(animate!=null)
         animate=null;
     }
     
     public void run()
     {
       int dy,num=0;
       double d;
      
       while(true){
         d=num*Math.PI/12;
         for(int i=0; i<height;i++)
         {
                     {dy=(int)((i/12.0D+1)*Math.sin(height/12.0D*(height-i)/(i-1)+d));}
           gContext.copyArea(width,i+dy,width,1,-width,-dy);
          }
         repaint();
         num=++num%12;
         try{
          Thread.sleep(50);
          }catch(InterruptedException e){}
  }

}
 public void update(Graphics g)
 {
   paint(g);
 }
 public void paint(Graphics g)
 {
   g.drawImage(img,0,-height,this);
   g.drawImage(buffer,0,height,this);
 }
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -