• Java编程——动画图标


     1 // AnimatorIcon.java
     2 
     3 /*
     4 * To change this license header, choose License Headers in Project Properties.
     5 * To change this template file, choose Tools | Templates
     6 * and open the template in the editor.
     7 */
     8 package newpackage;
     9 
    10 import java.awt.*;
    11 import java.awt.event.*;
    12 import javax.swing.*;
    13 
    14 //动画图标
    15 
    16 public class AnimatorIcon extends JPanel implements ActionListener {
    17 
    18 ImageIcon[] images; //用于动画的图标数组
    19 Timer animationTimer; 
    20 int currentImage = 0; //当前图像编号
    21 int delay = 500; //图像切换延迟
    22 int width; //图像宽度
    23 int height; //图像高度
    24 
    25 public AnimatorIcon() //构造函数
    26 {
    27 setBackground(Color.white);
    28 images = new ImageIcon[2]; //初始化数组
    29 for (int i=0;i<images.length;i++)
    30 images[i]=new ImageIcon(getClass().getResource("image"+i+".gif")); //实例化图标
    31 width = images[0].getIconWidth(); //初始化宽度值
    32 height = images[0].getIconHeight(); //初始化高度值
    33 }
    34 
    35 public void paintComponent(Graphics g) { //重载组件绘制方法
    36 super.paintComponent(g); //调用父类函数
    37 images[currentImage].paintIcon(this,g,70,0); //绘制图标
    38 currentImage=(currentImage+1)%2; //更改当前图像编号
    39 }
    40 
    41 public void actionPerformed(ActionEvent actionEvent) {
    42 repaint();
    43 }
    44 
    45 public void startAnimation() { //开始动画
    46 if (animationTimer==null) {
    47 currentImage=0; 
    48 animationTimer=new Timer(delay, this); //实例化Timer对象
    49 animationTimer.start(); //开始运行
    50 } else if (!animationTimer.isRunning()) //如果没有运行
    51 animationTimer.restart(); //重新运行
    52 }
    53 
    54 public void stopAnimation() { 
    55 animationTimer.stop(); //停止动画
    56 }
    57 
    58 public static void main(String args[]) {
    59 AnimatorIcon animation = new AnimatorIcon(); //实例化动画图标
    60 JFrame frame = new JFrame("动画图标"); //实例化窗口对象
    61 frame.getContentPane().add(animation); //增加组件到窗口上
    62 frame.setSize(200, 100); //设置窗口尺寸
    63 frame.setVisible(true); //设置窗口可视
    64 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口时退出程序
    65 animation.startAnimation(); //开始动画
    66 }
    67 
    68 }
    AnimatorIcon.java
  • 相关阅读:
    Spring整合JMS(一)——基于ActiveMQ实现 (转)
    spring mvc入门配置
    Java enum的用法详解 (转)
    php学习日记7(关于“ javascript : ; ”的意思)
    php学习日记6(关于php中json_decode和json_encode的区别)
    php学习日记5(关于php中getCacheAll()方法的使用)
    php学习日记4(关于php中的$this->assign(); 与 $this->display();)
    php学习日记3(关于Mysql中limit用法)
    php学习日记2(关于php中的符号->、=>和::的意思)
    php学习日记1(关于Thinkphp框架中D的含义和用法)
  • 原文地址:https://www.cnblogs.com/liao-pxsoftware15/p/7719763.html
Copyright © 2020-2023  润新知