In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the "Java animation and automatic picture browsing program how to develop", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Java animation and automatic picture browsing program how to develop" it!
Film and television pictures can be understood as a kind of animation display, they generally display the image at the speed of 24 frames per second, using the pause effect of the human eye to produce a continuous action display process. Therefore, the essence of animation is moving graphics, as long as the speed of the animation (frame speed) is fast enough, the scattered static graphics will be merged into a continuous motion process.
The realization of Java animation is to use the drawImage () method of Graphics class in Java.awt package to draw an image on the screen, then define a thread to let the thread sleep for a period of time, and then switch to another image, so as to achieve the purpose of displaying animation.
1 related concepts, classes and methods
1.1 Thread
One of the important features of Java is its support for multithreaded programming. In the traditional program design, to perform multi-task is to monitor the execution of multiple tasks regularly by means of timer interrupt, and to use the way of task interrupt switching to rotate each task, so as to achieve the purpose of multi-task concurrency. On the one hand, this method consumes a lot of memory space and cpu resources, on the other hand, it will bring system security problems. The thread, as an execution flow of the program, can be regarded as a state of the computing process at a certain moment, it is smaller than the process and has a higher degree of concurrency, so it is also called light process. Thread is the basic unit of cpu scheduling, and process is the basic unit of resource allocation. The introduction of threads can improve the throughput of the system, improve the communication efficiency between tasks, and make effective use of system resources.
? Creation of threads
There are two ways to create a thread, one is to generate a Thread class object with new, and the other is to implement the Runnable interface. When you need to implement multiple inheritance and establish multithreading, you can only use the Runnable interface method. We use the first method here:
ThreadName=new Thread
? sleep (long)
Let the thread sleep for a certain period of time without action, the parameter unit is 1/1000 seconds.
? suspend ()
Suspends the thread and suspends its execution.
? resume ()
Wake up a suspended thread.
1.2 ToolKit class
It provides links between AWT and platform-related and independent classes, and provides several ways to access device-related features. Such as retrieving images from URL or local systems, obtaining display features of the screen, and so on. Define a ToolKit class in the program: ToolKit=getToolKit ()
1.3 MediaTracker classes
Provides the ability to manage the loading of image files. The function of the method StatusID (int id,boolean load) is to detect the state (ABORTED,COMPLETE,ERRORED,LOADING) of the loaded image file. When it is detected that the state is COMPLETE, the image loading is completed.
2 text animation
The realization procedure of text animation is relatively simple. First of all, create a new process with the new method, display the corresponding text on the screen with the drawString () method, and then let the process sleep for a certain period of time. After the time is up, change the text display position, and then redisplay the text. It includes a boundary judgment, the process of resetting coordinates, so to and fro to achieve text animation. After the program runs, it displays "Welcome to the Animation World!" in the center of the screen and moves to the left every 0.15 seconds until the leftmost end of the screen starts again. For specific procedures, see Program 1 (Applet Mini Program).
3 Image animation
The implementation method of image animation is similar to that of text, except that an Image array is defined and the corresponding image files are loaded into the array using the getImage () method. After the program runs, you can see a lovely elf throwing the ball. In addition, based on the consideration of program interaction, the response action of two events is added, one is WINDOW_DESTROY, that is, close the running window and return normally. The other is MOUSE_DOWN, when the left mouse button is pressed, pause the thread to make the picture still, press the left button again, resume pausing the thread, and continue to display the next picture.
If the elf image file in the program is replaced with a picture, and the delay time is increased, the picture can be automatically switched into an automatic picture browsing program. See Program 2 for the source program.
4 improvement of animation quality
After the last program was run, we found two problems. The first is that the program starts to display before the image is fully loaded, and the phenomenon of switching too fast occurs. The second is the picture flickering.
For the former problem, you can use the MediaTracker class in the Java.awt package to solve. When the StatusID () method returns a value of COMPLETE, it means that all the image files have been loaded before the screen is displayed.
The second problem is more complicated to solve. The reason for flickering is that in the paint () method, if the calculation is more complex and the time for calculating and drawing the picture exceeds the refresh period of the screen, the first part of the frame is drawn in one refresh cycle, and the rest is drawn in the next or even later period, thus creating a time interval between different parts of the frame, resulting in flicker. One solution is to overload the update () method. The default implementation of update () is to clear the original background and then call the paint () method. So by overloading update (), the entire background is cleared only if necessary.
The second solution is to use double buffering technology. Double buffering technology is to create a graphic object in the background graphics area, draw the image that needs to be drawn on it, and then transmit it to the paint () method, which is drawn in the foreground by the paint () method. At the same time, the background graphics buffer enters the preparation of the next image. Figure 1 is a schematic diagram of double buffering technology.
Thank you for your reading, the above is the "Java animation and automatic picture browsing program how to develop" the content, after the study of this article, I believe you on the Java animation and automatic picture browsing program how to develop this problem has a deeper understanding, the specific use of the need for practice to verify. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.