home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 March / pcp161b.iso / handson / archive / Issue159 / files / copyjava.exe / ex2 / GIFLake.class (.txt) < prev    next >
Encoding:
Java Class File  |  1999-10-03  |  9.7 KB  |  416 lines

  1. import java.applet.Applet;
  2. import java.awt.Component;
  3. import java.awt.Dimension;
  4. import java.awt.Graphics;
  5. import java.awt.Image;
  6. import java.awt.MediaTracker;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.io.PrintStream;
  10. import java.net.MalformedURLException;
  11. import java.net.URL;
  12.  
  13. public class GIFLake extends Applet implements Runnable {
  14.    private int boatHeight;
  15.    private boolean keepRunning = true;
  16.    private int captureCount;
  17.    // $FF: renamed from: g1 Animator
  18.    private Animator field_0;
  19.    private transient Thread thrLake;
  20.    private static final String PARAM_IMAGE = "image";
  21.    private static final String PARAM_OVERLAY = "overlay";
  22.    private static final String PARAM_TARGET = "target";
  23.    private static final String PARAM_HREF = "hRef";
  24.    private static final String PARAM_ROCKING = "rocking";
  25.    private static final String PARAM_UNDERWATER = "underwater";
  26.    private static final String PARAM_SPEED = "speed";
  27.    private Image image;
  28.    private Image overlay;
  29.    private MediaTracker overlayTracker;
  30.    private String target = "_self";
  31.    private URL hRef;
  32.    private boolean rocking = false;
  33.    private boolean underwater = false;
  34.    private int speed = 50;
  35.    private int numFrames = 12;
  36.    private transient Graphics gMain;
  37.    private transient Graphics gWave;
  38.    private transient Image imgWave;
  39.    private transient int currImage;
  40.    private transient int widthImage;
  41.    private transient int heightImage;
  42.    private transient int widthOverlay;
  43.    private transient int heightOverlay;
  44.    private transient boolean allLoaded = false;
  45.    private transient boolean isAnimating = true;
  46.    private transient int boatPhase;
  47.    private transient int boatPhaseTotal = 50;
  48.  
  49.    public void initAnimation() {
  50.       Dimension var1 = ((Component)this).getSize();
  51.       this.field_0 = Animator.getInstance("GIF", var1.width, var1.height);
  52.       this.field_0.setFPS(40);
  53.    }
  54.  
  55.    public void startCapture() {
  56.       this.captureCount = 12;
  57.    }
  58.  
  59.    public void captureFrame() {
  60.       if (this.captureCount > 0) {
  61.          this.field_0.addImage(this.imgWave, this);
  62.          if (--this.captureCount == 0) {
  63.             try {
  64.                this.field_0.getAnimation().serializeTo(new PrintStream(new FileOutputStream("lake.gif")));
  65.                return;
  66.             } catch (IOException var1) {
  67.                return;
  68.             }
  69.          }
  70.       }
  71.  
  72.    }
  73.  
  74.    public void start() {
  75.       this.keepRunning = true;
  76.       if (this.thrLake == null) {
  77.          this.thrLake = new Thread(this);
  78.          this.thrLake.start();
  79.       }
  80.  
  81.    }
  82.  
  83.    public void stop() {
  84.       this.keepRunning = false;
  85.    }
  86.  
  87.    public void run() {
  88.       this.currImage = 0;
  89.       this.initAnimation();
  90.  
  91.       while(this.keepRunning) {
  92.          try {
  93.             while(!this.isAnimating) {
  94.                Thread.sleep(500L);
  95.             }
  96.  
  97.             if (++this.currImage == this.numFrames) {
  98.                this.currImage = 0;
  99.             }
  100.  
  101.             if (++this.boatPhase == this.boatPhaseTotal) {
  102.                this.boatPhase = 0;
  103.             }
  104.  
  105.             this.displayImage();
  106.             this.captureFrame();
  107.             ((Component)this).repaint();
  108.             Thread.sleep(30L);
  109.          } catch (InterruptedException var1) {
  110.             this.stop();
  111.          }
  112.       }
  113.  
  114.    }
  115.  
  116.    public String getAppletInfo() {
  117.       return "Name: Lake Version 3.1\r\nAuthor: David Griffiths\r\nis an applet class which takes in an \r\nimage and reflects it in a virtual Lake.\r\nLast compiled: 4th April 1999 at 08:27\t\r\nFor more information about this and other applets\r\ngo to http://www.demon.co.uk/davidg/spigots.htm\r\nCreated with Sun JDK 1.1";
  118.    }
  119.  
  120.    public Image getImage() {
  121.       return this.image;
  122.    }
  123.  
  124.    public void setImage(Image var1) {
  125.       this.image = var1;
  126.       this.widthImage = this.image.getWidth(this);
  127.       this.heightImage = this.image.getHeight(this);
  128.       this.allLoaded = true;
  129.       this.createAnimation();
  130.    }
  131.  
  132.    public void setImageValue(String var1) {
  133.       this.setImage(((Applet)this).getImage(((Applet)this).getDocumentBase(), var1));
  134.    }
  135.  
  136.    public boolean imageUpdate(Image var1, int var2, int var3, int var4, int var5, int var6) {
  137.       boolean var7 = super.imageUpdate(var1, var2, var3, var4, var5, var6);
  138.       if (var1 == this.image) {
  139.          int var8 = this.heightImage;
  140.          if ((var2 & 48) != 0) {
  141.             this.widthImage = var5;
  142.             this.heightImage = var6;
  143.          } else {
  144.             this.widthImage = ((Component)this).getSize().width;
  145.             this.heightImage = 10 * ((Component)this).getSize().height / 18;
  146.          }
  147.  
  148.          if (var8 != this.heightImage) {
  149.             this.createAnimation();
  150.          }
  151.       }
  152.  
  153.       return var7;
  154.    }
  155.  
  156.    public Image getOverlay() {
  157.       return this.overlay;
  158.    }
  159.  
  160.    public void setOverlay(Image var1) {
  161.       this.overlay = var1;
  162.       this.overlayTracker = new MediaTracker(this);
  163.       this.overlayTracker.addImage(var1, 0);
  164.    }
  165.  
  166.    public void setOverlayValue(String var1) {
  167.       this.setOverlay(((Applet)this).getImage(((Applet)this).getDocumentBase(), var1));
  168.    }
  169.  
  170.    private boolean overlayReady() {
  171.       if (this.overlayTracker != null) {
  172.          return this.overlayTracker.statusID(0, true) == 8;
  173.       } else {
  174.          return false;
  175.       }
  176.    }
  177.  
  178.    public String getTarget() {
  179.       return this.target;
  180.    }
  181.  
  182.    public void setTarget(String var1) {
  183.       this.target = var1;
  184.    }
  185.  
  186.    public void setTargetValue(String var1) {
  187.       this.setTarget(var1);
  188.    }
  189.  
  190.    public URL getHRef() {
  191.       return this.hRef;
  192.    }
  193.  
  194.    public void setHRef(URL var1) {
  195.       this.hRef = var1;
  196.    }
  197.  
  198.    public void setHRefValue(String var1) {
  199.       this.setHRef(this.createURL(var1));
  200.    }
  201.  
  202.    private URL createURL(String var1) {
  203.       URL var2 = null;
  204.       if (var1 != null) {
  205.          try {
  206.             var2 = new URL(((Applet)this).getDocumentBase(), var1);
  207.          } catch (MalformedURLException var3) {
  208.             this.error("Bad URL: " + var1);
  209.             var2 = null;
  210.          }
  211.       }
  212.  
  213.       return var2;
  214.    }
  215.  
  216.    public boolean isRocking() {
  217.       return this.rocking;
  218.    }
  219.  
  220.    public void setRocking(boolean var1) {
  221.       this.rocking = var1;
  222.    }
  223.  
  224.    public void setRockingValue(String var1) {
  225.       this.setRocking(var1.toUpperCase().equals("TRUE"));
  226.    }
  227.  
  228.    public void toggleRocking() {
  229.       this.setRocking(!this.isRocking());
  230.    }
  231.  
  232.    public boolean isUnderwater() {
  233.       return this.underwater;
  234.    }
  235.  
  236.    public void setUnderwater(boolean var1) {
  237.       this.underwater = var1;
  238.    }
  239.  
  240.    public void setUnderwaterValue(String var1) {
  241.       this.setUnderwater(var1.toUpperCase().equals("TRUE"));
  242.    }
  243.  
  244.    public void toggleUnderwater() {
  245.       this.setUnderwater(!this.isUnderwater());
  246.    }
  247.  
  248.    public int getSpeed() {
  249.       return this.speed;
  250.    }
  251.  
  252.    public void setSpeed(int var1) {
  253.       if (var1 > 100) {
  254.          var1 = 100;
  255.       } else if (var1 < 1) {
  256.          var1 = 1;
  257.       }
  258.  
  259.       this.speed = var1;
  260.       this.numFrames = 600 / var1;
  261.    }
  262.  
  263.    public void setSpeedValue(String var1) {
  264.       this.setSpeed(Integer.parseInt(var1));
  265.    }
  266.  
  267.    public String[][] getParameterInfo() {
  268.       String[][] var1 = new String[][]{{"image", "Image", "JPG or GIF file to reflect"}, {"overlay", "Image", "JPG or GIF file to use as an overlay"}, {"target", "String", "Target frame"}, {"hRef", "URL", "URL to link to"}, {"rocking", "boolean", "TRUE if boat rocking"}, {"underwater", "boolean", "TRUE if viewer underwater"}, {"speed", "int", "The animation speed: 1-100"}};
  269.       return var1;
  270.    }
  271.  
  272.    private void loadParams() {
  273.       String var1 = ((Applet)this).getParameter("image");
  274.       if (var1 != null) {
  275.          this.setImageValue(var1);
  276.       }
  277.  
  278.       var1 = ((Applet)this).getParameter("overlay");
  279.       if (var1 != null) {
  280.          this.setOverlayValue(var1);
  281.       }
  282.  
  283.       var1 = ((Applet)this).getParameter("target");
  284.       var1 = ((Applet)this).getParameter("hRef");
  285.       if (var1 != null) {
  286.          this.setHRefValue(var1);
  287.       }
  288.  
  289.       var1 = ((Applet)this).getParameter("rocking");
  290.       if (var1 != null) {
  291.          this.setRockingValue(var1);
  292.       }
  293.  
  294.       var1 = ((Applet)this).getParameter("underwater");
  295.       if (var1 != null) {
  296.          this.setUnderwaterValue(var1);
  297.       }
  298.  
  299.       var1 = ((Applet)this).getParameter("speed");
  300.       if (var1 != null) {
  301.          this.setSpeedValue(var1);
  302.       }
  303.  
  304.    }
  305.  
  306.    public void init() {
  307.       ((Component)this).repaint();
  308.       System.out.println(this.getAppletInfo());
  309.       this.loadParams();
  310.       this.allLoaded = true;
  311.       ((Component)this).addMouseListener(new GIFLake$1(this));
  312.    }
  313.  
  314.    public Dimension getPreferredSize() {
  315.       return new Dimension(this.widthImage, (int)((double)this.heightImage * 1.8));
  316.    }
  317.  
  318.    private void error(String var1) {
  319.       ((Applet)this).getAppletContext().showStatus(var1);
  320.    }
  321.  
  322.    public void update(Graphics var1) {
  323.       this.paint(var1);
  324.    }
  325.  
  326.    public void paint(Graphics var1) {
  327.       if (this.imgWave != null) {
  328.          var1.drawImage(this.imgWave, 0, this.boatHeight, this);
  329.       }
  330.  
  331.    }
  332.  
  333.    private void displayImage() {
  334.       if (this.isRocking()) {
  335.          this.boatHeight = (int)((double)this.heightImage * Math.sin((Math.PI * 2D) * (double)this.boatPhase / (double)this.boatPhaseTotal) / (double)8.0F) - this.heightImage / 8;
  336.       } else {
  337.          this.boatHeight = 0;
  338.       }
  339.  
  340.       if (this.imgWave != null) {
  341.          if (this.isUnderwater()) {
  342.             this.gWave.drawImage(this.image, 0, ((Component)this).getSize().height - this.heightImage, this);
  343.             this.makeWavesInverse(this.gWave, this.currImage);
  344.             this.boatHeight = -this.boatHeight;
  345.          } else {
  346.             this.gWave.drawImage(this.image, 0, 0, this);
  347.             this.makeWaves(this.gWave, this.currImage);
  348.          }
  349.  
  350.          if (this.overlay != null && this.overlayReady()) {
  351.             this.gWave.drawImage(this.overlay, this.widthImage - this.overlay.getWidth(this) >> 1, this.heightImage - (this.overlay.getHeight(this) >> 1) + this.boatHeight, this);
  352.          }
  353.       }
  354.  
  355.    }
  356.  
  357.    private void createAnimation() {
  358.       if (this.widthImage > 0 && this.heightImage > 0) {
  359.          synchronized(this){}
  360.  
  361.          try {
  362.             this.imgWave = ((Component)this).createImage(this.widthImage, 2 * this.heightImage);
  363.             this.gWave = this.imgWave.getGraphics();
  364.          } catch (Throwable var3) {
  365.             throw var3;
  366.          }
  367.       }
  368.  
  369.       ((Component)this).repaint();
  370.    }
  371.  
  372.    private void makeWaves(Graphics var1, int var2) {
  373.       double var3 = (Math.PI * 2D) * (double)var2 / (double)this.numFrames;
  374.  
  375.       for(int var5 = 0; var5 < this.heightImage; ++var5) {
  376.          int var6 = (int)((double)(this.heightImage / 14) * ((double)var5 + (double)28.0F) * Math.sin((double)(this.heightImage / 14 * (this.heightImage - var5)) / (double)(var5 + 1) + var3) / (double)this.heightImage);
  377.          if (var5 - this.heightImage > var6) {
  378.             var1.copyArea(0, this.heightImage - var5, this.widthImage, 1, 0, var5 << 1);
  379.          } else {
  380.             var1.copyArea(0, this.heightImage - var5 + var6, this.widthImage, 1, 0, (var5 << 1) - var6);
  381.          }
  382.       }
  383.  
  384.    }
  385.  
  386.    private void makeWavesInverse(Graphics var1, int var2) {
  387.       double var3 = (Math.PI * 2D) * (double)var2 / (double)this.numFrames;
  388.       int var5 = ((Component)this).getSize().height;
  389.  
  390.       for(int var6 = 0; var6 < this.heightImage; ++var6) {
  391.          int var7 = (int)((double)(this.heightImage / 14) * ((double)var6 + (double)28.0F) * Math.sin((double)(this.heightImage / 14 * (this.heightImage - var6)) / (double)(var6 + 1) + var3) / (double)this.heightImage);
  392.          if (var6 - this.heightImage > var7) {
  393.             var1.copyArea(0, var5 - this.heightImage + var6, this.widthImage, 1, 0, -(var6 << 1));
  394.          } else {
  395.             var1.copyArea(0, var5 - this.heightImage + var6 - var7, this.widthImage, 1, 0, var7 - (var6 << 1));
  396.          }
  397.       }
  398.  
  399.    }
  400.  
  401.    // $FF: synthetic method
  402.    static URL access$0(GIFLake var0) {
  403.       return var0.hRef;
  404.    }
  405.  
  406.    // $FF: synthetic method
  407.    static void access$1(GIFLake var0, String var1) {
  408.       var0.error(var1);
  409.    }
  410.  
  411.    // $FF: synthetic method
  412.    static String access$2(GIFLake var0) {
  413.       return var0.target;
  414.    }
  415. }
  416.