站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Java 专题水中倒影

水中倒影

摘要: 水中倒影

</td> </tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width="732" border="0"> <tr> <td> </td> </tr> </table>

import java.applet.*;

import java.net.URL;

import java.net.MalformedURLException;

import java.awt.*;

public class Lake extends Applet implements Runnable

{

Thread m_Lake = null;

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;

private String m_ImageName = "";

private String m_OverlayName = "";

private URL m_HRef;

private String m_Frame = "_self";

private final String PARAM_image = "image";

private final String PARAM_overlay = "overlay";

private final String PARAM_href = "href";

private final String PARAM_target = "target";

public Lake()

{

// TODO: Add constructor code here

}

public String getAppletInfo()

{

return "Name: Lake v3.0\r\n" +

       "Author: David Griffiths\r\n" +

       "Created with Microsoft Visual J++ Version 1.0";

}

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;

}

public void init()

{

String param;

                param = getParameter("image");

if (param != null)

m_ImageName = param;

param = getParameter(PARAM_overlay);

if (param != null)

m_OverlayName = param;

param = getParameter(PARAM_href);

if (param != null)

                try

                {

                    m_HRef = new URL(getDocumentBase(), param);

                }

                catch (MalformedURLException e)

                {

                    getAppletContext().showStatus("Bad URL: " + param);

                    return;

                }

param = getParameter(PARAM_target);

if (param != null)

m_Frame = param;

}

public void destroy()

{

// TODO: Place applet cleanup code here

}

private void displayImage(Graphics g)

{

if (!m_fAllLoaded)

return;

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);

}

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

}

public void paint(Graphics g)

{

if (m_fAllLoaded)

displayImage(g);

else

g.drawString("Loading images...", 10, 20);

// TODO: Place additional applet Paint code here

}

public void start()

{

if (m_Lake == null)

{

m_Lake = new Thread(this);

m_Lake.start();

}

}

public void stop()

{

if (m_Lake != null)

{

m_Lake.stop();

m_Lake = null;

}

}

public void run()

{

m_nCurrImage = 0;

        if (!m_fAllLoaded)

{

    repaint();

    m_Graphics = getGraphics();

    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);

try

{

tracker.waitForAll();

m_fAllLoaded = !tracker.isErrorAny();

}

catch (InterruptedException e) {}

if (!m_fAllLoaded)

{

    stop();

    m_Graphics.drawString("Error loading images!", 10, 40);

    return;

}

    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);

}

createAnimation();

        }

repaint();

while (true)

{

                        try{

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();

                          }

}

}

    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;

    }

    public void createAnimation ()

{

Image backImg = createImage (m_nImgWidth, m_nImgHeight + 1);

        Graphics backG = backImg.getGraphics();

        backG.drawImage (m_Image, 0, 1, this);

        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);

        }

        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);

        for (int phase = 0; phase < NUM_FRAMES; phase++)

           makeWaves (m_WaveGraphics, phase);

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;

}

public void makeWaves (Graphics g, int phase)

{

double p1;

int     dispx, dispy;

p1 = 2 * Math.PI * (double)phase / (double)NUM_FRAMES;

dispx = (NUM_FRAMES - phase) * m_nImgWidth;

for (int i = 0; i < m_nImgHeight; i++)

{

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 (i < -dispy)

g.copyArea (NUM_FRAMES * m_nImgWidth, i, m_nImgWidth, 1,

-dispx, 0);

else

g.copyArea (NUM_FRAMES * m_nImgWidth, i + dispy,

m_nImgWidth, 1, -dispx, -dispy);

}

if (!"".equals(m_OverlayName))

g.drawImage (m_Overlay,

(phase * m_nImgWidth) + ((m_nImgWidth - m_nOvlWidth) >> 1),

-m_nOvlHeight >> 1, this);

}

}

</td> </tr> <tr>


↑返回目录
前一篇: 雪花漂漂
后一篇: 翻转图像