仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1580|回复: 19
打印 上一主题 下一主题

[学习教程] 了解下JAVA的庞大性实际

[复制链接]
莫相离 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-18 11:34:52 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
在ruby里才是一切皆对象。当然我不并不是很了解ruby,但是ruby确实是将语法简化得很好。
上面要先容的程序的前身是由LarryOBrien原创的一些代码,并以由CraigReynolds于1986年体例的“Boids”程序为基本,事先是为了演示庞大性实际的一个特别成绩,名为“凸显”(Emergence)。
这儿要到达的方针是经由过程为每种植物都划定少量复杂的划定规矩,从而传神地再现植物的群聚举动。每一个植物都能看到看到全部情况和情况中的其他植物,但它只与一系列四周的“群聚同伴”打交道。植物的挪动基于三个复杂的引诱举动:
(1)分开:制止当地群聚同伴过于拥堵。
(2)偏向:服从当地群聚同伴的广泛偏向。
(3)聚合:朝当地群聚同伴组的中央挪动。
更庞大的模子乃至能够包含停滞物的要素,植物能预知和制止与停滞抵触的才能,以是它们能环绕情况中的流动物体自在举动。除此之外,植物也大概有本人的特别方针,这大概会形成群体按特定的路径行进。为简化会商,制止停滞和方针征采的要素并未包含到这里创建的模子中。
只管盘算机自己对照大略,并且接纳的划定规矩也相称复杂,但了局看起来是实在的。也就是说,相称传神的举动从这个复杂的模子中“凸显”出来了。
程序以分解到一同的使用程序/程序片的情势供应:
  1. //:FieldOBeasts.java
  2. //Demonstrationofcomplexitytheory;simulates
  3. //herdingbehaviorinanimals.Adaptedfrom
  4. //aprogrambyLarryOBrienlobrien@msn.com
  5. importjava.awt.*;
  6. importjava.awt.event.*;
  7. importjava.applet.*;
  8. importjava.util.*;
  9. classBeast{
  10. int
  11. x,y,//Screenposition
  12. currentSpeed;//Pixelspersecond
  13. floatcurrentDirection;//Radians
  14. Colorcolor;//Fillcolor
  15. FieldOBeastsfield;//WheretheBeastroams
  16. staticfinalintGSIZE=10;//Graphicsize
  17. publicBeast(FieldOBeastsf,intx,inty,
  18. floatcD,intcS,Colorc){
  19. field=f;
  20. this.x=x;
  21. this.y=y;
  22. currentDirection=cD;
  23. currentSpeed=cS;
  24. color=c;
  25. }
  26. publicvoidstep(){
  27. //Youmovebasedonthosewithinyoursight:
  28. Vectorseen=field.beastListInSector(this);
  29. //Ifyourenotoutinfront
  30. if(seen.size()>0){
  31. //Gatherdataonthoseyousee
  32. inttotalSpeed=0;
  33. floattotalBearing=0.0f;
  34. floatdistanceToNearest=100000.0f;
  35. BeastnearestBeast=
  36. (Beast)seen.elementAt(0);
  37. Enumeratione=seen.elements();
  38. while(e.hasMoreElements()){
  39. BeastaBeast=(Beast)e.nextElement();
  40. totalSpeed+=aBeast.currentSpeed;
  41. floatbearing=
  42. aBeast.bearingFromPointAlongAxis(
  43. x,y,currentDirection);
  44. totalBearing+=bearing;
  45. floatdistanceToBeast=
  46. aBeast.distanceFromPoint(x,y);
  47. if(distanceToBeast<distanceToNearest){
  48. nearestBeast=aBeast;
  49. distanceToNearest=distanceToBeast;
  50. }
  51. }
  52. //Rule1:Matchaveragespeedofthose
  53. //inthelist:
  54. currentSpeed=totalSpeed/seen.size();
  55. //Rule2:Movetowardstheperceived
  56. //centerofgravityoftheherd:
  57. currentDirection=
  58. totalBearing/seen.size();
  59. //Rule3:Maintainaminimumdistance
  60. //fromthosearoundyou:
  61. if(distanceToNearest<=
  62. field.minimumDistance){
  63. currentDirection=
  64. nearestBeast.currentDirection;
  65. currentSpeed=nearestBeast.currentSpeed;
  66. if(currentSpeed>field.maxSpeed){
  67. currentSpeed=field.maxSpeed;
  68. }
  69. }
  70. }
  71. else{//Youareinfront,soslowdown
  72. currentSpeed=
  73. (int)(currentSpeed*field.decayRate);
  74. }
  75. //Makethebeastmove:
  76. x+=(int)(Math.cos(currentDirection)
  77. *currentSpeed);
  78. y+=(int)(Math.sin(currentDirection)
  79. *currentSpeed);
  80. x%=field.xExtent;
  81. y%=field.yExtent;
  82. if(x<0)
  83. x+=field.xExtent;
  84. if(y<0)
  85. y+=field.yExtent;
  86. }
  87. publicfloatbearingFromPointAlongAxis(
  88. intoriginX,intoriginY,floataxis){
  89. //ReturnsbearingangleofthecurrentBeast
  90. //intheworldcoordiantesystem
  91. try{
  92. doublebearingInRadians=
  93. Math.atan(
  94. (this.y-originY)/
  95. (this.x-originX));
  96. //Inversetanhastwosolutions,soyou
  97. //havetocorrectforotherquarters:
  98. if(x<originX){
  99. if(y<originY){
  100. bearingInRadians+=-(float)Math.PI;
  101. }
  102. else{
  103. bearingInRadians=
  104. (float)Math.PI-bearingInRadians;
  105. }
  106. }
  107. //Justsubtracttheaxis(inradians):
  108. return(float)(axis-bearingInRadians);
  109. }catch(ArithmeticExceptionaE){
  110. //Divideby0errorpossibleonthis
  111. if(x>originX){
  112. return0;
  113. }
  114. else
  115. return(float)Math.PI;
  116. }
  117. }
  118. publicfloatdistanceFromPoint(intx1,inty1){
  119. return(float)Math.sqrt(
  120. Math.pow(x1-x,2)+
  121. Math.pow(y1-y,2));
  122. }
  123. publicPointposition(){
  124. returnnewPoint(x,y);
  125. }
  126. //Beastsknowhowtodrawthemselves:
  127. publicvoiddraw(Graphicsg){
  128. g.setColor(color);
  129. intdirectionInDegrees=(int)(
  130. (currentDirection*360)/(2*Math.PI));
  131. intstartAngle=directionInDegrees-
  132. FieldOBeasts.halfFieldOfView;
  133. intendAngle=90;
  134. g.fillArc(x,y,GSIZE,GSIZE,
  135. startAngle,endAngle);
  136. }
  137. }
  138. publicclassFieldOBeastsextendsApplet
  139. implementsRunnable{
  140. privateVectorbeasts;
  141. staticfloat
  142. fieldOfView=
  143. (float)(Math.PI/4),//Inradians
  144. //Deceleration%persecond:
  145. decayRate=1.0f,
  146. minimumDistance=10f;//Inpixels
  147. staticint
  148. halfFieldOfView=(int)(
  149. (fieldOfView*360)/(2*Math.PI)),
  150. xExtent=0,
  151. yExtent=0,
  152. numBeasts=50,
  153. maxSpeed=20;//Pixels/second
  154. booleanuniqueColors=true;
  155. ThreadthisThread;
  156. intdelay=25;
  157. publicvoidinit(){
  158. if(xExtent==0&&yExtent==0){
  159. xExtent=Integer.parseInt(
  160. getParameter("xExtent"));
  161. yExtent=Integer.parseInt(
  162. getParameter("yExtent"));
  163. }
  164. beasts=
  165. makeBeastVector(numBeasts,uniqueColors);
  166. //Nowstartthebeastsa-rovin:
  167. thisThread=newThread(this);
  168. thisThread.start();
  169. }
  170. publicvoidrun(){
  171. while(true){
  172. for(inti=0;i<beasts.size();i++){
  173. Beastb=(Beast)beasts.elementAt(i);
  174. b.step();
  175. }
  176. try{
  177. thisThread.sleep(delay);
  178. }catch(InterruptedExceptionex){}
  179. repaint();//Otherwiseitwontupdate
  180. }
  181. }
  182. VectormakeBeastVector(
  183. intquantity,booleanuniqueColors){
  184. VectornewBeasts=newVector();
  185. Randomgenerator=newRandom();
  186. //UsedonlyifuniqueColorsison:
  187. doublecubeRootOfBeastNumber=
  188. Math.pow((double)numBeasts,1.0/3.0);
  189. floatcolorCubeStepSize=
  190. (float)(1.0/cubeRootOfBeastNumber);
  191. floatr=0.0f;
  192. floatg=0.0f;
  193. floatb=0.0f;
  194. for(inti=0;i<quantity;i++){
  195. intx=
  196. (int)(generator.nextFloat()*xExtent);
  197. if(x>xExtent-Beast.GSIZE)
  198. x-=Beast.GSIZE;
  199. inty=
  200. (int)(generator.nextFloat()*yExtent);
  201. if(y>yExtent-Beast.GSIZE)
  202. y-=Beast.GSIZE;
  203. floatdirection=(float)(
  204. generator.nextFloat()*2*Math.PI);
  205. intspeed=(int)(
  206. generator.nextFloat()*(float)maxSpeed);
  207. if(uniqueColors){
  208. r+=colorCubeStepSize;
  209. if(r>1.0){
  210. r-=1.0f;
  211. g+=colorCubeStepSize;
  212. if(g>1.0){
  213. g-=1.0f;
  214. b+=colorCubeStepSize;
  215. if(b>1.0)
  216. b-=1.0f;
  217. }
  218. }
  219. }
  220. newBeasts.addElement(
  221. newBeast(this,x,y,direction,speed,
  222. newColor(r,g,b)));
  223. }
  224. returnnewBeasts;
  225. }
  226. publicVectorbeastListInSector(Beastviewer){
  227. Vectoroutput=newVector();
  228. Enumeratione=beasts.elements();
  229. BeastaBeast=(Beast)beasts.elementAt(0);
  230. intcounter=0;
  231. while(e.hasMoreElements()){
  232. aBeast=(Beast)e.nextElement();
  233. if(aBeast!=viewer){
  234. Pointp=aBeast.position();
  235. Pointv=viewer.position();
  236. floatbearing=
  237. aBeast.bearingFromPointAlongAxis(
  238. v.x,v.y,viewer.currentDirection);
  239. if(Math.abs(bearing)<fieldOfView/2)
  240. output.addElement(aBeast);
  241. }
  242. }
  243. returnoutput;
  244. }
  245. publicvoidpaint(Graphicsg){
  246. Enumeratione=beasts.elements();
  247. while(e.hasMoreElements()){
  248. ((Beast)e.nextElement()).draw(g);
  249. }
  250. }
  251. publicstaticvoidmain(String[]args){
  252. FieldOBeastsfield=newFieldOBeasts();
  253. field.xExtent=640;
  254. field.yExtent=480;
  255. Frameframe=newFrame("FieldOBeasts");
  256. //Optionallyuseacommand-lineargument
  257. //forthesleeptime:
  258. if(args.length>=1)
  259. field.delay=Integer.parseInt(args[0]);
  260. frame.addWindowListener(
  261. newWindowAdapter(){
  262. publicvoidwindowClosing(WindowEvente){
  263. System.exit(0);
  264. }
  265. });
  266. frame.add(field,BorderLayout.CENTER);
  267. frame.setSize(640,480);
  268. field.init();
  269. field.start();
  270. frame.setVisible(true);
  271. }
  272. }///:~
复制代码
只管这并不是对CraigReynold的“Boids”例子中的举动完善重现,但它却展示出了本人独占的诱人以外。经由过程对数字举行调剂,便可举行周全的修正。至于与这类群聚举动有关的更多的情形,人人能够会见CraigReynold的主页——在谁人中央,乃至还供应了Boids一个公然的3D展现版本:
http://www.hmt.com/cwr/boids.html
为了将这个程序作为一个程序片运转,请在HTML文件中设置下述程序片标记:
  1. <applet
  2. code=FieldOBeasts
  3. width=640
  4. height=480>
  5. <paramname=xExtentvalue="640">
  6. <paramname=yExtentvalue="480">
  7. </applet>
复制代码
你通过从书的数量和开发周期及运行速度来证明:net网页编程和ruby要比java简单。
莫相离 该用户已被删除
沙发
 楼主| 发表于 2015-2-12 06:10:54 | 显示全部楼层
Sun公司看见Oak在互联网上应用的前景,于是改造了Oak,于1995年5月以Java的名称正式发布。Java伴随着互联网的迅猛发展而发展,逐渐成为重要的网络编程语言。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-19 01:02

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表