Multi-threaded Animation

Source: Internet
Author: User
Use multiple threads to create an animation-Linux general technology-Linux programming and kernel information. The following is a detailed description. Import java. awt .*;
Import java. applet .*;

// Have you noticed that this program is different from the Hello program? Implements Runnable is added in this program.
Public class carton extends Applet implements Runnable
{
Image img;
Thread thd = null;
Int I;
Int imgWidth = 150;
Int imgHeight = 150;
Int ncyc = 1;

String namestr [] = new String [5];

// Run the run () function when the thread is activated.
Public void run ()
{
For (int j = 0; j <5; j ++)
{
Namestr [j] = Integer. toString (j, 8) + ". jpg ";
}

Ncyc =-1;
While (true)
{
If (ncyc <= 3) ncyc = ncyc + 1; // initialize the Loop Control Parameter
Else ncyc = 0;
Img = getImage (getCodeBase (), namestr [ncyc]);

If (img! = Null)
{
I = imgHeight;
// Repaint ();
}

Try {Thread. sleep (1000);} catch (InterruptedException e ){}
I = 0;
While (I {
Repaint ();
Try {Thread. sleep (50);} catch (InterruptedException e ){}
I + = 4;
}
}
}

// Each time the Code re-draws a bitmap at a new position, it must call repaint. This function calls the reloaded update method. The update method is the same as the paint method. Why not worry about the painting (Graphics g? Except that the paint method clears the window before drawing, the update method does not clear (if you change the update method to paint, you will see what is different ).
Public void update (Graphics g)
{
If (img! = Null)
{
G. clipRect (0, 0, imgWidth, I );
G. drawImage (img, 0, I-imgHeight, null );
}
}

Public void start ()
{
If (thd = null)
{
Thd = new Thread (this );
Thd. start ();
}
}

Public void stop ()
{
Thd = null;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.