多彩烟花

2 篇文章 0 订阅
/**
 * 多彩烟花绘制
 * @author Administrator
 *
 */
public class FireWorks extends JFrame{
    private static final long serialVersionUID = 1L;
    public FireWorks(){
        super("多彩烟花");
        this.setSize(800, 600);
        JPanel pane=new JPanel();
        pane.setBackground(new Color(0x0));
        this.setContentPane(pane);
        this.setVisible(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        start();
    }
    public void start(){
        final ExecutorService threadPool=Executors.newFixedThreadPool(8);
        this.addMouseListener(
                new MouseAdapter(){
                    public void mouseClicked(final MouseEvent e){
                        threadPool.execute(
                                new Runnable(){
                                    public void run(){
                                        drawFire(e.getX(),e.getY());
                                        drawFireWorks(e.getX(),e.getY());
                                    }
                                }
                                );
                    }
                }
                );
    }
    /**
     * 烟花发射
     */
    public void drawFire(int x,int y){
        Graphics g=this.getGraphics();
        int x_start=x;
        int y_start=550;
        Random color=new Random();
        while(y_start>=y){
            for(int i=0;i<11;i++){
                if(i!=0 && (y_start-i*5)>=y){
                g.setColor(
                        new Color(color.nextInt(256),color.nextInt(256),color.nextInt(256))
                        );
                }else{
                    g.setColor(
                            new Color(0x0)
                            );
                }
                g.fillOval(x_start, y_start-i*5, 5, 5);
            }
            y_start-=5;
            try{
                Thread.sleep(25);
            }catch(Exception e){
            }
        }
    }
    /**
     * 烟花绘制 
     */
    public void drawFireWorks(int x,int y){
        Graphics g=this.getGraphics();
        int x_start=x;
        int y_start=y;
        Random color=new Random();
        for(int i=0;i<20;i++){
            for(int j=0;j<4;j++){
                if(j!=0 && (i+j)<15){
                    g.setColor(
                            new Color(color.nextInt(256),color.nextInt(256),color.nextInt(256))
                            );
                }else{
                    g.setColor(
                            new Color(0x0)
                            );
                }
                g.drawOval(x_start-(i+j+3)*5, y_start-(i+j+3)*5, (i+j+3)*10, (i+j+3)*10);
            }
            try{
                Thread.sleep(60);
            }catch(Exception e){
            }
            if(i>3){
                drawString(x,y,i);
            }
        }
    }
    /**
     *画字符串 
     */
    public void drawString(int x,int y,int time){
        Graphics g=this.getGraphics();
        Random ran=new Random();
        String[] message={"恭喜发财","财源滚滚","心想事成","事业有成"};
        if(time!=19){
            g.setColor(
                    new Color(ran.nextInt(256),ran.nextInt(256),ran.nextInt(256))
                    );
        }else{
            g.setColor(
                    new Color(0x0)
                    );
        }
        g.drawString(message[(int)(x+y)%message.length], x-20, y);
    }
    public static void main(String[] args) {
        new FireWorks();
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值