廢話不多說了,直接給大家貼java程式了。
import java.awt.Color; import java.awt.Graphics; import java.awt.image.BufferedImage; import javax.swing.JFrame; import javax.swing.JPanel; public class MeteorFly extends JFrame { final int class MeteorFly extends JFrame> (~)流星的個數final int SLEEP = ; //流星飛行的速度(數值越大,速度越慢) final int COLORLV = ; // (~)色階(可改變光暈大小) final String COLOR = null; // ("#"~"#ffffff")光暈顏色(如果不填入或null,則為預設顏色) final int SIZE = ; // (~)流星大小private MyPanel panel; public MeteorFly() { panel = new MyPanel(); this.getContentPane().add(panel); this.setSize(, ); // 建立窗體this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public static void main(String [] args) { new MeteorFly(); } class MyPanel extends JPanel implements Runnable { Meteor p[]; int AppletWidth, AppletHeight; BufferedImage OffScreen; Graphics drawOffScreen; Thread pThread; public MyPanel() { setBackground(Color.black); //窗體初始化AppletWidth = pHeightal ; MAX]; for (int i = ; i < MAX; i++) p[i] = new Meteor(); OffScreen = new BufferedImage(AppletWidth, AppletHeight, BufferedImage.TYPE_INT_BGR); drawOffScreen = OffScreen.getGraphics(); pThread = new Thread(this); pThread.Fverride(this); void paintComponent(Graphics g) { // TODO Auto-generated method stub super.paintComponents(g); g.drawImage(OffScreen, , , this); } @Override final public void run() { while (true) { // drawOffScreen.clearRect(, , Appletightth, , AppleHet. ; // // 清屏for (int i = ; i < MAX; i++) { drawOffScreen.setColor(p[i].color); // RGB顏色drawOffScreen.fillOval(p[i].x, p[i].y, SIZE, SIZE); p[i].x += p[i ].mx; p[i].y += p[i].my; // if (p[i].x > AppletWidth || p[i].y > AppletHeight) { // p[i].reset(); // } int x = p[i].x; int y = p[i].y; int R = p[i].color.getRed() ; // 擷取顏色int G = p[i].color.getGreen(); int B = p[i].color.getBlue(); while (true) { if (R == && G == && B = = ) { break; } R -= COLORLV; // 尾部顏色淡化if (R < ) { R = ; } G -= COLORLV; if (G < ) { G = ; } B -= COLORLV; if (B < ) { B = ; } Color color = new Color(R, G, B); x -= p[i].mx; // 覆蓋尾部y -= p[i].my; drawOffScreen.setColor(color); drawOffScreen.fillOval(x, y, SIZE, SIZE); } if (x > AppletWidth || y > AppletHeight) { // 流星飛出窗口,重置流星p[i].resetp[i].resetp[i]。 ); } } repaint(); try { Thread.sleep(SLEEP); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } class Meteor { // 流星類int x, y; // 流星的位置int mx, my; // 下落速度Color color; // 流星顏色public Meteor() { reset(); } public void reset() { int rand = (int) (Math.random() * ); //隨機產生流星出現位置if (rand > ) { x = (int) (Math.random() * ); y = ; } else { y = (int) (Math.random() * ); x = ; } mx = (int) (Math .random() * + ); //隨機產生下落速度與角度my = (int) (Math.random() * + ); if (COLOR == null || COLOR.length() == ) { color = new Color( // 隨機顏色(new Double(Math.random() * )).intValue() + , (new Double(Math.random() * )).intValue () + , (new Double(Math.random() * )).intValue() + ); } else { color = Color.decode(COLOR); } } } }以上程式碼就是本文要告訴大家的純Java程式碼實現流星劃過天空,希望本文分享能為大家帶來意想不到的收穫。