long long ago

long long ago
本来在看nio,做了个聊天程序,觉得挺好玩,本来准备做完善了,结果公司要整理项目,只好花了一两天做了一个整理jsp的工具。
今天,有朋友要做抽奖程序,我一时兴起,不自量力的要试试看。后来一想自己做的程序向来是以丑陋闻名,朋友那边又是急用,还是觉得没自信啦。。。呵呵。
不过看了一下动画处理,关于去闪烁的机制,做了一个基类来用,屏蔽了线程/双缓冲和update这些技术:
效果: http://www.blogjava.net/Files/black_zerg/picker.rar
代码:


package com.shen.picker.common;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JPanel;

public abstract class AnimationPanel extends JPanel {
    Dimension offDimension;
    Image offImage;
    Graphics offGraphics;
    
int delay = 1000;
    Thread th;
    
boolean needRun;
    
/** *//**
     * 开始动画
     * 
@param delay
     *            延迟
     
*/

    
public void start(int delay) {
        setDelay(delay);
        start();
    }

    
public void start() {
        
if (th == null)
            th 
= new Timer();
        needRun 
= true;
        th.start();
    }

    
public void stop() {
        needRun 
= false;
    }

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

    
public void update(Graphics g) {
        Dimension d 
= size();
        
if ((offGraphics == null|| (d.width != offDimension.width)
                
|| (d.height != offDimension.height)) {
            offDimension 
= d;
            offImage 
= createImage(d.width, d.height);
            offGraphics 
= offImage.getGraphics();
        }

        offGraphics.setColor(getBackground());
        offGraphics.fillRect(
00, d.width, d.height);
        offGraphics.setColor(Color.black);
        paintFrame(offGraphics);
        g.drawImage(offImage, 
00null);
    }

    
public abstract void paintFrame(Graphics g);
    
public abstract void inThread();
    
public void setDelay(int delay) {
        
this.delay = delay;
    }


    
class Timer extends Thread {
        
public void run() {
            
while (needRun) {
                inThread();
                
try {
                    Thread.sleep(delay);
                }
 catch (InterruptedException e) {
                }

                repaint();
            }

        }

    }

}




你可能感兴趣的:(long long ago)