home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 March / pcp161b.iso / handson / archive / Issue153 / java / Lake.class (.txt) < prev    next >
Encoding:
Java Class File  |  1999-04-05  |  8.6 KB  |  377 lines

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