เพื่อเป็นการไม่ให้เสียเวลา ฉันจะโพสต์โค้ด Java ให้คุณ
นำเข้า java.awt.Color; นำเข้า java.awt.Graphics; นำเข้า java.awt.image.BufferedImage; นำเข้า javax.swing.JPanel; (~)จำนวนดาวตก int SLEEP สุดท้าย = ; // ความเร็วของดาวตกที่บิน (ยิ่งค่ามาก ความเร็วก็จะยิ่งช้าลง) สุดท้าย int COLORLV = ; (~) ระดับสี (สามารถเปลี่ยนขนาดรัศมีได้) สุดท้าย String COLOR = null; // ("#"~"#ffffff") สี Halo (หากไม่ได้เติมหรือเป็นโมฆะจะเป็นสีเริ่มต้น) int SIZE สุดท้าย = ; // (~) แผง MyPanel ส่วนตัวขนาดดาวตก () { แผง = ใหม่ MyPanel(); this.getContentPane().add(panel); สร้างแบบฟอร์ม this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public static void main(String[] args) { new MeteorFly(); } คลาส MyPanel ขยายการใช้งาน Runnable { Meteor p[]; AppletWidth, AppletHeight; กราฟิกบัฟเฟอร์ภาพ DrawOffScreen; Thread pThread; public MyPanel() { setBackground(Color.black); // การเริ่มต้นฟอร์ม AppletWidth = ; p = new Meteor[MAX]; ] = Meteor ใหม่ (); OffScreen = BufferedImage ใหม่ (AppletWidth, AppletHeight, BufferedImage.TYPE_INT_BGR); DrawOffScreen = OffScreen.getGraphics(); pThread = new Thread(this); pThread.start(); } @ แทนที่ public void paintComponent (Graphics g) { // TODO วิธีการสร้างอัตโนมัติ stub super.paintComponents ( g); g.drawImage (OffScreen, , , นี้); } @ แทนที่โมฆะสาธารณะขั้นสุดท้าย run() { while (true) { // DrawOffScreen.clearRect(, , AppletWidth, AppletHeight); // // ล้างหน้าจอสำหรับ (int i = ; i < MAX; i++) { DrawOffScreen.setColor(p[i] . สี); // สี RGB DrawOffScreen.fillOval(p[i].x, p[i].y, ขนาด, ขนาด); 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].getRed( ); // แยกสี int G = p[i].color.getGreen(); int B = p[i].color.getBlue(); ในขณะที่ (จริง) { if (R == && G == && B == ) { แตก; } R -= COLORLV; // จางสีหางถ้า (R < ) { R = ; } G -= COLORLV; { B = ; } สี = สีใหม่ (R, G, B); x -= p[i].mx; // ครอบคลุมส่วนท้าย y -= p[i].my; DrawOffScreen .fillOval(x, y, SIZE, SIZE } ถ้า (x > AppletWidth || y > AppletHeight) { // ดาวตกบินออกไปนอกหน้าต่าง รีเซ็ตดาวตก p[i].reset(); } } ทาสีใหม่ (); ลอง { Thread.sleep(SLEEP); } catch (InterruptedException e) { // TODO สร้างขึ้นโดยอัตโนมัติ e.printStackTrace (); } } } } คลาส Meteor { // คลาส Meteor int x, y; // ตำแหน่งดาวตก int mx, my; color; // Meteor color สาธารณะ Meteor() { รีเซ็ต (); } รีเซ็ตโมฆะสาธารณะ () { int rand = (int) (Math.random() * ); // สุ่มสร้างตำแหน่งดาวตกถ้า (rand > ) { x = (int) (Math.random() * ); y = ; (Math.random() * + ); //สุ่มสร้างความเร็วตกและมุม my = (int) (Math.random() * + ); if (COLOR == null || COLOR.length() == ) { color = new Color( // Random color (new Double(Math.random() * )).intValue() + , (new Double(Math.random() * )).intValue() + , (ใหม่ สองเท่า(Math.random() * )).intValue() + } else { color = Color.decode(COLOR);โค้ดด้านบนเป็นโค้ด Java ล้วนๆ ที่บทความนี้บอกให้คุณใช้อุกกาบาตข้ามท้องฟ้า ฉันหวังว่าการแบ่งปันบทความนี้จะทำให้คุณได้รับผลประโยชน์ที่ไม่คาดคิด