多彩烟花

来源:互联网 发布:php技术文档模板 编辑:程序博客网 时间:2024/05/16 01:54
/** * 多彩烟花绘制 * @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();    }}