Java--下大雪模拟

package firstpack;
import java.awt.*;

public class MyStar {
    public static void main(String[] args) {
        Frame w = new Frame();
        w.setSize(800, 700);
        w.setBackground(Color.black);
        
        MyPanel mp = new MyPanel();
        w.add(mp);
        
        Thread t = new Thread(mp);
        t.start();
        
        w.setVisible(true);
    }

}

class MyPanel extends Panel implements Runnable {
    int x[] = new int[300];
    int y[] = new int[300];
    
    //构造方法
    public MyPanel() {
        for(int i=0;i<300;i++) {
            x[i] = (int)(Math.random()*800);
            y[i] = (int)(Math.random()*700);
        }
    }
    
    public void paint(Graphics g) {
            g.setColor(Color.white);
            for(int i=0;i<300;i++) {
                g.drawString("*", x[i], y[i]);
            }
            
    }
        
    public void run() {
        while(true) {
            try {
                for(int i=0;i<300;i++) {
                    y[i]++;
                    if(y[i]>700) {
                        y[i] = 0;
                    }
                }
                Thread.sleep(10);
            }catch(Exception e) {
                
            }
            repaint();//重画
        }
        
    }
}
原文地址:https://www.cnblogs.com/fredkeke/p/7636178.html