[java]代码库
//1510821001(廖益利)
//练习二 下大雪.txt(已完成100%)
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Panel;
public class pp{
public static void main(String args[]){
Frame w = new Frame();
w.setSize(300,400);
w.setBackground(Color.BLACK);
MyPanel mp= new MyPanel();
w.add(mp);
Thread t=new Thread(mp);
t.start();
w.show();
}
}
class MyPanel extends Panel implements Runnable{
//1 定义2个整形全局变量数组变量,每一个变量有10个成员
int x[]=new int[10];
int y[]=new int[10];
public MyPanel() {
// 2、将数组分别存放10个随机变量值
for (int i = 0; i < 10; i++) {
x[i] = (int) (Math.random() * 300);
y[i] = (int) (Math.random() * 400);
}
}
public void paint(Graphics g) {
//3 使用for循环与drawString函数绘出10个雪花,然后让雪花下落
g.setColor(Color.WHITE);
for (int i = 0; i < 10; i++)
g.drawString("*", x[i], y[i]);
}
public void run() {
while (true) {
try {
for (int i = 0; i < 10; i++) {
y[i]++;
if (y[i] > 400) {
y[i] = 0;
}
}
Thread.sleep(10);
} catch (Exception e) {
}
repaint();
}
}
}
[代码运行效果截图]