1importjava.awt.Color;2importjava.awt.Graphics;3importjava.awt.image.BufferedImage;45importjavax.swing.JFrame;6importjavax.swing.JPanel;78publicclassMeteorFlyextendsJFrame {910finalintMAX = 5;//(1~1000)流星的个数11finalintSLEEP = 30;//流星飞行的速度(数值越大,速度越慢)12finalintCOLORLV = 2;//(2~20)色阶(可改变光晕大小)13finalString COLOR =null;//("#000000"~"#ffffff")光晕颜色(如果不填或null,则为默认颜色)14finalintSIZE = 3;//(2~50)流星大小1516PRivateMyPanel panel;1718publicMeteorFly() {19panel =newMyPanel();20this.getContentPane().add(panel);2122this.setSize(800, 400);//创建窗体23this.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);24this.setVisible(true);25}2627publicstaticvoidmain(String[] args) {28newMeteorFly();29}3031classMyPanelextendsJPanelimplementsRunnable {3233Meteor p[];3435intAppletWidth, AppletHeight;3637BufferedImage OffScreen;38Graphics drawOffScreen;39Thread pThread;4041publicMyPanel() {42setBackground(Color.black);//窗体初始化43AppletWidth = 800;44AppletHeight = 400;45p =newMeteor[MAX];46for(inti = 0; i < MAX; i++)47p[i] =newMeteor();48OffScreen =newBufferedImage(AppletWidth, AppletHeight,49BufferedImage.TYPE_INT_BGR);50drawOffScreen =OffScreen.getGraphics();51pThread =newThread(this);52pThread.start();53}5455@Override56publicvoidpaintComponent(Graphics g) {57//TODO Auto-generated method stub58super.paintComponents(g);59g.drawImage(OffScreen, 0, 0,this);60}6162@Override63finalpublicvoidrun() {64while(true) {65//drawOffScreen.clearRect(0, 0, AppletWidth, AppletHeight);//66//清屏6768for(inti = 0; i < MAX; i++) {69drawOffScreen.setColor(p[i].color);//RGB颜色70drawOffScreen.fillOval(p[i].x, p[i].y, SIZE, SIZE);71p[i].x +=p[i].mx;72p[i].y +=p[i].my;73//if (p[i].x > AppletWidth || p[i].y > AppletHeight) {74//p[i].reset();75//}7677intx =p[i].x;78inty =p[i].y;79intR = p[i].color.getRed();//提取颜色80intG =p[i].color.getGreen();81intB =p[i].color.getBlue();82while(true) {83if(R == 0 && G == 0 && B == 0) {84
break;85}86R -= COLORLV;//尾部颜色淡化87if(R < 0) {88R = 0;89}90G -=COLORLV;91if(G < 0) {92G = 0;93}94B -=COLORLV;95if(B < 0) {96B = 0;97}98Color color =newColor(R, G, B);99x -= p[i].mx;//覆盖尾部100y -=p[i].my;101drawOffScreen.setColor(color);102drawOffScreen.fillOval(x, y, SIZE, SIZE);103}104if(x > AppletWidth || y > AppletHeight) {//流星飞出窗口,重置流星105p[i].reset();106}107}108repaint();109110try{111Thread.sleep(SLEEP);112}catch(InterruptedException e) {113//TODO Auto-generated catch block114e.printStackTrace();115}116}117}118119}120121classMeteor {//流星类122intx, y;//流星的位置123intmx, my;//下落速度124Color color;//流星颜色125126publicMeteor() {127reset();128}129130publicvoidreset() {131intrand = (int) (Math.random() * 100);//随机生成流星出现位置132if(rand > 35) {133x = (int) (Math.random() * 600);134y = 0;135}else{136y = (int) (Math.random() * 150);137x = 0;138}139mx = (int) (Math.random() * 2 + 1);//随机生成下落速度和角度140my = (int) (Math.random() * 2 + 1);141if(COLOR ==null|| COLOR.length() == 0) {142color =newColor(143//随机颜色144(newDouble(Math.random() * 128)).intValue() + 128,145(newDouble(Math.random() * 128)).intValue() + 128,146(newDouble(Math.random() * 128)).intValue() + 128);147}else{148color =Color.decode(COLOR);149}150}151}152153}
本人原创未经允许请勿转载