home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1997 October / PCO1097.ISO / FilesBBS / FREI / MB-JAVA.EXE / WobbleCanvas.class (.txt) < prev   
Encoding:
Java Class File  |  1996-11-20  |  3.6 KB  |  126 lines

  1. import java.awt.Canvas;
  2. import java.awt.Component;
  3. import java.awt.Graphics;
  4. import java.awt.Image;
  5. import java.awt.MediaTracker;
  6. import java.awt.image.MemoryImageSource;
  7. import java.awt.image.PixelGrabber;
  8.  
  9. class WobbleCanvas extends Canvas {
  10.    Image sourceImage;
  11.    Image destImage;
  12.    int widthImage;
  13.    int heightImage;
  14.    MediaTracker tracker;
  15.    int[] sourcePixels;
  16.    int[] destPixels;
  17.    double RadiansPerDegree = (Math.PI / 180D);
  18.    private int delta;
  19.    private double amplitude = (double)100.0F;
  20.    private int baseline;
  21.    private MemoryImageSource producer;
  22.    private int color;
  23.  
  24.    public WobbleCanvas(Image image, int colorValue) {
  25.       this.sourceImage = image;
  26.       this.color = colorValue;
  27.       this.tracker = new MediaTracker(this);
  28.       this.tracker.addImage(image, 0);
  29.       ((Component)this).reshape(0, 0, 500, 100);
  30.    }
  31.  
  32.    protected void finalize() throws Throwable {
  33.       this.tracker = null;
  34.       this.destImage = null;
  35.       this.destPixels = null;
  36.       this.sourcePixels = null;
  37.       super.finalize();
  38.    }
  39.  
  40.    public void moveDelta(int direction) {
  41.       this.delta += direction;
  42.    }
  43.  
  44.    public void setAmplitude(double value) {
  45.       this.amplitude = value;
  46.    }
  47.  
  48.    public void setBaseline(int value) {
  49.       this.baseline = value;
  50.    }
  51.  
  52.    public void setColor(int value) {
  53.       this.color = value;
  54.    }
  55.  
  56.    protected void HorizLine(int Offset, int width) {
  57.       int SH = 16;
  58.       int add = (this.widthImage << SH) / width;
  59.       int srcOffset;
  60.       int destOffset;
  61.       if (width > this.widthImage) {
  62.          srcOffset = Offset + (width >> 1) - (this.widthImage >> 1) << SH;
  63.          destOffset = Offset;
  64.          width = this.widthImage;
  65.       } else {
  66.          srcOffset = Offset << SH;
  67.          destOffset = Offset + (this.widthImage >> 1) - (width >> 1);
  68.       }
  69.  
  70.       for(int x = 0; x < width; ++x) {
  71.          this.destPixels[destOffset] = this.sourcePixels[srcOffset >>> SH];
  72.          ++destOffset;
  73.          srcOffset += add;
  74.       }
  75.  
  76.    }
  77.  
  78.    public void paintScreen() {
  79.       int y = 0;
  80.  
  81.       for(int Offset = 0; y < this.heightImage; Offset += this.widthImage) {
  82.          int value = (int)(Math.sin((double)(y + this.delta) * this.RadiansPerDegree) * this.amplitude);
  83.          this.HorizLine(Offset, value + this.widthImage + this.baseline);
  84.          ++y;
  85.       }
  86.  
  87.    }
  88.  
  89.    public void update(Graphics g) {
  90.       if ((this.tracker.statusAll(true) & 8) == 0) {
  91.          g.drawString("(c)1996 MYSTiC BYTES. http://www.mystic.de - Loading...", 0, 50);
  92.       } else {
  93.          if (this.destPixels == null) {
  94.             this.widthImage = this.sourceImage.getWidth(this);
  95.             this.heightImage = this.sourceImage.getHeight(this);
  96.             ((Component)this).reshape(0, 0, this.widthImage, this.heightImage);
  97.             this.sourcePixels = new int[this.widthImage * this.heightImage];
  98.             PixelGrabber pg = new PixelGrabber(this.sourceImage, 0, 0, this.widthImage, this.heightImage, this.sourcePixels, 0, this.widthImage);
  99.  
  100.             try {
  101.                pg.grabPixels();
  102.             } catch (InterruptedException var4) {
  103.                System.err.println("interrupted waiting for pixels!");
  104.                return;
  105.             }
  106.  
  107.             this.destPixels = new int[this.widthImage * this.heightImage];
  108.  
  109.             for(int index = 0; index < this.widthImage * this.heightImage; ++index) {
  110.                this.destPixels[index] = -16777216 + this.color;
  111.             }
  112.  
  113.             this.producer = new MemoryImageSource(this.widthImage, this.heightImage, this.destPixels, 0, this.widthImage);
  114.          }
  115.  
  116.          this.paintScreen();
  117.          this.destImage = ((Component)this).createImage(this.producer);
  118.          g.drawImage(this.destImage, 0, 0, this);
  119.       }
  120.    }
  121.  
  122.    public void paint(Graphics g) {
  123.       this.update(g);
  124.    }
  125. }
  126.