首页 > 代码库 > 练习.下大雪

练习.下大雪

 1 package com.java7.mysnow.main; 2 import java.awt.*; 3 import javax.swing.JFrame; 4 import javax.swing.JPanel; 5  6 public class MySnow { 7     public static void main(String[] args) { 8         Frame w = new JFrame(); 9         w.setSize(1024, 768);10 11         MyPanel mp = new MyPanel();12         w.add(mp);13 14         Thread t = new Thread(mp);15         t.start();16 17         w.setVisible(true);18     }19 }20 21 class MyPanel extends JPanel implements Runnable {22     int x[] = new int[300];23     int y[] = new int[300];24     int fx[] = new int[300];25     int fy[] = new int[300];26 27     // 构造方法28     public MyPanel() {29         for(int i = 0; i < 300; i++) {30             x[i] = (int)(Math.random() * 1024);31             y[i] = (int)(Math.random() * 768);32             fx[i] = (int)(Math.random() * 30);33             fy[i] = (int)(Math.random() * 30);34         }35     }36     public void paint(Graphics g) {37         super.paint(g);38         this.setBackground(new Color(205,240,253));39         g.setColor(Color.WHITE);40         for(int i = 0; i < 300; i++) {41             g.setFont(new Font("", fx[i], fy[i]));42             g.drawString(".", x[i], y[i]);43         }44     }45     public void run() {46         while(true) {47             try {48                 for(int i = 0; i < 300; i++) {49                     y[i]++;50                     if(y[i] > 768) {51                         y[i] = 0;52                     }53                 }54                 Thread.sleep(25);55             } catch (Exception e) {56                 57             }58             repaint();59         }60     }61 }

 

练习.下大雪