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

📄 ripple.java

📁 JavaApplet实例编程源代码, 里面有好几章
💻 JAVA
字号:
import java.applet.*;
import java.awt.*;

public class Ripple extends Applet implements Runnable
{
   Thread thread = null;
   private Graphics g, refraction;
   private Image image, refimage;
   private int 	 currentImg;
   private int 	 imageW  = 0, imageH = 0;
   private int 	 ovalW  = 0, ovalH = 0;
	
   private boolean  finishLoad = false;
   private final int frames = 12;

   private String name = "";

   public void init(){
      String param;

      param = getParameter("image");
      if (param != null)
         name = param;
      }


   public void paint(Graphics g){
      if (!finishLoad)
         return;

      if (refimage != null) {
         g.drawImage (refimage, (-currentImg * imageW), imageH, this);
         g.drawImage (refimage, ((frames-currentImg) * imageW), imageH, this);
      }
      g.drawImage (image, 0, -1, this);

   }

   public void start(){
      if (thread == null){
         thread = new Thread(this);
	 thread.start();
      }
   }
	
   public void run(){
      currentImg = 0;
		
    	 g = getGraphics();
    	 MediaTracker imageTracker = new MediaTracker(this);
    	 String strImage;
    	 
         image = getImage(getDocumentBase(), name);
         imageTracker.addImage(image,0);
       	 try{
  	    imageTracker.waitForAll();
	    finishLoad = !imageTracker.isErrorAny();
	 }
	 catch (InterruptedException e) {}

         imageW  = image.getWidth(this);
         System.out.println(imageW);
	 imageH = image.getHeight(this);
	 createRipple();

      repaint();
      while (true){
         try{
            if (!finishLoad)
               return;

            if (refimage != null) {
               g.drawImage (refimage, (-currentImg * imageW), imageH, this);
               g.drawImage (refimage, ((frames-currentImg) * imageW), imageH, this);
            }
            g.drawImage (image, 0, -1, this);

     	    if (++currentImg == frames)
  	       currentImg = 0;
  	    Thread.sleep(50);
         }catch (InterruptedException e){
 	     stop();
         }
      }
  }


    public void createRipple () {
    
        Image back = createImage (imageW, imageH + 1);
        Graphics offg = back.getGraphics();
        int phase = 0;
        int x, y;
        double p1;
        offg.drawImage (image, 0, 1, this);
        for (int i = 0; i < (imageH >> 1); i++) {
           offg.copyArea (0, i, imageW, 1, 0, imageH - i);
           offg.copyArea (0, imageH - 1 - i, imageW, 1,0, -imageH + 1 + (i << 1));
           offg.copyArea (0, imageH, imageW, 1, 0, -1 - i);
        }
        refimage = createImage ((frames + 1) * imageW, imageH);
        refraction = refimage.getGraphics();
        refraction.drawImage (back, frames * imageW, 0, this);

        for (phase = 0; phase < frames; phase++) {
       
           p1 = 2 * Math.PI * (double)phase / (double)frames;
           x = (frames - phase) * imageW;
           for (int i = 0; i < imageH; i++) {
              y = (int)((imageH/14) * ((double) i + 28.0)
                       * Math.sin ((double)((imageH/14)*(imageH - i))/(double)(i + 1)
				                              + p1)/ (double) imageH);
              if (i < -y)
   	         refraction.copyArea (frames * imageW, i, imageW, 1,-x, 0);
              else
	         refraction.copyArea (frames * imageW, i + y,imageW, 1, -x, -y);
          }
        }

	offg.drawImage (image, 0, 1, this);

	image = back;
    }

 } 

⌨️ 快捷键说明

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