Write animation icon code in Java

Source: Internet
Author: User

Import java.awt.*;
Import java.awt.event.*;
Import javax.swing.*;

Animated icons

public class Animatoricon extends JPanel implements ActionListener {

imageicon[] images; An array of icons for animations
Timer Animationtimer;
int currentimage = 0; Current image number
int delay = 500; Image Switching Latency
int width; Image width
int height; Image Height

Public Animatoricon ()//constructors
{
SetBackground (Color.White);
Images = new Imageicon[2]; Initializing an array
for (int i=0;i<images.length;i++)
Images[i]=new ImageIcon (GetClass (). GetResource ("image" +i+ ". gif")); Instantiating icons
width = Images[0].geticonwidth (); Initialize Width value
Height = images[0].geticonheight (); Initialize Height value
}

public void Paintcomponent (Graphics g) {//Overloaded component rendering method
Super.paintcomponent (g); Calling the parent class function
Images[currentimage].painticon (this,g,70,0); Drawing icons
Currentimage= (currentimage+1)%2; Change the current image number
}

public void actionperformed (ActionEvent actionevent) {
Repaint ();
}

public void Startanimation () {//Start animation
if (animationtimer==null) {
currentimage=0;
Animationtimer=new Timer (delay, this); Instantiating a Timer object
Animationtimer.start (); Start running
else if (!animationtimer.isrunning ())//If not running
Animationtimer.restart (); Run again
}

public void StopAnimation () {
Animationtimer.stop (); Stop animation
}

public static void Main (String args[]) {
Animatoricon animation = new Animatoricon (); Instantiating an animated icon
JFrame frame = new JFrame ("animated icon"); Instantiating a Window object
Frame.getcontentpane (). Add (animation); Add Component to Window
Frame.setsize (200, 100); Set Window size
Frame.setvisible (TRUE); Set Window Visual
Frame.setdefaultcloseoperation (Jframe.exit_on_close); Exit Program when window closes
Animation.startanimation (); Start animation
}

}

Animated pictures. You write your own 1==n OK.

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.