用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

小球撞墙

2017-03-16 作者: 道高一尺举报

[java]代码库

//MY
import java.awt.*;
 
public class pp {
    public static void main(String args[]) {
        Frame w = new Frame();
        w.setSize(300, 400);
        MyPanel mp = new MyPanel();
        w.add(mp);
 
        Thread t = new Thread(mp);
        t.start();
 
        w.show();
    }
}
 
class MyPanel extends Panel implements Runnable {
    int x = 30;
    int y = 30;
    int att = 0;
 
    public void paint(Graphics g) {
      
     g.fillOval(x, y, 20, 30); }
     
 
    public void run() {
        while (true) {
            //定义状态
            if(att==0)
            {
                x++;
                y++;
            }
            if(att==1)
            {
                x--;
                y++;
            }
            if(att==2)
            {
                x--;
                y--;
            }
            if(att==3)
            {
                x++;
                y--;
            }
            //改变状态
            if (x > 263) {
                if (att == 0)
                    att = 1;
                else
                    att = 2;
            }
                if (y > 348) {
                    if (att == 1)
                        att = 2;
                     else
                        att = 3;
                }
                    if (x < 0) {
                        if (att == 2)
                            att = 3;
                        else
                            att = 0;
                    }
                     
                    if (y < 0) {
                        if (att == 3)
                            att = 0;
                        else
                            att = 1;
                }
 
             
            try {
                Thread.sleep(10);
            } catch (Exception e) {
            }
             
            repaint();
        }
        }
    }

[代码运行效果截图]


小球撞墙


网友评论    (发表评论)

共3 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...