先看代码import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.Iterator;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;/**
 * @author CITLi 2008-12-29
 * @version 1.0
 */
public class Bounce { /**
 * @param args
 */
public static void main(String[] args) {
JFrame frame = new BounceFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}}class BounceFrame extends JFrame {
public BounceFrame() {
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
setTitle("Bounce");

panel = new BallPanel();
add(panel, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
addButton(buttonPanel, "Start", 
new ActionListener() {
public void actionPerformed(ActionEvent event) {
addBall();
}
}
);

addButton(buttonPanel, "Close", 
new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
}
);

add(buttonPanel, BorderLayout.SOUTH);
}

public void addButton(Container c, String title, ActionListener listener) {
JButton button = new JButton(title);
c.add(button);
button.addActionListener(listener);
}

public void addBall() {
Ball ball = new Ball();
panel.add(ball, ADD_COUNT++);
Runnable r = new BallRunnable(ball, panel);
Thread t = new Thread(r);
t.start();
}

private BallPanel panel;
private static int ADD_COUNT = 0;
private static final int DEFAULT_WIDTH = 450;
private static final int DEFAULT_HEIGHT = 350;
}
class BallPanel extends JPanel {
public void add(Ball ball, int colorIndex) {
balls.add(ball);
Color color = Color.BLACK;
int colorflg = colorIndex % COLOR_NUMBER;
switch(colorflg) {
            case 0 : color = Color.RED; break;
            case 1 : color = Color.LIGHT_GRAY; break;
            case 2 : color = Color.PINK; break;
            case 3 : color = Color.BLUE; break;
            case 4 : color = Color.GREEN; break;
            case 5 : color = Color.ORANGE; break;
            case 6 : color = Color.YELLOW; break;
            default : color = Color.BLACK;
        }
        colors.add(color);
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
Iterator<Ball> ballsIte = balls.iterator();
Iterator<Color> colorsIte = colors.iterator();
while (ballsIte.hasNext()) {
Ball b = ballsIte.next();
Color c = colorsIte.next();
g2.setColor(c);
g2.fill(b.getShape());
}
}

private ArrayList<Ball> balls = new ArrayList<Ball>();
private ArrayList<Color> colors = new ArrayList<Color>();
private static final int COLOR_NUMBER = 8;
}class BallPanel2 extends JPanel {
public void add(Ball ball, int colorIndex) {
balls.add(ball);
Graphics g = this.getGraphics();
Color color = Color.BLACK;
int colorflg = colorIndex % COLOR_NUMBER;
switch(colorflg) {
            case 0 : color = Color.RED; break;
            case 1 : color = Color.LIGHT_GRAY; break;
            case 2 : color = Color.PINK; break;
            case 3 : color = Color.BLUE; break;
            case 4 : color = Color.GREEN; break;
            case 5 : color = Color.ORANGE; break;
            case 6 : color = Color.YELLOW; break;
            default : color = Color.BLACK;
        }
g.setColor(color);
graphicses.add(g);
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
Iterator<Ball> ballsIte = balls.iterator();
Iterator<Graphics> graphicsesIte = graphicses.iterator();
while (ballsIte.hasNext()) {
Ball b = ballsIte.next();
Graphics2D g2 = (Graphics2D) graphicsesIte.next();
g2.fill(b.getShape());
}
}

private ArrayList<Ball> balls = new ArrayList<Ball>();
private ArrayList<Graphics> graphicses = new ArrayList<Graphics>();
private static final int COLOR_NUMBER = 8;
}class Ball {
public void move(Rectangle2D bounds) {
x += dx;
y += dy;
if (x < bounds.getMinX()) {
x = bounds.getMinX();
dx = -dx;
}
if (x + XSIZE >= bounds.getMaxX()) {
x = bounds.getMaxX() - XSIZE;
dx = -dx;
}
if (y < bounds.getMinY()) {
y = bounds.getMinY();
dy = -dy;
}
if (y + YSIZE >= bounds.getMaxY()) {
y = bounds.getMaxY() - YSIZE;
dy = -dy;
}
}

public Ellipse2D getShape() {
return new Ellipse2D.Double(x, y, XSIZE, YSIZE);
}

private static final int XSIZE = 15;
private static final int YSIZE = 15;
private double x = 0;
private double y = 0; 
private double dx = 1;
private double dy = 1;
}class BallRunnable implements Runnable {

public BallRunnable(Ball aBall, Component aComponent) {
ball = aBall;
component = aComponent;
} /* (non-Javadoc)
 * @see java.lang.Runnable#run()
 */
public void run() {
try {
while (true) {
ball.move(component.getBounds());
component.repaint();
Thread.sleep(DLAYE);
}
} catch(InterruptedException e) {
Thread.currentThread().interrupt();
}
}

private Ball ball;
private Component component;
private static final int DLAYE = 3;
}
在学习线程中给出一个示例,就是每次点击start按钮,画面就添加一个小球。但是我想给每个小球都置上颜色,所以添加了给每个小球赋颜色的处理。如BallPanel和BallPanel2中处理。
问题:在使用BallPanel没有问题,但是在使用BallPanel2时却发现画面上的小球时有时无的显示。不知道是为啥,请高手给指点一下。