java多个小球碰撞_JAVA核心技术之球体碰撞多线程版

该博客演示了一个使用JAVA编写的多球碰撞动画程序。通过创建BallRunnable类来实现球体的移动和反弹,并在BounceThread类中进行动画的更新和绘制。程序中,每个球体在指定区域内根据设定的速度和方向移动,当碰到边界时会改变方向。用户可以通过按钮启动和关闭动画。
摘要由CSDN通过智能技术生成

1 /**

2 @version1.32 2004-07-273 @authorCay Horstmann4 */

5

6 import java.awt.*;7 import java.awt.event.*;8 import java.awt.geom.*;9 import java.util.*;10 import javax.swing.*;11

12 /**

13 Shows an animated bouncing ball.14 */

15 public classBounceThread16 {17 public static voidmain(String[] args)18 {19 JFrame frame = newBounceFrame();20 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);21 frame.setVisible(true);22 }23 }24

25 /**

26 A runnable that animates a bouncing ball.27 */

28 class BallRunnable implementsRunnable29 {30 /**

31 Constructs the runnable.32 @aBall the ball to bounce33 @aPanel the component in which the ball bounces34 */

35 publicBallRunnable(Ball aBall, Component aComponent)36 {37 ball =aBall;38 component =aComponent;39 }40

41 public voidrun()42 {43 try

44 {45 for (int i = 1; i <= STEPS; i++)46 {47 ball.move(component.getBounds());48 component.repaint();49 Thread.sleep(DELAY);50 }51 }52 catch(InterruptedException e)53 {54 }55 }56

57 privateBall ball;58 privateComponent component;59 public static final int STEPS = 1000;60 public static final int DELAY = 5;61 }62

63 /**

64 A ball that moves and bounces off the edges of a65 rectangle66 */

67 classBall68 {69 /**

70 Moves the ball to the next position, reversing direction71 if it hits one of the edges72 */

73 public voidmove(Rectangle2D bounds)74 {75 x +=dx;76 y +=dy;77 if (x =bounds.getMaxX())83 {84 x = bounds.getMaxX() -XSIZE;85 dx = -dx;86 }87 if (y =bounds.getMaxY())93 {94 y = bounds.getMaxY() -YSIZE;95 dy = -dy;96 }97 }98

99 /**

100 Gets the shape of the ball at its current position.101 */

102 publicEllipse2D getShape()103 {104 return newEllipse2D.Double(x, y, XSIZE, YSIZE);105 }106

107 private static final int XSIZE = 15;108 private static final int YSIZE = 15;109 private double x = 0;110 private double y = 0;111 private double dx = 1;112 private double dy = 1;113 }114

115 /**

116 The panel that draws the balls.117 */

118 class BallPanel extendsJPanel119 {120 /**

121 Add a ball to the panel.122 @paramb the ball to add123 */

124 public voidadd(Ball b)125 {126 balls.add(b);127 }128

129 public voidpaintComponent(Graphics g)130 {131 super.paintComponent(g);132 Graphics2D g2 =(Graphics2D) g;133 for(Ball b : balls)134 {135 g2.fill(b.getShape());136 }137 }138

139 private ArrayList balls = new ArrayList();140 }141

142 /**

143 The frame with panel and buttons.144 */

145 class BounceFrame extendsJFrame146 {147 /**

148 Constructs the frame with the panel for showing the149 bouncing ball and Start and Close buttons150 */

151 publicBounceFrame()152 {153 setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);154 setTitle("BounceThread");155

156 panel = newBallPanel();157 add(panel, BorderLayout.CENTER);158 JPanel buttonPanel = newJPanel();159 addButton(buttonPanel, "Start",160 newActionListener()161 {162 public voidactionPerformed(ActionEvent event)163 {164 addBall();165 }166 });167

168 addButton(buttonPanel, "Close",169 newActionListener()170 {171 public voidactionPerformed(ActionEvent event)172 {173 System.exit(0);174 }175 });176 add(buttonPanel, BorderLayout.SOUTH);177 }178

179 /**

180 Adds a button to a container.181 @paramc the container182 @paramtitle the button title183 @paramlistener the action listener for the button184 */

185 public voidaddButton(Container c, String title, ActionListener listener)186 {187 JButton button = newJButton(title);188 c.add(button);189 button.addActionListener(listener);190 }191

192 /**

193 Adds a bouncing ball to the canvas and starts a thread194 to make it bounce195 */

196 public voidaddBall()197 {198 Ball b = newBall();199 panel.add(b);200 Runnable r = newBallRunnable(b, panel);201 Thread t = newThread(r);202 t.start();203 }204

205 privateBallPanel panel;206 public static final int DEFAULT_WIDTH = 450;207 public static final int DEFAULT_HEIGHT = 350;208 public static final int STEPS = 1000;209 public static final int DELAY = 3;210 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值