38th section (java-for GIF animation effect)

Source: Internet
Author: User

Package Com.tanzhou.test;import Java.applet.applet;import java.awt.*; import Java.net.malformedurlexception;import Java.net.URL;/** * In Java there are two ways to implement multi-threading, one is to inherit the thread class, one is to implement the Runnable interface; * @author Administrator * * In the actual development can be determined to runnable interface as the main * 1, Draw interface 2, draw the boundary Face (show static time) 3, call thread Refresh interface * *1.applet is a small program that cannot be run alone but can be embedded in other applications 2. This class directly inherits from the Java.awt.Panel class, whose immediate subclass is Javax.swing.JApplet*/ Public classAnimation extends Applet implements Runnable {Image igs[]; //for loading JPG images that compose animated GIFsThread th; //the thread object is declared    intMax_hight = $, Max_width = $;//defines the size of the program interfaceimage Image;//declares a picture; is an abstract class that is a superclass of all classes that represent graphic imagesGraphics gh;//declares that a graphics object is an abstract base class for all graphics contexts, allowing applications to draw on components and imagesMediatracker MT;//declares a Mediatracker object; is a tool class that tracks the state of multiple media objects and currently supports only imagesBoolean flag=false;//its role is to flag whether the picture is loaded successfully    intID =0;//defines an image index     Public Static voidMain (string[] args) {NewAnimation (); }  /** public void init (): Called by the browser or applet viewer to notify the current applet that it has been loaded into the system and is often called before the first call to the Start method; Subclasses of the applet can override the method and perform some initialization operations in the method*/     Public voidinit () {IGs=Newimage[8];//instantiates an array of images with a length of 8MT =NewMediatracker ( This);//instantiating the media tracker MtImage = This. CreateImage (Max_width, max_hight);//creates a double-buffered image that can be drawn out of the screenGH = Image.getgraphics ();//instantiating a graphical object GHGh.setcolor (Color.White);//sets the current color of the GH graphic to whiteGh.fillrect (0,0, Max_width, max_hight);//specifies the position and size of the rectangle to draw         This. SetSize (Max_width, max_hight);//set the size of an applet         for(inti =0; i < igs.length; i++) {//The purpose of this loop is to get each JPG picture and put it into MtString file_name = (i +1) +". jpg"; Igs[i]= This. GetImage (GetCodeBase (), file_name);        Mt.addimage (Igs[i], i); }        Try{mt.waitforall ();//start loading all images tracked by this media tracker, that is, those JPG images}Catch(interruptedexception e) {e.printstacktrace (); } Flag=true;//change the identity to true, that is, loading is complete    }     Public voidPaint (Graphics g) {if(flag) {//Determine if loading is completeG.drawimage (Image,0,0, This);//draw a rectangle defined by the above        }    }    /** public void Start (): Called by the browser or applet viewer to notify the current applet that it should begin execution, often after the init () method call and in the Web page Called every time the applet is re-accessed*/     Public voidstart () {if(Mt.checkid (ID)) {//determine if the image at the ID location is finished loadingGh.drawimage (Igs[id],0,0, This);//Draw the image} th=NewThread ( This);//instantiation of thread thTh.start ();//Start Thread    }     Public voidrun () { while((Th! =NULL)) {            if(Mt.checkid (ID)) {//determine if the image at the ID location is finished loadingGh.fillrect (0,0, Max_width, max_hight);//specifies the position and size of the rectangle to drawGh.drawimage (Igs[id],0,0, This);//draw an image out of the ID locationid++;//ID plus 1                if(ID >= igs.length) {//If the image is finished drawingID =0;//ID Clear 0                }            }            Try{th.sleep ( Max);//thread hibernation 0.1 seconds}Catch(Interruptedexception e) {} This. repaint ();// Redraw        }    }}

38th section (java-for GIF animation effect)

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.