刻意编程D4

2017-09-14  本文已影响0人  周偉誠

练习内容

感想

附件

import java.awt.*;
public class MyBall{
    public static void main(String[] args) {
        Frame w = new Frame();
        w.setSize(300,400);
        
        MyPanel mp = new MyPanel();
        
        Thread t = new Thread(mp);
        w.add(mp);
        t.start();
        
        w.show();
    }
}

class MyPanel extends Panel implements Runnable{
    int x = 30;
    int y = 30;
    public void paint(Graphics g) {
        g.fillOval(x,y,50,100);
    } //要补全paint()方法的括号
    public void run() {
        while(true) {
            y++;
            if (y>400) {
                y = 0;
            }
            try {
                Thread.sleep(30);
            }catch(Exception e) {}  //Exception拼写
            repaint();
        }
    }
    }
上一篇 下一篇

猜你喜欢

热点阅读