🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: cn.bbs.comp.lang.java
Article #24350

ͼ����תԴ�����(ת��)

#24350
From: wooce
Date: Tue, 24 Mar 1998 00:00
252 lines
7060 bytes



// applet "Whirls  an Image"

// needs
import java.applet.* ;
import java.awt.image.* ;
import java.awt.Image ;
import java.awt.image.PixelGrabber;
import java.awt.MediaTracker ; 
import java.awt.image.MemoryImageSource ;
import java.awt.Graphics ;

public final class Whirling extends Applet implements Runnable {
  static String
    message = "...courtesy Taylor Computing (http://taylor.digex.net)";
  private Thread
    whirling_thread = null;
  private AudioClip
    audio_clip = null;
  private Image 
    base_image,
    whirling_image;
                
  public void init() {
    MediaTracker
        tracker = new MediaTracker(this);
    String
        audio_file;

    // who
    showStatus(message);

    // base_image
    base_image = getImage(getDocumentBase(), getParameter("img"));
    tracker.addImage(base_image, 0);
    whirling_image = base_image;

    // who
    showStatus(message);

    // load it all before continuing
    try { tracker.waitForAll(); } catch (InterruptedException e){} ;

    // so to see
    prepareImage(base_image, this);
    repaint(0);

    // audio
    audio_file = getParameter("audio");
    if ( audio_file != null ) {
        audio_clip = getAudioClip( getDocumentBase(),  audio_file);
    }

    // who
    showStatus(message);

   }

  public void paint(Graphics g) {

    g.drawImage(whirling_image, 0, 0, this);

  }

  public void run() {
     int
        wstep_i,wstep_n=10,wstep_height;
     int
        image_cx, image_cy, image_width, image_height;
     int
        image_pixels[];
     Image
        whirling_images[];

     image_width = base_image.getWidth(this);
     image_height = base_image.getHeight(this);
     image_cx = image_width/2;
     image_cy = image_height/2;

     image_pixels = new int[image_width * image_height];

     //# Create a PixelGrabber to Get the Pixels of the image and store
     //# them into the image_pixels array
     PixelGrabber pixel_grabber = new PixelGrabber(base_image.getSource(), 0, 0,
              image_width, image_height, image_pixels, 0, image_width);

     try {
      pixel_grabber.grabPixels();
     } catch (InterruptedException e) {;}

     pixel_grabber = null;

     // copy the base
     // base_copy = new LoadImage(base_image);

     // "whirl the image"
     whirling_images = new Image[wstep_n];
     wstep_height = image_height/wstep_n;

     // first is base...then make the rest
     {
       double
         dim = Math.min(image_width,image_height),
         dialation_step = 0.25*(dim/wstep_n),
         dialation = dialation_step,
         times_around = 2.0,
         freq_step = (times_around*2.0*Math.PI)/wstep_n,
         freq = freq_step;

       // original
       whirling_images[0] = base_image;

       // the rest
       for(wstep_i=1;

           wstep_i<wstep_n; 

           wstep_i++,
             dialation += dialation_step,
           freq += freq_step) {
         int 
           to_pixels[] = new int[image_width*image_height];
 
         // fill
         {
           int
             from_x, from_y,
             to_index, to_x, to_y;

           // resample in a goofy way
           for(to_index=0,to_y=0;to_y<image_height;to_y++) {
             double 
               y = (double)(image_cy - to_y),
               y_2 = y*y;

             for(to_x=0;to_x<image_width;to_x++,to_index++) {
               double
                 x = (double)(image_cx - to_x),
                 r,theta;

               // get polar
               theta = Math.atan2(x,y);
               r = Math.sqrt( (x*x) + y_2 );

               // get from_x,from_y, shift in theta, dialate
               theta += (r/dim)*freq;
               r += dialation;

               if ( r < 0.0 ) { 
                 from_x = image_cx;
                 from_y = image_cy;
               } else {
                 from_x = (int)(r * Math.cos(theta)) + image_cx;
                 from_y = (int)(r * Math.sin(theta)) + image_cy;
               }

               // clip
               if (from_y<0)
                 from_y = 0;
               else if (from_y >= image_height)
                 from_y = image_height - 1;

               // clip
               if (from_x<0)
                 from_x = 0;
               else if (from_x >= image_width)
                 from_x = image_width - 1;

               to_pixels[to_index] = image_pixels[ (from_y * image_width) + from_x];

             }// end for x

           }// end for y
         }
                
         // make it
         whirling_images[wstep_i] = createImage(new MemoryImageSource(image_width,image_height,to_pixels,0,image_width));

         // don't need
         to_pixels = null;

         // make it real
         prepareImage(whirling_images[wstep_i], this);

       }
     }

     // gc just for fun
     Runtime.getRuntime().gc();

     // loop every ??sec 
     try { whirling_thread.sleep(5*1000) ; } catch(InterruptedException e){} ;
     while( true ) {

        // play audio if you have some
        if (audio_clip != null) {
               audio_clip.play();
               try { whirling_thread.sleep(1*1000) ; } catch(InterruptedException e){} ;
        }

        // whirl
        for(wstep_i=1;wstep_i<wstep_n;wstep_i++) {
                whirling_image = whirling_images[wstep_i];
                repaint(0);
                showStatus("I am whirling!");
                try { whirling_thread.sleep(300) ; } catch(InterruptedException e){} ;
        }// end for 

        // try { whirling_thread.sleep(5*1000) ; } catch(InterruptedException e){} ;

       // un-whirl
        for(wstep_i=wstep_n-1;wstep_i>-1;wstep_i--) {
                whirling_image = whirling_images[wstep_i];
                repaint(0);
                try { whirling_thread.sleep(300) ; } catch(InterruptedException e){} ;
        }// end for 

        // who
        showStatus("Better.");

        try { whirling_thread.sleep(10*1000) ; } catch(InterruptedException e){} ;

     }// end while

  }

  public void start() {
    if (whirling_thread == null) {
        whirling_thread = new Thread(this);
        whirling_thread.start();
     }

    // who
    showStatus(message);
  }

  public void stop() {
        
    whirling_thread.stop();
    whirling_thread = null;

    // who
    showStatus(message);
  }

} // end Whirling

--
�� ��Դ:���������� BBS bbs.nease.net��[FROM: 202.96.174.34]
Technical Details
Message-ID: <3Nc9Dj$7ea@bbs.nease.net>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII
X-Google-Thread: f6cb0,8f1df17b2fe3e86a,start
X-Google-Attributes: gidf6cb0,public
From: wooce.bbs@bbs.nease.net (wooce)
Subject: ͼ����תԴ�����(ת��)
Date: 1998/03/24
Message-ID: <3Nc9Dj$7ea@bbs.nease.net>
X-Deja-AN: 337116361
X-Filename: Java/M.890729373.A
Organization: Net Ease
Newsgroups: cn.bbs.comp.lang.java,