翻译文献-基于实时仿真的嵌入式系统结构设计_第1页
翻译文献-基于实时仿真的嵌入式系统结构设计_第2页
翻译文献-基于实时仿真的嵌入式系统结构设计_第3页
翻译文献-基于实时仿真的嵌入式系统结构设计_第4页
翻译文献-基于实时仿真的嵌入式系统结构设计_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、大连交通大学 2016 届本科生毕业设计(论文)外文翻译 1 外文原文 More Animation Images and Sound Animations are fun and easy to do in Java, but theres only so much you can do with the builtin,Java methods for lines and fonts and colors. For really interestinganimations, you have toprovide your own images for each frame of the a

2、nimationand having sounds is nice, as well.Today, youll do more with animations, incorporating images and sounds into Java applets.Specifically, youll explore the following topics: 1.Using imagesgetting them from the server, loading them into Java, and displaying them in your applet 2.Creating anima

3、tions by using images, including an extensive example 3.Using soundsgetting them and playing them at the appropriate times 4.Suns Animator appletan easy way to organize animations and sounds in Java 5.Double-bufferinghardcore flicker avoidance Retrieving and Using Images Basic image handling in Java

4、 is easy. The Image class in java.awt provides abstract methods to represent common image behavior, and special methods defined in Applet and Graphics give you everything you need to load and display images in your applet as easily as drawing a rectangle. In this section, youll learn about how to ge

5、t and draw images in your Java applets. Getting Images To display an image in your applet, you first must load that image over the net into your Java program. Images are stored as separate files from your Java class files, so you have to tell Java where to find them. The Applet class provides a meth

6、od called getImage, which loads an image and automatically creates an instance of the Image class for you. To use it, all you have to do is import the java.awt.Image class, and then give getImage the URL of the image you want to load. There are two ways of doing the latter step: 1.The getImage metho

7、d with a single argument (an object of type URL) retrieves the image at that URL. The getImage method with two arguments: the base URL (also a URL object) and a string representing the path or filename of the actual image (relative to the base). 大连交通大学 2016 届本科生毕业设计(论文)外文翻译 2 Although the first way

8、may seem easier (just plug in the URL as a URL object), the second is more flexible. Remember, because youre compiling Java files, if you include a hard-coded URL of an image and then move your files around to a different location, you have to recompile all your Java files. The latter form, therefor

9、e, is usually the one to use. The Applet class also provides two methods that will help with the base URL argument to getImage: The getDocumentBase() method returns a URL object representing the directory of the HTML file that contains this applet. So, for example, if the HTML file is located at get

10、DocumentBase returns a URL pointing to that path. The getCodeBase() method returns a string representing the directory in which this applet is containedwhich may or may not be the same directory as the HTML file, depending on whether the CODEBASE attribute in is set or not. Whether you use getDocume

11、ntBase() or getCodebase() depends on whether your images are relative to your HTML files or relative to your Java class files. Use whichever one applies better to your situation. Note that either of these methods is more flexible than hard-coding a URL or pathname into the getImage method; using eit

