用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字

海大软件1102班    -  云代码空间

—— 你究竟输入什么了导致它崩溃?

Java图形——水纹倒映效果

2012-12-31|3627阅||

摘要:效果图如下: package org.test; import java.awt.Color; import java.awt.Frame; import java.awt.Graphics; import java.awt.Image; import java.awt.Med

效果图如下:



package org.test;

import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Panel;
import java.awt.image.BufferedImage;
import java.awt.image.MemoryImageSource;
import java.awt.image.PixelGrabber;

import org.loon.framework.game.helper.ImageHelper; //http://code.google.com/p/loon-simple/downloads/list

/**
 * <p>
 * Title: LoonFramework
 * </p>
 * <p>
 * Description:Java图形程序水纹倒映效果
 * </p>
 * <p>
 * Copyright: Copyright (c) 2007
 * </p>
 * <p>
 * Company: LoonFramework
 * </p>
 * 
 * @author chenpeng
 * @email:ceponline@yahoo.com.cn
 * @version 0.1
 */
public class ImageWave extends Panel implements Runnable {

	/**
     * 
     */
	private static final long serialVersionUID = 1L;

	private Thread thread;

	private Image _image;

	private MediaTracker _mt;

	private int _delay;

	private Graphics _bg;

	private Image _screen;

	private boolean _playing;

	private int _pixels[];

	private int _mpixels[];

	private int _wpixels[];

	private final static int _WIDTH = 480;

	private final static int _HEIGHT = 604;

	private Image _images[];

	private int _sleep;

	private int _frame;

	/**
	 * 构造函数,注入背景图与延迟时间
	 * 
	 * @param img
	 * @param delay
	 */
	public ImageWave(Image img, int delay) {
		_delay = delay;

		init(img, delay);

	}

	public void init(Image img, int delay) {
        setBackground=\'#\'" />        _image = img;
        _mt = new MediaTracker(this);
        _mt.addImage(_image, 0);
        _mt.checkAll(true);
        _frame = 10;
        _screen = new BufferedImage(_WIDTH, _HEIGHT, 1);
        _bg = _screen.getGraphics();
        thread = new Thread(this);
        thread.start();
    }

	public void run() {
		Thread.currentThread().setPriority(1);
		long l = System.currentTimeMillis();
		while (Thread.currentThread() == thread)
			try {
				repaint();
				_sleep++;
				l += _delay;
				Thread.sleep(Math.max(0L, l - System.currentTimeMillis()));
			} catch (InterruptedException _ex) {
				return;
			}
	}

	/**
	 * 绘制图象
	 */
	public void paint(Graphics g) {

		if (!_playing) {
			if (_mt.checkAll()) {
				_playing = true;
				_bg.drawImage(_image, 0, 0, this);
				_pixels = new int[(_WIDTH * _HEIGHT) / 2];
				_mpixels = new int[(_WIDTH * _HEIGHT) / 2];
				_wpixels = new int[((_WIDTH * _HEIGHT) / 2) * _frame];
				PixelGrabber pixelgrabber = new PixelGrabber(_image, 0, 0,
						_WIDTH, _HEIGHT / 2, _pixels, 0, _WIDTH);
				try {
					pixelgrabber.grabPixels();
				} catch (InterruptedException _ex) {
				}
				blure();
				_images = new Image[_frame];
				for (int i = 0; i < _frame; i++)
					_images[i] = createMirrorImage(i);

			}
		} else {
			if (_sleep > _frame - 1)
				_sleep = 0;
			_bg.drawImage(_images[_sleep], 0, _HEIGHT / 2, this);
		}
		g.drawImage(_screen, 0, 0, this);
	}

	public void update(Graphics g) {
		paint(g);
	}

	/**
	 * 创建波纹反射图片
	 * 
	 * @param i
	 * @return
	 */
	Image createMirrorImage(int i) {
		int k1 = _HEIGHT / 2;
		int j1 = i * _WIDTH * k1;
		for (int k = 0; k < k1; k++) {
			int l = k * _WIDTH;
			int i1 = k1 - k - 1;
			i1 += (int) ((double) ((k * 5) / k1 + 2) * Math
					.sin((25.132741228718345D * (double) i1 * (double) i1)
							/ (double) k1 / (double) k1
							- (6.2831853071795862D * (double) i)
							/ (double) _frame));
			if (i1 < 0)
				i1 = 0;
			if (i1 > k1 - 1)
				i1 = k1 - 1;
			i1 *= _WIDTH;
			for (int j = 0; j < _WIDTH; j++)
				_wpixels[j1 + j + l] = _mpixels[j + i1];

		}

		Image image = createImage(new MemoryImageSource(_WIDTH, k1, _wpixels,
				j1, _WIDTH));
		return image;
	}

	/**
	 * 模糊化反射图片
	 * 
	 */
	void blure() {
		int l = _HEIGHT / 2;
		for (int j = 0; j < l; j++) {
			int k = j * _WIDTH;
			for (int i = 0; i < _WIDTH; i++) {
				int i1 = _pixels[i + k];
				int j1;
				if (i != _WIDTH - 1)
					j1 = _pixels[i + k + 1];
				else
					j1 = _pixels[(i + k) - 1];
				int k1 = 0;
				if (j != l - 1)
					k1 = _pixels[i + k + _WIDTH];
				else
					k1 = _pixels[(i + k) - _WIDTH];
				int l1 = 0;
				if (j != l - 1 && i != _WIDTH - 1)
					l1 = _pixels[i + k + _WIDTH + 1];
				else
					l1 = _pixels[i + k];
				int i2 = (i1 >> 16 & 0xff) + (j1 >> 16 & 0xff)
						+ (k1 >> 16 & 0xff) + (l1 >> 16 & 0xff);
				int j2 = (i1 >> 8 & 0xff) + (j1 >> 8 & 0xff) + (k1 >> 8 & 0xff)
						+ (l1 >> 8 & 0xff);
				int k2 = (i1 & 0xff) + (j1 & 0xff) + (k1 & 0xff) + (l1 & 0xff);
				i2 >>= 3;
				j2 = (j2 >> 3) + (j2 >> 4);
				k2 = (k2 >> 3) + (k2 >> 4);
				_mpixels[i + k] = i1 & 0xff000000 | i2 << 16 | j2 << 8 | k2;
			}

		}

	}

	public static void main(String[] args) {
		Frame frm = new Frame();
		frm.setTitle("Java图形程序水纹倒映效果(由Loonframework提供)");
		frm.setSize(_WIDTH, _HEIGHT + 20);
		frm.setResizable(false);
		frm.add(new ImageWave(ImageHelper.loadImage("C:/back.jpg", true), 100));
		frm.setVisible(true);
	}

}



顶 6踩 2收藏
分享到:
更多
文章评论
    发表评论

    个人资料

    • 昵称: 海大软件1102班
    • 等级: 初级设计师
    • 积分: 2516
    • 代码: 147 个
    • 文章: 70 篇
    • 随想: 11 条
    • 访问: 190 次
    • 关注

    站长推荐