Water surface reflection source

zhaozj2021-02-11  227

************************************************** *******************************************

// Lake.java: applet

//

// (c) David Griffiths, 1997

// this Source Code May Not Be Reproduced without The Express Permission of The Express Permission of The

// Author.

// ******************************************************** *****************************

Import java.applet. *;

Import java.net.URL;

Import java.net.malformedurlexception;

Import java.awt. *;

/ / =========================================================================================================================================================================================== ===============================

// main class for applet lake

//

/ / =========================================================================================================================================================================================== ===============================

Public Class Lake Extends Applet Implements Runnable

{

// Thread Support:

// m_lake is the thread Object for the applet

/ / -------------------------------------------------------------------------------------------- ----------------------------

Thread m_lake = null;

// Animation Support:

// m_graphics buy for storing the applet's graphics context

// m_wavegraphics buy for storing the animation's graphics context

// m_image the Original Image

// m_waveimage the Image Containing the Wave Animations

// m_ncurrimage the index of the next image to be displayed // m_imgwidth Width of Each Image

// m_imgheight heihant of each image

// m_ovlwidth Width of Each Overlay

// m_ovlheight heihant of each overlay

// m_fallloaded INDICES WHETHER All IMAGES HAVE BEEN LOADED

// m_tanimate indeicates That Ok to do animation (Changed by mouse

// click

// Num_frames Number of Num_Frames Used in The Animation

/ / -------------------------------------------------------------------------------------------- ----------------------------

Private graphics m_graphics, m_wavegraphics;

Private image m_image, m_overlay, m_waveImage;

Private int m_ncurrimage;

Private int m_nimgwidth = 0;

Private int m_nimgheight = 0;

Private int m_novlwidth = 0;

Private int m_novlheight = 0;

Private boolean m_fallloaded = false, m_tanimate = true;

PRIVATE FINAL INT NUM_FRAMES = 12;

// Parameter Support:

// Parameters allow an html author to pass information to the applet;

// the html author specifies itm using the Tag within the

// tag. The following variables are buy to store the value of the

// Parameters.

/ / -------------------------------------------------------------------------------------------- ----------------------------

//Members for applet parameters

// =

/ / -------------------------------------------------------------------------------------------- ----------------------------

PRIVATE STRING M_IMAGENAME = "";

Private string m_overlayname = "";

PRIVATE URL M_HREF;

Private string m_frame = "_self";

// Parameter names. To change a name of a parameter, you need onLy Make

// a Single Change. Simply Modify The value of the parameter string below.

/ / -------------------------------------------------------------------------------------------- -------------------------- Private Final String param_image = "image";

Private final string param_overlay = "overlay";

PRIVATE FINAL STRING param_href = "href";

Private final string param_target = "Target";

// Lake Class Constructionor

/ / -------------------------------------------------------------------------------------------- ----------------------------

Public Lake ()

{

// Todo: Add Construction Code Here

}

// Applet Info Support:

// the getappletinfo () Method Returns a string describing the applet?

// Author, Copyright Date, or miscellaneous information.

/ / -------------------------------------------------------------------------------------------- ----------------------------

Public string getAppletInfo ()

{

Return "Name: Lake v3.0 / r / n"

"Author: David Griffiths / R / N"

"CREATED with Microsoft Visual J Version 1.0";

}

// Parameter Support

// the getParameterInfo () Method Returns an Array of strings describing

// The parameters understandtood by this applet.

//

// Lake Parameter Information:

// {"Name", "Type", "description"},

/ / -------------------------------------------------------------------------------------------- ----------------------------

Public String [] [] getParameterInfo ()

{

String [] [] info =

{

{Param_image, "string", "jpg or gif file to reflect"}}

{Param_overlay, "string", "jpg or gif file to use as overlay"},

{Param_href, "url", "url to link to"},

{Param_target, "string", "target frame"},

}

Return INFO;

}

// the init () Method Is Called by the AWT WHEN An Applet Is First Loaded OR

// Reloaded. Override this method to Perform Whatver Initialization Your

