博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java用线程做小球碰撞_JAVA核心技术之球体碰撞多线程版
阅读量:6691 次
发布时间:2019-06-25

本文共 3884 字,大约阅读时间需要 12 分钟。

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 }

转载地址:http://itdoo.baihongyu.com/

你可能感兴趣的文章
DroidPilot使用第一步 - 安装
查看>>
0059-乘积问题
查看>>
2019年的第一篇随笔
查看>>
关于公网ip的一些信息(摘抄)
查看>>
5分钟弄懂Docker!
查看>>
BZOJ1076:[SCOI2008]奖励关(状压DP,期望)
查看>>
BZOJ2223/3524:[POI2014] Couriers(主席树)
查看>>
MyEclipse — Maven+Spring+Struts+Hibernate 整合 [学习笔记-5]
查看>>
Nodejs 连接各种数据库集合例子
查看>>
easyui的datagrid用js插入数据等编辑功能的实现
查看>>
Windows App开发之集合控件与数据绑定
查看>>
AMD、CMD/AMD与CMD的区别
查看>>
Python~第一天
查看>>
Linux管理用户账号
查看>>
redis中使用lua脚本
查看>>
颜色数组
查看>>
ELASTICSEARCH清理过期数据
查看>>
oo第三次博客作业
查看>>
《结对-结对编项目作业名称-需求分析》
查看>>
iView3.x Anchor(锚点)组件 导航锚点
查看>>