home *** CD-ROM | disk | FTP | other *** search
Java Source | 1999-04-05 | 14.2 KB | 597 lines |
- /*
- * @(#)Lake.java 1999/03/04
- *
- * Copyright (c) 1999, David Griffiths. All Rights Reserved.
- *
- * This software is the proprietary information of David Griffiths.
- * This source code may not be published or redistributed without the
- * express permission of the author.
- *
- * THE AUTHOR MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY
- * OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
- * THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
- * PURPOSE, OR NON-INFRINGEMENT. THE AUTHOR SHALL NOT BE LIABLE FOR ANY DAMAGES
- * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
- * THIS SOFTWARE OR ITS DERIVATIVES.
- */
-
- /**
- * Lake is an applet class which takes in an
- * image and reflects it in a virtual Lake.
- * @version 3.1 1999/03/04
- * @author David Griffiths
- */
-
- import java.lang.String;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.awt.*;
- import java.awt.event.*;
- import java.applet.Applet;
- import java.applet.AppletContext;
- import java.io.*;
- import java.net.*;
- import java.awt.image.MemoryImageSource;
-
- public class Lake extends Applet implements Runnable {
- private int boatHeight;
-
- private boolean keepRunning = true;
-
-
- ///////////////////////
- // //
- // THREAD MANAGEMENT //
- // //
- ///////////////////////
- transient private Thread thrLake;
-
- public void start() {
- keepRunning = true;
- if (thrLake == null) {
- thrLake = new Thread(this);
- thrLake.start();
- }
- }
-
- public void stop() {
- keepRunning = false;
- }
-
- public void run() {
- currImage = 0;
- while (keepRunning) {
- try {
- while (!isAnimating)
- Thread.sleep(500);
- if (++currImage == numFrames)
- currImage = 0;
- if (++boatPhase == boatPhaseTotal)
- boatPhase = 0;
- displayImage();
- repaint();
- Thread.sleep(30);
- }
- catch (InterruptedException e2) {
- stop();
- }
- }
- }
-
-
-
- /**
- * Information about this applet.
- */
- public String getAppletInfo() {
- return "Name: Lake Version 3.1\r\n"
- + "Author: David Griffiths\r\n"
- + "is an applet class which takes in an \r\n"
- + "image and reflects it in a virtual Lake.\r\n"
- + "Last compiled: 4th April 1999 at 08:27 \r\n"
- + "For more information about this and other applets\r\n"
- + "go to http://www.demon.co.uk/davidg/spigots.htm\r\n"
- + "Created with Sun JDK 1.1";
- }
-
-
- ////////////////////////
- // //
- // ATTRIBUTES SECTION //
- // //
- ////////////////////////
- private final static String PARAM_IMAGE = "image";
- private final static String PARAM_OVERLAY = "overlay";
- private final static String PARAM_TARGET = "target";
- private final static String PARAM_HREF = "hRef";
- private final static String PARAM_ROCKING = "rocking";
- private final static String PARAM_UNDERWATER = "underwater";
- private final static String PARAM_SPEED = "speed";
-
- /**
- * Declaration of the image attribute (JPG or GIF file to reflect)
- * and the definition of its getter and setter methods.
- */
-
- private Image image = null;
-
- /**
- * Returns JPG or GIF file to reflect
- * @return This Lake's image
- * @see #setImage
- * @since JDK1.0
- */
- public Image getImage() {
- return image;
- }
-
- /**
- * Sets JPG or GIF file to reflect
- * @param <code>image</code> the Image that is to be the Lake's image
- * @see #getImage
- * @since JDK1.0
- */
- public void setImage(Image image) {
- this.image = image;
- widthImage = this.image.getWidth(this); // Start it loading
- heightImage = this.image.getHeight(this); // Start it loading
- allLoaded = true;
- createAnimation();
- }
-
- /**
- * Sets the JPG or GIF file to reflect
- * @param <code>image</code> the String representing the Lake's image
- * @see #getImage
- * @since JDK1.0
- */
-
- public void setImageValue(String strImage) {
- setImage(getImage(getDocumentBase(), strImage));
- }
-
- /**
- * Implementation of imageUpdate method so that we
- * can have asynchronous image loading.
- */
- public boolean imageUpdate(Image img, int flags,
- int x, int y, int w, int h) {
- boolean status = super.imageUpdate(img, flags, x, y, w, h);
-
- if (img == image) {
- int oldImageHeight = heightImage;
-
- if ((flags & (FRAMEBITS|ALLBITS)) != 0) { // if complete frame loaded
- widthImage = w;
- heightImage = h;
- }
- else { // guess dimensions
- widthImage = size().width;
- heightImage = 10 * size().height / 18;
- }
- if (oldImageHeight != heightImage)
- createAnimation();
- }
-
- return status;
- }
-
- /**
- * Declaration of the overlay attribute (JPG or GIF file to use as an overlay)
- * and the definition of its getter and setter methods.
- */
-
- private Image overlay = null;
- private MediaTracker overlayTracker = null;
-
- /**
- * Returns JPG or GIF file to use as an overlay
- * @return This Lake's overlay
- * @see #setOverlay
- * @since JDK1.0
- */
- public Image getOverlay() {
- return overlay;
- }
-
- /**
- * Sets JPG or GIF file to use as an overlay
- * @param <code>overlay</code> the Image that is to be the Lake's overlay
- * @see #getOverlay
- * @since JDK1.0
- */
- public void setOverlay(Image overlay) {
- this.overlay = overlay;
- overlayTracker = new MediaTracker(this);
- overlayTracker.addImage(overlay, 0);
- }
-
- public void setOverlayValue(String strImage) {
- setOverlay(getImage(getDocumentBase(), strImage));
- }
-
- private boolean overlayReady() {
- if (overlayTracker != null)
- return (overlayTracker.statusID(0, true) == MediaTracker.COMPLETE);
- else
- return false;
- }
-
- /**
- * Declaration of the target attribute (Target frame)
- * and the definition of its getter and setter methods.
- */
-
- private String target = "_self";
-
- /**
- * Returns Target frame
- * @return This Lake's target
- * @see #setTarget
- * @since JDK1.0
- */
- public String getTarget() {
- return target;
- }
-
- /**
- * Sets Target frame
- * @param <code>target</code> the String that is to be the Lake's target
- * @see #getTarget
- * @since JDK1.0
- */
- public void setTarget(String target) {
- this.target = target;
- }
-
- public void setTargetValue(String target) {
- setTarget(target);
- }
-
-
- /**
- * Declaration of the hRef attribute (URL to link to)
- * and the definition of its getter and setter methods.
- */
-
- private URL hRef;
-
- /**
- * Returns URL to link to
- * @return This Lake's hRef
- * @see #setHRef
- * @since JDK1.0
- */
- public URL getHRef() {
- return hRef;
- }
-
- /**
- * Sets URL to link to
- * @param <code>hRef</code> the URL that is to be the Lake's hRef
- * @see #getHRef
- * @since JDK1.0
- */
- public void setHRef(URL hRef) {
- this.hRef = hRef;
- }
-
-
- /**
- * Sets URL to link to
- * @param <code>hRef</code> the URL that is to be the Lake's hRef
- * @see #getHRef
- * @since JDK1.0
- */
- public void setHRefValue(String u) {
- setHRef(createURL(u));
- }
-
-
- private URL createURL(String u) {
- URL createURL = null;
-
- if (u != null) {
- try {
- createURL = new URL(getDocumentBase(), u);
- }
- catch (MalformedURLException e) {
- error("Bad URL: " + u);
- createURL = null;
- }
- }
- return createURL;
- }
-
- /**
- * Declaration of the rocking attribute (TRUE if boat rocking)
- * and the definition of its getter and setter methods.
- */
-
- private boolean rocking = false;
-
- /**
- * Returns TRUE if boat rocking
- * @return This Lake's rocking
- * @see #setRocking
- * @since JDK1.0
- */
- public boolean isRocking() {
- return rocking;
- }
-
- /**
- * Sets TRUE if boat rocking
- * @param <code>rocking</code> the boolean that is to be the Lake's rocking
- * @see #isRocking
- * @since JDK1.0
- */
- public void setRocking(boolean rocking) {
- this.rocking = rocking;
- }
-
- public void setRockingValue(String r) {
- setRocking(r.toUpperCase().equals("TRUE"));
- }
-
- public void toggleRocking() {
- setRocking(!isRocking());
- }
-
- /**
- * Declaration of the underwater attribute (TRUE if viewer underwater)
- * and the definition of its getter and setter methods.
- */
-
- private boolean underwater = false;
-
- /**
- * Returns TRUE if viewer underwater
- * @return This Lake's underwater
- * @see #setunderwater
- * @since JDK1.0
- */
- public boolean isUnderwater() {
- return underwater;
- }
-
- /**
- * Sets TRUE if viewer underwater
- * @param <code>underwater</code> the boolean that is to be the Lake's underwater
- * @see #isUnderwater()
- * @since JDK1.0
- */
- public void setUnderwater(boolean underwater) {
- this.underwater = underwater;
- }
-
- public void setUnderwaterValue(String r) {
- setUnderwater(r.toUpperCase().equals("TRUE"));
- }
-
- public void toggleUnderwater() {
- setUnderwater(!isUnderwater());
- }
-
- /**
- * Declaration of the speed attribute (animation speed)
- * and the definition of its getter and setter methods.
- */
-
- private int speed = 50;
-
- /**
- * Returns animation speed
- * @return This Lake's speed
- * @see #setSpeed
- * @since JDK1.0
- */
- public int getSpeed() {
- return speed;
- }
-
- /**
- * Sets animation speed
- * @param <code>speed</code> the int that is to be the Lake's speed
- * @see #getSpeed
- * @since JDK1.0
- */
- public void setSpeed(int speed) {
- if (speed > 100)
- speed = 100;
- else if (speed < 1)
- speed = 1;
- this.speed = speed;
- numFrames = 12 * 50 / speed;
- }
-
- public void setSpeedValue(String speed) {
- setSpeed(Integer.parseInt(speed));
- }
-
-
- public String[][] getParameterInfo() {
- String s[][] = {
- {PARAM_IMAGE, "Image", "JPG or GIF file to reflect"},
- {PARAM_OVERLAY, "Image", "JPG or GIF file to use as an overlay"},
- {PARAM_TARGET, "String", "Target frame"},
- {PARAM_HREF, "URL", "URL to link to"},
- {PARAM_ROCKING, "boolean", "TRUE if boat rocking"},
- {PARAM_UNDERWATER, "boolean", "TRUE if viewer underwater"},
- {PARAM_SPEED, "int", "The animation speed: 1-100"}
- };
- return s;
- }
-
- private void loadParams() {
- String param;
-
- param = getParameter(PARAM_IMAGE);
- if (param != null)
- setImageValue(param);
-
- param = getParameter(PARAM_OVERLAY);
- if (param != null)
- setOverlayValue(param);
-
- param = getParameter(PARAM_TARGET);
-
- param = getParameter(PARAM_HREF);
- if (param != null)
- setHRefValue(param);
-
- param = getParameter(PARAM_ROCKING);
- if (param != null)
- setRockingValue(param);
-
- param = getParameter(PARAM_UNDERWATER);
- if (param != null)
- setUnderwaterValue(param);
-
- param = getParameter(PARAM_SPEED);
- if (param != null)
- setSpeedValue(param);
- }
-
- public boolean mouseUp(Event evt, int i, int j) {
- if (hRef != null) {
- error("" + hRef);
- getAppletContext().showDocument(hRef, target);
- }
- return true;
- }
-
- private int numFrames = 12;
-
- transient private Graphics gMain, gWave;
- transient private Image imgWave;
- transient private int currImage;
- transient private int widthImage, heightImage;
- transient private int widthOverlay, heightOverlay;
- transient private boolean allLoaded = false, isAnimating = true;
- transient private int boatPhase = 0;
- transient private int boatPhaseTotal = 50;
-
- /**
- * Initialisation section.
- */
- public void init() {
- repaint();
- System.out.println(getAppletInfo());
- loadParams();
- allLoaded = true;
- }
-
- /**
- * getPreferredSize() method used for when the class is a component.
- */
- public Dimension getPreferredSize() {
- return new Dimension(widthImage,
- (int)((double)heightImage * 1.8));
- }
-
- /**
- * Display section.
- *
- * The following methods are those used to create and display the
- * final images.
- */
-
- private void error(String msg) {
- getAppletContext().showStatus(msg);
- }
-
- /**
- * Stop a Lake performing unnecessary
- * screen clears by over-riding the update method and
- * calling paint straight away.
- */
- public void update(Graphics g) {
- paint(g);
- }
-
- public void paint(Graphics g) {
- if (imgWave != null) {
- g.drawImage(imgWave, 0, boatHeight, this);
- }
- }
-
- private void displayImage() {
- if (isRocking())
- boatHeight = (int)((double)heightImage *
- Math.sin(2.0 * Math.PI * boatPhase / boatPhaseTotal)
- / 8.0) - (heightImage / 8);
- else
- boatHeight = 0;
-
- if (imgWave != null) {
- if (isUnderwater()) {
- gWave.drawImage(image, 0, (size().height - heightImage), this);
- makeWavesInverse(gWave, currImage);
- boatHeight = -boatHeight;
- }
- else {
- gWave.drawImage(image, 0, 0, this);
- makeWaves(gWave, currImage);
- }
-
- if (overlay != null)
- if (overlayReady())
- gWave.drawImage(overlay,
- ((widthImage - overlay.getWidth(this)) >> 1),
- (heightImage - (overlay.getHeight(this) >> 1))
- + boatHeight,
- this);
- }
- }
-
- private void createAnimation() {
- if ((widthImage > 0) && (heightImage > 0)) {
- synchronized(this) {
- imgWave = createImage(widthImage, 2 * heightImage);
- gWave = imgWave.getGraphics();
- }
- }
- repaint();
- }
-
- private void makeWaves(Graphics g, int phase) {
- double d = 2.0 * Math.PI * phase / (double)numFrames;
- for (int row = 0; row < heightImage; row++) {
- int dispY = (int)((double)(heightImage / 14)
- * ((double)row + 28.0)
- * Math.sin((double)(heightImage / 14 * (heightImage - row))
- / (row + 1) + d)
- / heightImage);
-
- if ((row - heightImage) > dispY)
- g.copyArea(0, (heightImage - row),
- widthImage, 1, 0, row << 1);
- else
- g.copyArea(0, (heightImage - row + dispY),
- widthImage, 1, 0, (row << 1) - dispY);
- }
- }
-
- private void makeWavesInverse(Graphics g, int phase) {
- double d = 2.0 * Math.PI * phase / (double)numFrames;
- int appletHeight = size().height;
-
- for (int row = 0; row < heightImage; row++) {
- int dispY = (int)((double)(heightImage / 14)
- * ((double)row + 28.0)
- * Math.sin((double)(heightImage / 14 * (heightImage - row))
- / (row + 1) + d)
- / heightImage);
-
- if ((row - heightImage) > dispY)
- g.copyArea(0, ((appletHeight - heightImage) + row),
- widthImage, 1, 0, -(row << 1));
- else
- g.copyArea(0, ((appletHeight - heightImage) + row - dispY),
- widthImage, 1, 0, dispY - (row << 1));
- }
- }
- }
-
-