分享

撞墙的小球

 古方眼宝_燕子 2017-08-15

 package five;

import java.awt.Graphics;
import java.awt.Panel;

import javax.swing.JFrame;

public class zhuangqiang {

public static void main(String[] args){
JFrame w=new JFrame();
w.setDefaultCloseOperation(w.EXIT_ON_CLOSE);
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, 30, 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>253){
if(att==0){
att=1;
}else{
att=2;
}
}
if(y>325){
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(30);
}catch(Exception e){}
repaint();
}
}
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多