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

练习.小球下落

时间:2015-01-02 22:19:16      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:

 1 package com.java7.myballfalldown.main;
 2 
 3 import java.awt.Graphics;
 4 
 5 import javax.swing.JFrame;
 6 import javax.swing.JPanel;
 7 
 8 public class MyBallFallDown {
 9     public static void main(String[] args) {
10         JFrame w = new JFrame();
11         w.setSize(300, 400);
12         
13         MyPanel mp = new MyPanel();
14         w.add(mp);
15         
16         Thread t = new Thread(mp);
17         t.start();
18         
19         w.setVisible(true);
20     }
21 }
22 
23 class MyPanel extends JPanel implements Runnable {
24     int x = 30;
25     int y = 30;
26     
27     public void paint(Graphics g) {
28         super.paint(g);
29         g.fillOval(x, y, 20, 20);
30     }
31     public void run() {
32         while(true) {
33             y++;
34             if(y > 400) {
35                 y = 0;
36             }
37             try {
38                 Thread.sleep(20);
39             } catch(Exception e) {
40                 
41             }
42             repaint();
43         }
44     }
45 }

 

练习.小球下落

标签:

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

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