1.1利用Java实现地球系动画完整实现实例(第4部分)1.1.1产生地球和月亮、并且月亮围着地球转1、如何实现?(1)什么是软件需求“产生地球和月亮、并且月亮围着地球转。”(2)什么是软件设计(思考)设计的结果其实就是产生出“解决方案”。(3)什么是软件开发实现(编程)程序员按照设计人员的设计文档进行编程实现。(4)什么是软件测试——找出程序中的“各种缺陷(Bug)”(5)部署和维护一、第一步:创建两个地球线程1、EarthPlanetApplet程序代码import;import;import;import;import;import;import;publicclassEarthPlanetAppletextendsJAppletimplementsRunnable{privateThreadearthMultiThread=null;privateThreadmoonMultiThread=null;privateGraphicsguiGraphics;privateImageallImagesArray[];privateintcurrentImageIndex=0;privateintimageWidth=0;privateintimageHeight=0;privatebooleanallImagesLoaded=false;privatefinalintNUMBER_IMAGES=18;privatebooleanisRunedByApplication=false;publicstaticvoidmain(String[]args){EarthPlanetFrameapplicationJFrame=null;applicationJFrame=newEarthPlanetFrame(利用Java实现的地球系动画);EarthPlanetAppletthreadApplet=null;threadApplet=newEarthPlanetApplet();applicationJFrame.getContentPane().add(Center,threadApplet);threadApplet.isRunedByApplication=true;threadApplet.init();threadApplet.start();applicationJFrame.setVisible(true);}publicEarthPlanetApplet(){}publicvoidstart(){if(earthMultiThread==null){earthMultiThread=newThread(this);earthMultiThread.start();}if(moonMultiThread==null){//创建出第2个线程对象moonMultiThread=newThread(this);moonMultiThread.start();}}publicvoidstop(){if(earthMultiThread!=null){earthMultiThread.stop();earthMultiThread=null;}if(moonMultiThread!=null){//销毁线程对象moonMultiThread.stop();moonMultiThread=null;}}publicvoidloadAllEarthImage(){guiGraphics=getGraphics();allImagesArray=newImage[NUMBER_IMAGES];MediaTrackertracker=newMediaTracker(this);StringstrImage;for(intcurrentIndex=1;currentIndex=NUMBER_IMAGES;currentIndex++){strImage=/images/img00+((currentIndex10)?0:)+currentIndex+.gif;URLoneImageURL=getClass().getResource(strImage);if(isRunedByApplication){allImagesArray[currentIndex-1]=Toolkit.getDefaultToolkit().getImage(oneImageURL);}else{}allImagesArray[currentIndex-1]=getImage(oneImageURL);tracker.addImage(allImagesArray[currentIndex-1],0);}try{tracker.waitForAll();}catch(InterruptedExceptione){e.printStackTrace();}allImagesLoaded=!tracker.isErrorAny();if(!allImagesLoaded){stop();guiGraphics.drawString(图像加载错误,可能是没有找到文件!,10,40);return;}imageWidth=allImagesArray[0].getWidth(this);imageHeight=allImagesArray[0].getHeight(this);}publicvoidrun(){if(!allImagesLoaded){loadAllEarthImage();}repaint();while(true){try{displayImage(guiGraphics);currentImageIndex++;if(currentImageIndex==NUMBER_IMAGES)currentImageIndex=0;Thread.sleep(50);}catch(InterruptedExceptione){stop();}}}privatevoiddisplayImage(Graphicsg){//分别显示两个地球图像if(!allImagesLoaded)return;if(Thread.currentThread()==earthMultiThread){g.drawImage(allImagesArray[currentImageIndex],(getSize().width-imageWidth)/2,(getSize().height-imageHeight)/2,null);}else{//改变坐标位置g.drawImage(allImagesArray[currentImageIndex],(getSize().width-imageWidth)/4,(getSize().height-imageHeight)/4,null);}}publicvoidpaint(Graphicsg){if(allImagesLoaded){Rectangler=g.getClipBounds();g.clearRect(r.x,r.y,r.width,r.height);displayImage(g);}else}g.drawString(正在加载图像,请等待...,10,20);}上面的代码主要是再创建出第2个线程,并显示出第2套图像。其中的getClipBounds方法得到代表画板边界的矩形,然后从这个矩形中提取宽度和高度数据,再清除该矩形区域。如果除掉下面的语句,将会出现什么现象?g.clearRect(r.x,r.y,r.width,r.height);2、Applet程序方式执行(两个转动的图像)3、应用程序方式执行(两个转动的图像)二、第二步:控制两个地球线程的旋转的速度1、需要补充的知识sleep(毫秒数)是Thread类的静态方法,它能够控制线程。如Thread.sleep(1000);该线程睡眠1秒钟,等1秒钟之后,继续余下代码执行。但在sleep时间间隔期满后,线程不一定立即恢复执行。这是因为在那个时刻,其它线程可能正在运行而且没有被调度为放弃执行,除非出现下面的情况:1)“醒来”的线程具有更高的优先级。2)正在运行的线程因为其它原因而阻塞。2、EarthPlanetApplet程序的代码import;import;import;import;import;import;import;publicclassEarthPlanetAppletextendsJAppletimplementsRunnable{privateThreadearthMultiThread=null;privateThreadmoonMultiThread=null;privateGraphicsguiGraphics;privateImageallImagesArray[];privateintcurrentImageIndex=0;privateintimageWidth=0;privateintimageHeight=0;privatebooleanallImagesLoaded=false;privatefinalintNUMBER_IMAGES=18;privatebooleanisRunedByApplication=false;publicstaticvoidmain(String[]args){EarthPlanetFrameapplicationJFrame=null;applicationJFrame=newEarthPlanetFrame(利用Java实现的地球系动画);EarthPlanetAppletthreadApplet=null;threadApplet=newEarthPlanetApplet();applicationJFrame.getContentPane().add(Center,threadApplet);threadApplet.isRunedByApplication=true;threadApplet.init();threadApplet.start();applicationJFrame.setVisible(true);}publicEarthPlanetApplet(){}publicvoidstart(){if(earthMultiThread==null){earthMultiThread=newThread(this);earthMultiThread.start();}if(moonMultiThread==null){moonMultiThread=newThread(this);moonMultiThread.start();}}publicvoidstop(){if(earthMultiThread!=null){earthMultiThread.stop();earthMultiThread=null;}if(moonMultiThread!=null){moonMultiThread.stop();moonMultiThread=null;}}publicvoidloadAllEarthImage(){guiGraphics=getGraphics();allImagesArray=newImage[NUMBER_IMAGES];MediaTrackertracker=newMediaTracker(this);StringstrImage;for(intcurrentIndex=1;currentIndex=NUMBER_IMAGES;currentIndex++){strImage=/images/img00+((currentIndex10)?0:)+currentIndex+.gif;URLoneImageURL=getClass().getResource(strImage);if(isRunedByApplication){allImag