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: 

  <table CellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6> 

  Import javax.swing.JApplet; import java.awt.Graphics; import java.awt.Image; import java.awt.MediaTracker; public class Animate extends JApplet (/ / photos private static final int NUM_OF_PIC = 4; int count; Image pics []; TimerThread timer; public void init () (count = 1; pics = new Image [NUM_OF_PIC]; MediaTracker tracker = new MediaTracker (this) for (int i = 0; i 

{

//将图片按照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;

}

}   </ Td> </ tr> </ table> 



  Animation control by a special thread TimerThread for processing. 

  <table CellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6> 

  Import java.awt.Component; public class TimerThread extends Thread (Component comp; int timediff; / / shouldRun statement for the volatile volatile boolean shouldRun; public TimerThread (Component comp, int timediff) (super ( "TimerThread (" + + timediff "millseconds "); this.comp = comp; this.timediff = timediff; shouldRun = true;) public void run () (while (shouldRun) (try (comp.repaint (); sleep (timediff);) catch (Exception e) ())))    </ Td> </ tr> </ table> 



  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. 

  <table CellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6> 

  MediaTracker tracker = new MediaTracker (this);    </ Td> </ tr> </ table> 



  2, will be carrying the Image Object joined MediaTracker 

  <table CellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6> 

  Pics [i] = getImage (getCodeBase (), new Integer (i). ToString ()+". jpg "); tracker.addImage (pics [i], 0);    </ Td> </ tr> </ table> 



  3, call the checkAll MediaTracker (), waiting for the loading process has ended. 

  <table CellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6> 

  Tracker.checkAll (true);    </ Td> </ tr> </ table> 



  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: 

  <table CellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6> 

  While (timer.isAlive ()) (try (Thread.sleep (50);) catch (InterruptedException e) ())    </ Td> </ tr> </ table> 



  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 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags:

Releated Java Articles

Comments

Leave a Reply