码迷,mamicode.com
首页 > 其他好文 > 详细

练习.下大雪

时间:2015-01-03 00:54:11      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:

 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 }

 

练习.下大雪

标签:

原文地址:http://www.cnblogs.com/fatoland/p/4199037.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!