求一个Java动画,一个小球在屏幕上碰到边缘就反弹无限循环
1个回答
展开全部
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Panel;
public class Demo {
public static void main(String[] args) {
Frame w = new Frame();
w.setSize(300, 400);
CPanel mp = new CPanel();
w.add(mp);
w.setVisible(true);
Thread t = new Thread(mp);
t.start();
}
}
class CPanel extends Panel implements Runnable {
private static final long serialVersionUID = 3474337559197220434L;
int x = 30;
int y = 30;
boolean down = true;
public void paint(Graphics g) {
g.fillOval(x, y, 20, 20);
}
public void run() {
try {
while (true) {
if (down) {
y++;
if (y >= 340) {
down = false;
}
} else {
y--;
if (y <= 20) {
down = true;
}
}
repaint();
Thread.sleep(1);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |