How do you prepare a Shoubashoujiao Applet example of animation
Abstract: How do you prepare a Shoubashoujiao Applet example of animation
</ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width="665" border="0"> <tr> <td width = " 385 "> Applet is in the browser running small programs, Java Applet is from the start sweeping the world. Applet through the preparation of this, we can learn knowledge as follows:
1, JApplet Applet and the main interface
2, the image loading and the use of MediaTracker
3, and the use of multiple threads threaded direct communication
4, Thread.join () method of use
5, the use of volatile keyword </ td> <td width="270">
</ Td> </ tr> </ table>?
Animation is a major part of the Applet from codebase a group picture in the paper read, and then every one second rotation of a show. Code as follows:
{ //将图片按照0,1,…,NUM_OF_PIC -1, 放置在目录ä¸ï¼Œæ ¼å¼ä¸º.jpg pics[i] = getImage(getCodeBase(), new Integer(i).toString()+”.jpg”); tracker.addImage(pics[i], 0); } tracker.checkAll(true); } public void start() { timer = new TimerThread(this, 1000); timer.start(); } public void stop() { timer.shouldRun = false; try { timer.join(); //ç‰å¾…timer线程退出 } catch (InterruptedException e) { }; } public void paint(Graphics g) { g.drawImage(pics[count++], 0, 0, null); if(count == NUM_OF_PIC) count = 0; } }
Animation control by a special thread TimerThread for processing.
The use of MediaTracker
Applet obtained in an image file can be called the Applet getImage () method. But getImage method will be called immediately after the return, if at once using the Image getImage acquisition target, and then the Image object and not really loading or loading completed.
Therefore, we in the use of image files, the use of the MediaTracker java.awt package tracking an Image object loading, can guarantee that all pictures are finished loading. MediaTracker need to use the following three steps:
1, an instance of MediaTracker, attention to the picture to show the object as a parameter Component imported.
2, will be carrying the Image Object joined MediaTracker
3, call the checkAll MediaTracker (), waiting for the loading process has ended.
Thread.join () use
We stop in the Animate method join in the call timer () method will timer thread connection (join) to the current thread, the thread will wait for the timer will be consistent thread running after timer.join () method will return.
If the current thread waiting for the timer to return to the process of other thread was interrupted, then the current thread will be dished out InterruptedException. If we do not use the Thread join method can be implemented only through polling timer thread state judge:
Clearly this approach and use join method, cpu resources will be wasted, but also a waste of time for some, because the current thread from time to time to make enquiries timer thread whether it is still viable, may timer thread has already ended. However, the current thread or to wait for some time before we can to monitor it.
On the volatile
We know that, in Java value of the variable settings operation, in addition to long and double types of variables, are atomic operation, that is, the variable value of the simple read and write operations without the need for synchronization. This JVM 1.2, Java memory model of achieving always read from the main variable, is the need for special attention.
With JVM mature and optimization, and now in a multi-threaded environment the use of volatile keyword become very important. In the current Java memory model, the thread can be stored in local memory variables (such as machines register), rather than directly in the main deposit and write.
This may result in the main thread of a revision in the value of a variable, and the other threads also continue to use it in the register in the value of the variable copy, causing data inconsistencies.
To solve this problem, just like in the proceedings of the case, the variable declaration for the volatile (instability) can be, which instructed JVM, this variable is unstable, it has to use each of the main to read. Generally speaking, multi-tasking environment under the mandate of the signs should be shared and modified volatile.
Function TempSave (ElementID) (CommentsPersistDiv.setAttribute ( "CommentContent" document.getElementById (ElementID). Value); CommentsPersistDiv.save ( "CommentXMLStore");) function Restore (ElementID) (CommentsPersistDiv.load ( "CommentXMLStore"); document . getElementById (ElementID). CommentsPersistDiv.getAttribute value = ( "CommentContent");) </ td> </ tr> <tr>
↑ Back
Tags: applet