12、her getDocumentBase or getCodeBase enables you to move your HTML files and applets around and Java can still find your images. Here are a few examples of getImage, to give you an idea of how to use it. This first call to getImage retrieves the file at that specific URL (“ If any part of that URL cha

13、nges, you have to recompile your Java applet to take the new path into account: Image img = getImage( new URL(“ In the following form of getImage, the image.gif file is in the same directory as the HTML files that refer to this applet: Image img = getImage(getDocumentBase(), “image.gif”) In this sim

14、ilar form, the file image.gif is in the same directory as the applet itself: Image img = getImage(getCodeBase(), “image.gif”) If you have lots of image files, its common to put them into their own subdirectory. This form of getImage looks for the file image.gif in the directory images, which, in tur

15、n, is in the same directory as the Java applet: Image img = getImage(getCodeBase(), “images/image.gif”) 大连交通大学 2016 届本科生毕业设计(论文)外文翻译 3 If Java cant find the file youve indicated, getImage returns null. Your program will continue to runyou just wont see that image on your screen when you try to draw

16、it. Drawing Images All that stuff with getImage does nothing except go off and retrieve an image and stuff it into an instance of the Image class. Now that you have an image, you have to do something with it. The most likely thing youre going to want to do is display it as you would a rectangle or a

17、 text string. The Graphics class provides two methods to do just that, both called drawImage. The first version of drawImage takes four arguments: the image to display, the x and y positions of the top left corner, and this: void paint() g.drawImage(img, 10, 10, this); This first form does what you

18、would expect it to: it draws the image in its original dimensions with the top left corner at the given x and y positions. Listing 11.1 shows the code for a very simple applet that loads in an image called ladybug.gif and displays it. Figure 11.1 shows the obvious result. Listing 11.1. The Ladybug a

19、pplet. 1: import java.awt.Graphics; 2: import java.awt.Image; 3: 4: public class LadyBug extends java.applet.Applet 5: 6: Image bugimg; 7: 8: public void init() 9: bugimg = getImage(getCodeBase(), 10: “images/ladybug.gif”); 11: 12: 13: public void paint(Graphics g) 14: g.drawImage(bugimg,10,10,this)

20、; 15: 16: 大连交通大学 2016 届本科生毕业设计(论文)外文翻译 4 The second form of drawImage takes six arguments: the image to draw, the x and y coordinates, a width and height of the image bounding box, and this. If the width and height arguments for the bounding box are smaller or larger than the actual image, the image

21、 is automatically scaled to fit. Using those extra arguments enables you to squeeze and expand images into whatever space you need them to fit in (keep in mind, however, that there may be some image degradation from scaling it smaller or larger than its intended size). One helpful hint for scaling i

22、mages is to find out the size of the actual image that youve loaded, so you can then scale it to a specific percentage and avoid distortion in either direction. Two methods defined for the Image class enable you do this: getWidth() and getHeight(). Both take a single argument, an instance of ImageOb

23、server, which is used to track the loading of the image (more about this later). Most of the time, you can use just this as an argument to eithergetWidth() or getHeight(). If you stored the ladybug image in a variable called bugimg, for example, this line returns the width of that image, in pixels:

24、theWidth = bugimg.getWidth(this); Listing 11.2 shows another use of the ladybug image, this time scaled several times to different Listing 11.2. More Ladybugs, scaled. 1: import java.awt.Graphics; 2: import java.awt.Image; 3: 4: public class LadyBug2 extends java.applet.Applet 5: 6: Image bugimg; 7:

25、 8: public void init() 9: bugimg = getImage(getCodeBase(), 10: “images/ladybug.gif”); 11: 12: 13: public void paint(Graphics g) 14: int iwidth = bugimg.getWidth(this); 大连交通大学 2016 届本科生毕业设计(论文)外文翻译 5 15: int iheight = bugimg.getHeight(this); 16: int xpos = 10; 17: 18: / 25 % Listing 11.2. continued 1

26、9: g.drawImage(bugimg,xpos,10, 20: iwidth / 4, iheight / 4, this); 21: 22: / 50 % 23: xpos += (iwidth / 4) + 10; 24: g.drawImage(bugimg, xpos , 10, 25: iwidth / 2, iheight / 2, this); 26: 27: / 100% 28: xpos += (iwidth / 2) + 10; 29: g.drawImage (bugimg, xpos, 10, this); 30: 31: / 150% x, 25% y 32:

27、g.drawImage(bugimg, 10, iheight + 30, 33: (int)(iwidth * 1.5), iheight / 4, this); 34: 35: Ive been steadfastly ignoring mentioning that last argument to drawImage: the mysterious this, which also appears as an argument to getWidth() and getHeight(). Why is this argument used? Its official use is to

28、 pass in an object that functions as an ImageObserver (that is, an object that implements the ImageObserver interface). Image observers enable you to watch the progress of how far along an image is in the loading process and to make decisions Listing 11.2. continued Ive been steadfastly ignoring men

29、tioning that last argument to drawImage: the mysterious this, which also appears as an argument to getWidth() and getHeight(). Why is this argument used? Its official use is to pass in an object that functions as an ImageObserver (that is, an object that implements the ImageObserver interface). Imag

30、e observers enable you to watch the progress of how far along an image is in the loading process and to make decisions when the image is only fully or 大连交通大学 2016 届本科生毕业设计(论文)外文翻译 6 partially loaded. The Applet class, which your applet inherits from, contains default behavior for watching for images

31、 that should work in the majority of caseshence, the this argument to drawImage(), getWidth(), and getHeight(). The only reason youll want to use an alternate argument in its place is if you are tracking lots of image loading synchronously. See the java.awt.image.ImageObserver class for more details

32、. Modifying Images In addition to the basics and handling images described in this section, the java.awt.image package provides more classes and interfaces that enable you to modify images and their internal colors, or to create bitmap images by hand. Most of these classes require background knowled

33、ge in image processing, including a good grasp of color models and bitwise operations. All these things are outside the scope of an introductory book on Java, but if you have this background (or youre interested in trying it out), the classes in java.awt.image will be helpful to you. Take a look at

34、the example code for creating and using images that comes with the Java development kit for examples of how to use the image classes. Creating Animation Using Images Creating animations by using images is much the same as creating images by using fonts, colors, or shapesyou use the same methods, the

35、 same procedures for painting, repainting, and reducing flicker that you learned about yesterday. The only difference is that you have a stack of images to flip through rather than a set of painting methods. Probably the best way to show you how to use images for animation is simply to walk through

36、an example. Heres an extensive one of an animation of a small cat called Neko. An Example: Neko Neko was a small Macintosh animation/game written and drawn by Kenji Gotoh in 1989. “Neko” is Japanese for “cat,” and the animation is of a small kitten that chases the mouse pointer around the screen, sl

37、eeps, scratches, and generally acts cute. The Neko program has since been ported to just about every possible platform, as well as rewritten as a popular screensaver. For this example, youll implement a small animation based on the original Neko graphics. Because the original Neko the cat was autono

38、mous (it could “sense” the edges of the window and turn and run in a different direction), this applet merely causes Neko to run in from the left side of the screen, 大连交通大学 2016 届本科生毕业设计(论文)外文翻译 7 stop in the middle, yawn, scratch its ear, sleep a little, and then run off to the right. Note: This is

39、 by far the largest of the applets discussed in this book, and if I either print it here and then describe it, or build it up line by line, youll be here for days. Instead, Im going to describe the parts of this applet independently, and Im going to leave out the basicsthe stuff you learned yesterda

40、y about starting and stopping threads, what the run() method does, and so on. All the code is printed later today so that you can put it all together. Before you begin writing Java code to construct an animation, you should have all the images that form the animation itself. For this version of Neko

41、 there are nine of them (the original has36) Ive stored these images in a subdirectory of my applet directory called, appropriately, images. Where you store your images isnt all the important, but you should take note of where youve put them because youll need that information Now, onto the applet.

42、The basic idea of animation by using images is that you have a set of images, and you display them one at a time, rapidly, so they give the appearance of movement. The easiest way to manage this in Java is to store the images in an array of class Image, and then to have a special variable that store

43、s a reference to the current image. Technical Note: The java.util class contains a class (HashTable) that implements a hash table. For large amounts of images, a hash table is faster to find and retrieve images from than an array is. Because you have a relatively small amount of images here, and bec

44、ause arrays are easier to deal with, Ill use an array here. For the Neko applet, youll include instance variables to implement both these things: an array to hold the images called nekopics, and a variable of type Image to hold the current image: Image nekopics = new Image9; Image currentimg; Becaus

45、e youll need to pass the position of the current image around between the methods in this applet, youll also need to keep track of the current x and y positions. The y stays constant for this particular applet, but the x may vary. lets add two instance variables for those two positions: int xpos; in

46、t ypos = 50; Now, onto the body of the applet. During the applets initialization, youll read in all the images and store them in the nekopics array. This is the sort of operation that 大连交通大学 2016 届本科生毕业设计(论文)外文翻译 8 works especially well in an init() method. Given that you have nine images with nine

47、different filenames, you could do a separate call to getImage for each one. You can save at least a little typing, however, by creating an array of the file names (nekosrc, an array of strings) and then just using a for loop to iterate over each one. Heres the init() method for the Neko applet that

48、loads all the images into the nekopics array: public void init() String nekosrc = “right1.gif”, “right2.gif”, “stop.gif”, “yawn.gif”, “scratch1.gif”, “scratch2.gif”,”sleep1.gif”, “sleep2.gif”, “awake.gif” ; for (int i=0; i nekopics.length; i+) nekopicsi = getImage(getCodeBase(), “images/” + nekosrci

49、); Note here in the call to getImage that the directory these images are stored in is included as part of the path. With the images loaded, the next step is to start animating the bits of the applet. You do this inside the applets threads run() method. In this applet, Neko does five main things: 1.R

50、uns in from the left side of the screen 2.Stops in the middle and yawns 3.Scratches four times 4.Sleeps Wakes up and runs off to the right side of the screen Because you could animate this applet by merely painting the right image to the screen at the right time, it makes more sense to write this ap

51、plet so that many of Nekos activities are contained in individual methods. This way, you can reuse some of the activities (the animation of Neko running, in particular) if you want Neko to do things in a different order. Lets start by creating a method to make Neko run. Because youre going to be usi

52、ng this one twice, making it generic is a good plan. Lets create the nekorun method, which takes two arguments: the x position to start, and the x position to end. Neko then runs between those two positions (the y remains constant). 大连交通大学 2016 届本科生毕业设计(论文)外文翻译 9 There are two images that represent

53、Neko running; so, to create the running effect, you need to alternate between those two images (stored in positions 0 and 1 of the image array), as well as move them across the screen. The moving part is a simple for loop between the start and end arguments, setting the global x position to the curr

54、ent loop value. Swapping the images means merely testing to see which one is active at any turn of the loop and assigning the other one to the current image. Finally, at each new frame, youll call repaint and sleep for a bit. Actually, given that during this animation there will be a lot of sleeping

55、 of various intervals, it makes sense to create a method that does the sleeping for the appropriate time interval. Call it pauseheres its definition: void pause(int time) try Thread.sleep(time); catch (InterruptedException e) Back to the nekorun method. To summarize, nekorun iterates from the start

56、position to the end position. For each turn of the loop, it sets the current x position, sets currentimg to the right animation frame, calls repaint, and pauses. Got it? Heres the definition of nekorun: void nekorun(int start, int end) for (int i = start; i end; i+=10) this.xpos = i; / swap images i

57、f (currentimg = nekopics0) currentimg = nekopics1; else if (currentimg = nekopics1) currentimg = nekopics0; repaint(); pause(150); Note that in that second line you increment the loop by ten pixels. Why ten pixels, and not, say, five or eight? The answer is determined mostly through trial and error

58、to see what looks right. Ten seems to work best for the animation. When you write your own animations, you have to play with both the distances and the sleep times until you get 大连交通大学 2016 届本科生毕业设计(论文)外文翻译 10 an animation you like. Speaking of repaint, lets cover the paint() method, which paints ea

59、ch frame. Here the paint method is trivially simple; all paint is responsible for is painting the current image at the current x and y positions. All that information is stored in global variables, so the paint method has only a single line in it: public void paint(Graphics g) g.drawImage(currentimg, xpos, ypos, this); Now lets back up to the run() method, where the main processing of this animation is happen

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论