// Applet Needs, Such As Initializing Data Structures, Loading images OR / / FONTS, CREANG FRAME Windows, Setting The Layout Manager, or Adding UI

// Components.

/ / -------------------------------------------------------------------------------------------- ----------------------------

Public void init ()

{

// Parameter Support

// The Following Code Retrieves The Value of Each Parameter

// Specified with the Tag and Stores IT IN A MEMBER

// Variable.

/ / -------------------------------------------------------------------------------------------- ----------------------

String param;

// Image: JPG of Gif File to Reflect

/ / -------------------------------------------------------------------------------------------- ----------------------

Param = getParameter (param_image);

IF (param! = null)

m_imagename = param;

// Overlay: JPG of Gif File To Use As overlay

/ / -------------------------------------------------------------------------------------------- ----------------------

Param = getParameter (param_overlay);

IF (param! = null)

m_overlayname = param;

// href: url to link to

/ / -------------------------------------------------------------------------------------------- ----------------------

Param = getParameter (param_href);

IF (param! = null)

Try

{

m_href = new url (getDocumentbase (), param);

}

Catch (Malformedurlexception E)

{

GetAppletContext (). Showstatus ("BAD URL:" Param;

Return;

}

// Target: Target Frame

/ / -------------------------------------------------------------------------------------------- ----------------------

Param = getParameter (param_target);

IF (param! = null)

m_frame = param;

}

// Place Additional Applet Clean Up Code Here. Destroy () IS Called When

// when you applet is terminating and being unloaded.

/ / -------------------------------------------------------------------------------------------- -------------------------

Public void destroy ()

{

// Todo: Place Applet Cleanup Code Here

}

// animation support: // Draws the next image, if all images are currently loading

/ / -------------------------------------------------------------------------------------------- ----------------------------

Private Void DisplayImage (Graphics G)

{

IF (! m_fallloaded)

Return;

// Draw Frame of Rippled Lower Half

/ / -------------------------------------------------------------------------------------------- ----------------------

IF (m_waveimage! = null) {

g.drawImage (M_WaveImage, (-M_NCurrimage * m_nimgwidth), m_nimgheight, this);

g.drawImage (M_WaveImage, ((Num_frames-m_ncurrimage) * m_nimgwidth,

M_nImgheight, this);

}

// Draw the Original in The Tophalf.

/ / -------------------------------------------------------------------------------------------- ----------------------

g.drawImage (m_image, 0, -1, this);

}

// Lake Paint Handler

/ / -------------------------------------------------------------------------------------------- ----------------------------

Public void Paint (Graphics G)

{

// Animation Support:

// the folowing code displays a status message unsplays

// Images is loaded. The IT Calls DisplayImage to Display THE CURRENT

// Image.

/ / -------------------------------------------------------------------------------------------- ----------------------

IF (M_FallLoaded)

DisplayImage (g);

Else

g.drawstring ("Loading Images ...", 10, 20);

// Todo: Place Additional Applet Paint Code Here

}

// the start () Method Is Called when Page Containing the applet

// First Appears on the screen. The appletwizard's initial usteation

// of this method Starts Execution of the applet's thread.

/ / -------------------------------------------------------------------------------------------- ----------------------------

Public void start ()

{

IF (m_lake == null)

{

M_Lake = New Thread (this);

m_lake.start ();

}

}

// the stop () Method Is Called when Page ConTAING THE APPLET IS

// no longer on the screen. The appletwizard's initial us AppleTwizard's INITIAL IMPLEMENTATION OF THIS METHOD Stops Execution of the applet's thread.

/ / -------------------------------------------------------------------------------------------- ----------------------------

Public void stop ()

{

IF (M_Lake! = NULL)

{

m_lake.stop ();

m_lake = null;

}

}

// Thread Support

// the run () Method is Called when Applet's Thread is Started. IF

// Your Applet Performs Any ONGOING ACTIVITIES WITHOUT WAITI for User

// INPUT, The CODE for Implementing That Behavior Typically Goes Here. for

// EXAMPLE, For An Applet That Performs Animation, THE Run () Method Controls

// The Display of Images.

/ / -------------------------------------------------------------------------------------------- ----------------------------

Public void Run ()

{

m_ncurrimage = 0;

// if Re-entering the page, Then The images has arrive.

// m_fallloaded == True.

/ / -------------------------------------------------------------------------------------------- ----------------------

IF (! m_fallloaded)

{

Repaint ();

M_Graphics = getGraphics ();

// now load up the image to be used in the animation. Rather Than DO

// this asynchronously with imageupdate (Which is a pain in the bum to

// USE) We'll Do It Synchronously with a mediatracker. this hangs

// around until the image is loaded. Using the waitforlm Method, Just

// In Case We Ever Add Other Images to the applet.

/ / -------------------------------------------------------------------------------------------- ------------------

Mediatracker Tracker = New Mediatracker (this);

String strimage;

m_image = getImage (getDocumentbase (), m_imagename);

IF (! "". Equals (m_overlayname))

m_overlay = getImage (getDocumentbase (), m_overlayname;

Tracker.addImage (m_image, 0);

IF (! "". Equals (m_overlayname))

Tracker.addImage (m_overlay, 1); // Wait Until All Images Are Fully Loaded

/ / -------------------------------------------------------------------------------------------- ------------------

Try

{

Tracker.waitForl ();

m_fallloaded =! tracker.iserroorany ();

}

Catch (InterruptedExcect E) {}

IF (! m_fallloaded)

{

STOP ();

M_Graphics.drawstring ("Error Loading Images!", 10, 40);

Return;

}

// can now set width and height straight away because the

// Mediatracker Object Has Ensured This Information is now available.

/ / -------------------------------------------------------------------------------------------- ----------------

m_nimgwidth = m_image.Getwidth (this);

M_nImgheight = m_image.getheight (this);

IF (! "". Equals (m_overlayname)) {

m_novlwidth = m_overlay.getwidth (this);

M_novlheight = m_overlay.getheight (this);

}

// Now create the animation of the rippling Waves.

/ / -------------------------------------------------------------------------------------------- ----------------

CreateAnimation ();

}

Repaint ();

While (True)

{

Try

// Draw next image in animation

/ / -------------------------------------------------------------------------------------------- ----------------

IF (m_tanimate)

{

DisplayImage (M_Graphics);

IF ( m_ncurrimage == num_frames)

m_ncurrimage = 0;

Thread.sleep (50);

}

Else

Thread.sleep (500);

Catch (InterruptedException E)

STOP ();

}

}

// Mouse Support:

// Clicking on the applet starts / stops it.

// doesn't call 'stop ()' Directly Because OtherWise An InterruptedException

// is thrown ..

/ / -------------------------------------------------------------------------------------------- ----------------------------

Public Boolean MouseUp (Event Event, Int i, Int J)

{

Boolean flag = Super.MouseUp (Event, I, J);

IF (m_href == null)

M_tanimate =! m_tanimate; // Toggle M_Tanimate To Start / Stop Animation.

Else

{

Showstatus (" m_href); getAppletContext (). showdocument (m_href, m_frame);

}

Return True;

}

// animation

// Create The Animation Withnin A Single Background Image. We use a single

// Image Rather Than The Default Multiple Images Because It's Quicker.

/ / -------------------------------------------------------------------------------------------- ---------------------------

Public void createanimation ()

{

// CREATE INVERTED Image of Original Loaded Image.

// We create a background image image (backimg) 1 Pixel Higher

// Than The Original Because We'll Need An Extra Line of

// Pixels to Play with WHEN We flip the Image Upside Down.

/ / -------------------------------------------------------------------------------------------- ------------

Image backimg = CreateImage (m_nimgwidth, m_nimgheight 1);

Graphics backg = backimg.getgraphics ();

// Copy THE Original Image (M_Image) Onto the Background

// Version.

/ / -------------------------------------------------------------------------------------------- ------------

Backg.drawImage (M_Image, 0, 1, this);

// Now flip the image upside down.

/ / -------------------------------------------------------------------------------------------- ------------

For (int i = 0; i <(m_nimgheight >> 1); i )

{

Backg.copyarea (0, I, m_nimgwidth, 1, 0, m_nimgheight - i);

Backg.copyarea (0, M_NIMGHEIGHT - 1 - I, M_NIMGWIDTH, 1,

0, -M_nImgheight 1 (i << 1));

Backg.copyarea (0, M_NIMGHEIGHT, M_NIMGWIDTH, 1, 0, -1 - i);

}

// now create the large (num_frames 1 Times the width) Image Image

// That Will Store Dithere Copies of the inverted Original.

/ / -------------------------------------------------------------------------------------------- ------------

M_WaveImage = CreateImage (Num_Frames 1) * m_nimgwidth, m_nimgheight);

M_WaveGraphics = m_waveimage.getgraphics ();

M_WaveGraphics.drawImage (Backimg, Num_Frames * m_nimgwidth, 0, this); // Now crete Dithere Copies (Sine Displacement Up or Down) of the

// inverted Original.

/ / -------------------------------------------------------------------------------------------- ------------

For (int phase = 0; Phase

Makewaves (M_WaveGraphics, Phase);

// now, if there is an offlay image, draw the top half of it

// over the frame (The Bottom Half of The overlay Will Be Drawn Over

// the rippled image

/ / -------------------------------------------------------------------------------------------- ----------------

Backg.drawImage (M_Image, 0, 1, this);

IF (! "". Equals (m_overlayname))

Backg.drawImage (M_overlay,

(m_nimgwidth - m_novlwidth >> 1,

M_NIMGHEIGHT - (M_novlheight >> 1), this);

m_image = backimg;

}

// animation

// Take the initial (unwaved) Image from the left-hand-side of the graphics

// and make num_frames copies of it - the Pixels Rows of Each One Dithered

// Up or Down Depending Upon The DiSPY SINE FUNCTION.

/ / -------------------------------------------------------------------------------------------- ---------------------------

Public Void Makewaves (Graphics G, INT PHASE)

{

Double P1;

Int Dispx, DISPY;

// Convert the Phase Into Radians (by splitting 2 * pi inTo

// Num_frames segments.

/ / -------------------------------------------------------------------------------------------- ------------

P1 = 2 * Math.pi * (double) Phase / (double) Num_frames;

// Dispx defines how far across the Image Has to be copied

// from the Original LHS Frame.

/ / -------------------------------------------------------------------------------------------- ------------

Dispx = (Num_frames - Phase) * m_nimgwidth;

// Now Process Each Horizontal Line of Pixels. Copy Across

// from Original Frame on The Left-Had-Side and Displacing

// Up or Down WRT The DISPY SINE FUNCTION.

/ / -------------------------------------------------------------------------------------------- -------- for (int i = 0; i

{

// DISPY Defines The Vertical Sine Displacement. IT

// attenuates higher up the image, for personpect.

/ / -------------------------------------------------------------------------------------------- ------------

DISPY = (INT) (m_nimgheight / 14) * (Double) i 28.0)

* Math.sin ((double) (m_nimgheight / 14) * (m_nimgheight - i)))

/ (double) (i 1)

P1)

/ (double) m_nimgheight;

// if no line dithers here the copy Original.

/ / -------------------------------------------------------------------------------------------- ------------

IF (i <-dispy)

g.copyarea (num_frames * m_nimgwidth, i, m_nimgwidth, 1,

-Dispx, 0);

Else

// Else Copy Dithere Line.

/ / -------------------------------------------------------------------------------------------- ------------

g.copyarea (Num_Frames * m_nimgwidth, i DISPY,

M_nimgwidth, 1, -dispx, -dispy;

}

// now, if there is an offlay image, draw the bottom Half of IT

// over the frame (The top Half of the overlay Will Be Drawn over

// the Original Image

/ / -------------------------------------------------------------------------------------------- ----------------

IF (! "". Equals (m_overlayname))

g.drawimage (m_overlay,

(Phase * m_nimgwidth) (m_nimgwidth - m_novlwidth >> 1),

-m_novlheight >> 1, this);

}

}

转载请注明原文地址:https://www.9cbs.com/read-3980.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.033, SQL: 9