想实现 小球在窗口内来回反弹 点击按钮会继续增加小球 
我现在的代码,小球总是闪烁,而且还不能反弹
请高手帮我改正下 谢了
package lab8;import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;class PingBall {
private int x; private int y; private int z; private int a, b; private int startX; private int startY; private Color tempColor; private int flag; JPanel panel; Graphics g; PingBall(JPanel panel) {
Random rand = new Random();
this.panel = panel;
g = panel.getGraphics();
// a = rand.nextInt(80);
// b = rand.nextInt(500);
startX = 0;
startY = 0;
a = 0;
b = 0;
flag = 1; // x = rand.nextInt(255);
// y = rand.nextInt(255);
// z = rand.nextInt(255); } public synchronized void draw() {
tempColor = new Color(255, 0, 0); ((Graphics2D) g).setPaint(tempColor);
if( a >0 && b >0 && a < 380  )
//east
if (a > 380 && b < 480) {
a = 380;
flag = 2;
// g.fillOval(a, b, 10, 10);
} else
//south
if (a < 380 && b > 480) {
b = 480;
flag = 3;
// g.fillOval(a, b, 10, 10); } else 
if (a > 10 && b < 480) {
a = 10;
flag = 4;
// g.fillOval(a, b, 10, 10);
}



 g.fillOval(a, b, 10, 10);
 System.out.println("a"+a);
 System.out.println("b"+b);

} // do // System.out.println(a + " " + b);
public synchronized void move() { if (flag == 1) {
a ++;
b ++;
//draw();
} else
//east
if (flag == 2) {
a--;
b++;
// draw();
} else
//south
if (flag == 3) {
a--;
b--;
// draw();
} else
//west
if (flag == 4) {
a++;
b--;
// draw();
}
// if()
draw();
}}class Draw_Thread extends Thread {
PingBall c; Draw_Thread(PingBall c) {
super();
this.c = c;
} public void run() {
while (true) { try {
// Color
// color=(Color)((Graphics2D)c.panel.getGraphics()).getPaint();
// ((Graphics2D)c.panel.getGraphics()).setPaint(c.panel.getBackground());
c.panel.getGraphics().fillRect(0, 0, c.panel.getWidth(),
c.panel.getHeight());
// .setBackground(c.panel.getBackground());
// ((Graphics2D)c.panel.getGraphics()).setPaint(color);
sleep(5);
c.draw(); } catch (InterruptedException e) {
e.printStackTrace();
} } }}class Move_Thread extends Thread {
PingBall c; Move_Thread(PingBall c) {
super();
this.c = c;
} public void run() {
while (true) { try {
// c.panel.getGraphics().fillRect(0,0,c.panel.getWidth(),c.panel.getHeight());
sleep(5);
c.move(); } catch (InterruptedException e) {
e.printStackTrace();
} } }}class MyFrame extends JFrame implements MouseListener { JPanel panel = new JPanel(); JButton button = new JButton("增加小球"); public MyFrame() { setBackground(Color.black);
panel.setBackground(Color.gray);
button.setSize(80, 30);
button.addMouseListener(this); panel.setVisible(true);
setLayout(null); button.setLocation(0, 0);
panel.setSize(new Dimension(400, 540-40 ));
panel.setLocation(0, 40); add(button);
add(panel);
// setVisible(true);
} public void mouseClicked(MouseEvent e) { PingBall ball = new PingBall(panel);
Vector vec = new Vector();
vec.add(ball);
ball.move();
System.out.println("new one");
// PingBall ball = new PingBall(); // ball.move();
// if(startX>=400 || startY<=500 || startX<0 || startY <0)
// 400,500 } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) {
} public void mouseEntered(MouseEvent e) {
} public void mouseExited(MouseEvent e) {
}
}public class Ball { /**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
MyFrame aa = new MyFrame();
aa.setSize(400, 500);
aa.setVisible(true);
PingBall ball = new PingBall(aa.panel);
Draw_Thread a = new Draw_Thread(ball);
Move_Thread b = new Move_Thread(ball);
a.start();
b.start(); }}