请帮我看看这个java程序为什么没有动画效果?
importjava.awt.*;importjavax.swing.*;publicclassAnimation{intx=70;inty=70;publicstati...
import java.awt.*;
import javax.swing.*;
public class Animation {
int x=70;
int y=70;
public static void main(String[] args) {
// TODO Auto-generated method stub
Animation animation=new Animation();
animation.go();
}
public void go() {
// TODO Auto-generated method stub
JFrame frame=new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyDraw draw=new MyDraw();
frame.getContentPane().add(draw);
frame.setSize(300, 300);
frame.setVisible(true);
for (int i = 0; i < 130; i++) {
x++;
y++;
draw.repaint();
try {
Thread.sleep(50);
} catch (Exception e) {
// TODO: handle exception
}
}
}
class MyDraw extends JPanel{
@Override
public void paintComponents(Graphics g) {
// TODO Auto-generated method stub
g.setColor(Color.white);
g.fillOval(0, 0, this.getWidth(), this.getHeight());
g.setColor(Color.black);
g.fillOval(x, y, 40, 40);
}
}
} 展开
import javax.swing.*;
public class Animation {
int x=70;
int y=70;
public static void main(String[] args) {
// TODO Auto-generated method stub
Animation animation=new Animation();
animation.go();
}
public void go() {
// TODO Auto-generated method stub
JFrame frame=new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyDraw draw=new MyDraw();
frame.getContentPane().add(draw);
frame.setSize(300, 300);
frame.setVisible(true);
for (int i = 0; i < 130; i++) {
x++;
y++;
draw.repaint();
try {
Thread.sleep(50);
} catch (Exception e) {
// TODO: handle exception
}
}
}
class MyDraw extends JPanel{
@Override
public void paintComponents(Graphics g) {
// TODO Auto-generated method stub
g.setColor(Color.white);
g.fillOval(0, 0, this.getWidth(), this.getHeight());
g.setColor(Color.black);
g.fillOval(x, y, 40, 40);
}
}
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询