1.小球类 package teacherBall; import java.awt.Color; import java.awt.Rectangle; import java.awt.Shape; import java.awt.geom.Ellipse2D; import java.util.Random; public class Ball { private double x,y; public static final int X_SIZE=20; public static final int Y_SIZE=20; private int xMoveDis=10; private int yMoveDis=10; private Ellipse2D shape; private Color color; 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 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 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 Color getColor() { return color; } public void setColor(Color color) { this.color = color; } } 2.主界面 package teacherBall; import java.awt.BorderLayout; import java.awt.Panel; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import com.myemap.ui.Face; public class BallFrame{ JButton jbStart; JButton jbStop; JFrame jf; BallPanel bp; public BallFrame(){ jf = new JFrame("Ball Game"); jf.setLayout(new BorderLayout()); bp = new BallPanel(); Panel buttonPanel = new Panel(); jbStart = new JButton("开始"); jbStop = new JButton("停止"); buttonPanel.add(jbStart); buttonPanel.add(jbStop); jf.add(buttonPanel,BorderLayout.SOUTH); jf.add(bp,BorderLayout.CENTER); this.actionHandler(); this.showMi(); } private void showMi(){ jf.setSize(400,300); //jf.pack(); Toolkit tool = Toolkit.getDefaultToolkit(); double x =tool.getScreenSize().getWidth(); double y =tool.getScreenSize().getHeight(); jf.setLocation((int)(x-jf.getWidth())/2,(int)(y-jf.getHeight())/2); jf.setVisible(true); jf.setResizable(false); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private void actionHandler(){ jbStart.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { System.out.println("start pressed"); /*1.创建小球 2.创建小球线程 3.加入小球*/ Ball ball = new Ball(); new BallThread(ball,bp.getBounds()).start(); bp.addBall(ball); } }); jbStop.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { System.out.println("stop pressed"); System.exit(0); } }); } public static void main(String[] args) { Face.setFace(); new BallFrame(); } } 3.小球面板类 package teacherBall; import java.awt.Graphics; import java.awt.Graphics2D; import java.util.ArrayList; import java.util.List; import javax.swing.JPanel; public class BallPanel extends JPanel{//负责画小球 private List<Ball> balls; public BallPanel(){ balls = new ArrayList<Ball>(); startPanitThread(); } public void addBall(Ball ball){ balls.add(ball); } private void startPanitThread(){ new Thread(){ @Override public void run() { while(true) { repaint(); try { Thread.sleep(50); //50ms重绘一次 } 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()); } } } 4.小球线程类 package teacherBall; import java.awt.Rectangle; import java.util.List; 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++; } } }