//老师代码
//
public class Ball {
private double x;
private double y;
private static final double X_SIZE = 20;
private static final double Y_SIZE = 20;
private double xMoveDis;
private double yMoveDis;
private Color color;
private Ellipse2D shape;
private static Random random;
static {
random = new Random();
}
public Ball() {
color = new Color(random.nextInt(256)
,random.nextInt(256),random.nextInt(256));
xMoveDis = 5 + random.nextInt(5)-2;
yMoveDis = 5 + random.nextInt(5)-2;
}
public void move(Rectangle rect) {
x += xMoveDis;
y += yMoveDis;
//判断边界问题
if (x < rect.getMinX()) {
x = rect.getMinX();
xMoveDis = -xMoveDis;
} else if ((x+X_SIZE) > rect.getMaxX()) {
x = rect.getMaxX()-X_SIZE;
xMoveDis = -xMoveDis;
}
if (y < rect.getMinY()) {
y = rect.getMinY();
yMoveDis = -yMoveDis;
} else if ((y+Y_SIZE) > rect.getMaxY()) {
y = rect.getMaxY()-Y_SIZE;
yMoveDis = -yMoveDis;
}
}
public Shape getShape() {
if (shape == null) {
shape = new Ellipse2D.Double(x, y, X_SIZE, Y_SIZE);
} else {
shape.setFrame(x, y, X_SIZE, Y_SIZE);
}
return shape;
}
public Color getColor() {
return color;
}
}
public class BallFrame extends JFrame {
private JButton startBtn;
private JButton endBtn;
private BallPanel ballPanel;
public BallFrame() {
ballPanel = new BallPanel();
this.add(ballPanel, BorderLayout.CENTER);
JPanel btnPanel = new JPanel();
startBtn = new JButton("start");
btnPanel.add(startBtn);
endBtn = new JButton("end");
btnPanel.add(endBtn);
this.add(btnPanel, BorderLayout.SOUTH);
actionHandler();
showMe();
}
private void actionHandler() {
startBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.out.println("startBtn pressed");
//1.创建小球
Ball ball = new Ball();
//2.启动小球线程
new BallThread(ball, ballPanel.getBounds()).start();
//3.存入BallPanel
ballPanel.addBall(ball);
}
});
endBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
}
private void showMe() {
this.setTitle("小球碰撞");
this.setSize(400,300);
Toolkit toolkit = Toolkit.getDefaultToolkit();
double width = toolkit.getScreenSize().getWidth();
double height = toolkit.getScreenSize().getHeight();
this.setLocation((int)(width-this.getWidth())/2
,(int)(height-this.getHeight())/2);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
///
public class BallPanel extends JPanel{
private List<Ball> balls;
public BallPanel() {
balls = new ArrayList<Ball>();
startPaintThread();
}
private void startPaintThread() {
new Thread(){
public void run() {
while(true) {
// System.out.println("repaint");
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
for (Ball ball : balls) {
g2.setColor(ball.getColor());
g2.fill(ball.getShape());
}
}
public void addBall(Ball ball) {
balls.add(ball);
}
}
/
public class BallThread extends Thread {
private Ball ball;
private Rectangle rect;
public BallThread(Ball ball, Rectangle rect) {
this.ball = ball;
this.rect = rect;
}
@Override
public void run() {
int count = 0;
while (count < 10000) {
ball.move(rect);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
count++;
}
}
}
public class BallMain {
public static void main(String[] args) {
new BallFrame();
}
}