用户注册



邮箱:

密码:

用户登录


邮箱:

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

发表随想


还能输入:200字
云代码 - java代码库

实现窗体中角色逐渐风化效果

2013-01-01 作者: 程序猿style举报

[java]代码库

package org.test;
 
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.PixelGrabber;
import java.util.Random;
 
import org.loon.framework.game.image.Bitmap;
import org.loon.framework.game.image.LColor;
 
/**
 * <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 AirslakeImage extends Panel implements Runnable, ActionListener {
 
    /**
     *
     */
    private static final long serialVersionUID = 1L;
 
    static final public int _WIDTH = 400;
 
    static final public int _HEIGHT = 400;
 
    private boolean _isRun = false;
 
    private Image _img;
 
    private Image _screen;
 
    private Graphics _back;
 
    private int _imgWidth;
    private int _imgHeight;
 
    private Fraction[] _fractions;
 
    private Thread _timer;
 
    private Button _meganteButton;
    private Button _revivalButton;
 
    private Random _rand = new Random();
 
    public AirslakeImage() {
 
        _screen = new Bitmap(_WIDTH, _HEIGHT).getImage();
 
        _back = _screen.getGraphics();
 
        setSize(_WIDTH, _HEIGHT);
 
        _meganteButton = new Button("破碎图片");
        _meganteButton.addActionListener(this);
        add(_meganteButton);
 
        _revivalButton = new Button("还原图片");
        _revivalButton.addActionListener(this);
        add(_revivalButton);
        _revivalButton.setEnabled(false);
 
        loadImage("role.png");
 
        init(_img);
    }
 
    /**
     * 初始化image图像,分解其中像素
     *
     * @param _img
     */
    private void init(Image _img) {
        if (_timer != null) {
            _timer = null;
            _isRun = false;
        }
        _fractions = new Fraction[_imgWidth * _imgHeight];
        PixelGrabber pg = new PixelGrabber(_img, 0, 0, _imgWidth, _imgHeight,
                true);
        try {
            pg.grabPixels();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        int pixel[] = (int[]) pg.getPixels();
 
        // 重新封装像素
        for (int y = 0; y < _imgHeight; y++) {
            for (int x = 0; x < _imgWidth; x++) {
                int n = y * _imgWidth + x;
                _fractions[n] = new Fraction();
                double angle = _rand.nextInt(360);
                double speed = 10.0 / _rand.nextInt(30);
                _fractions[n].x = x + 90;
                _fractions[n].y = y + 20;
                _fractions[n].vx = Math.cos(angle * Math.PI / 180) * speed;
                _fractions[n].vy = Math.sin(angle * Math.PI / 180) * speed;
                _fractions[n].color = pixel[n];
                _fractions[n].countToCrush = x / 6 + _rand.nextInt(10);
            }
        }
    }
 
    public void update(Graphics g) {
        paint(g);
    }
 
    public void paint(Graphics g) {
        // 变更背景色
        _back.setColor(Color.WHITE);
        // 清空背景
        _back.fillRect(0, 0, _WIDTH, _HEIGHT);
 
        for (int n = 0; n < _imgWidth * _imgHeight; n++) {
            int x = (int) _fractions[n].x;
            int y = (int) _fractions[n].y;
            LColor color = LColor.getLColor(_fractions[n].color);
            // 纯黑色区域不读取
            if (!LColor.equals(color, LColor.fromArgb(0, 0, 0))) {
                // 获得rgb三色
                int red = color.R;
                int green = color.G;
                int blue = color.B;
                _back.setColor(new Color(red, green, blue));
                // 绘制
                _back.drawLine(x, y, x, y);
            }
        }
        g.drawImage(_screen, 0, 0, this);
    }
 
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == _meganteButton) {
            execute();
            _meganteButton.setEnabled(false);
            _revivalButton.setEnabled(true);
        } else if (e.getSource() == _revivalButton) {
            init(_img);
            repaint();
            _meganteButton.setEnabled(true);
            _revivalButton.setEnabled(false);
        }
    }
 
    /**
     * 加载图像
     *
     * @param filename
     */
    private void loadImage(String filename) {
        Bitmap bitmap = new Bitmap(("./image/" + filename).intern());
        // 替换透明区域颜色(象素化后,转为rgb形式的透明区域色值将显示为r=0,g=0,b=0),可以直接用pixel识别透明区域,也可以替换或跳过该区域)
        _img = bitmap.getImage();
        _imgWidth = _img.getWidth(this);
        _imgHeight = _img.getHeight(this);
    }
 
    /**
     * 执行操作
     *
     */
    private void execute() {
        _timer = new Thread(this);
        _timer.start();
        _isRun = true;
    }
 
    class Fraction {
        // 图片在窗体中x
        public double x;
        // 图片在窗体中y
        public double y;
        // 显示图x
        public double vx;
        // 显示图y
        public double vy;
        // color
        public int color;
        // 变形颗粒数量
        public int countToCrush;
    }
 
    public void run() {
        while (_isRun) {
            for (int n = 0; n < _imgWidth * _imgHeight; n++) {
                if (_fractions[n].countToCrush <= 0) {
                    _fractions[n].x += _fractions[n].vx;
                    _fractions[n].y += _fractions[n].vy;
                    _fractions[n].vy += 0.1;
                } else {
                    _fractions[n].countToCrush--;
                }
 
            }
            // 间隔
            try {
                Thread.sleep(60);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            repaint();
        }
    }
 
    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                Frame frm = new Frame("java实现图片风化效果");
                frm.add(new AirslakeImage());
                frm.setResizable(false);
                frm.setSize(_WIDTH, _HEIGHT);
                frm.addWindowListener(new WindowAdapter() {
                    public void windowClosing(WindowEvent e) {
                        System.exit(0);
                    }
                });
                frm.setLocationRelativeTo(null);
                frm.setVisible(true);
            }
        });
    }
}

[代码运行效果截图]


实现窗体中角色逐渐风化效果


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...