2022-2-12 周六 雪花飘落练习


1.循环变量的初始化;

    int i = 0;

2.循环的条件;

    i < 5;

3.循环变量的改变;

    i++

package cn.tedu;

import java.awt.*;
import java.util.Random;
import javax.swing.ImageIcon;
import cn.tedu.jf.SnowPanelSec;

public class SnowPanel extends SnowPanelSec {

//定义雪花的数量
int snow = 30;
//定义雪花的坐标(x,y)
int[] x = new int[snow];
int[] y = new int[snow];

//初始化(给坐标赋值)
public  void  init() {

    int nextInt = new Random().nextInt(1441);
    //给坐标赋值
    for (int i = 0; i < snow; i++) {
        x[i] = new Random().nextInt(1441);
        y[i] = new Random().nextInt(901);

    }

    //加载图片

// snowImg = new ImageIcon("Image/老虎.png").getImage();

    int num = new Random().nextInt(5);

    switch (num) {
        case 0:
            snowImg = new ImageIcon("Image/老虎.png").getImage();
            break;

        case 1:
            snowImg = new ImageIcon("Image/冰墩墩.png").getImage();
            break;

        case 2:
            snowImg = new ImageIcon("Image/冰雪.png").getImage();
            break;
        case 3:
            snowImg = new ImageIcon("Image/雪融容.png").getImage();
            break;
        case 4:
            snowImg = new ImageIcon("Image/雪花.png").getImage();
            break;

// default:
// System.out.println("输入有误");
}
}
//画雪花
public void paintImage (Graphics g){
for (int i = 0; i < snow; i++)
g.drawImage(snowImg, x[i], y[i], this);
}
//让雪花动起来
public void auto () {
for (int i = 0; i < snow; i++) {
x[i] = x[i] + 1;
y[i] = y[i] + 1;
if (x[i] > 1441) {
x[i] = 0;
}
if (y[i] > 901) {
y[i] = 0;
}
}
}
//主程序,程序的入口
public static void main (String[]args){
new SnowPanel().start();
}