Swing实现动画效果(实现Loding动画)【转载】

申明:

以下代码非本人原创,原创地址:原创链接
本人只是在原作者博客的基础上进行理解、修改、添加注释

思路说明:

  1. 概览:

利用Timer(计时器)更改变量X的值和重写paintComponent方法实现动画旋转效果

  1. 详细:
  1. 设定Timer的delay和定义一个ActionListener
  2. 在paintComponent方法中绘制一个图形(圆)再更改画笔颜色去填充,填充时根据变量X设定填充开始位置(角度)
  3. 在ActionListener事件中不断修改变量X的值和调用重写的paintComponent方法

不多说,直接上代码

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class DoRun extends JPanel {
	private static final long serialVersionUID = 1L;
	
	private final int DELAY = 50;// 转动快慢设置
//	private final static Long time = (long) 5000;	//窗体关闭事件
	private static Timer timer;	//动画计时器
	private int x = 0;
	/**
	 * 调用
	 */
	public static void main(String[] args) {
		JFrame frame = new JFrame("正转");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//本类为Panel
		frame.add(new DoRun());
		frame.setSize(300, 300);
		frame.setLocation(400, 400);
		frame.setVisible(true);
		//窗体定时关闭
		/*try {
			Thread.sleep(time);
		} catch (InterruptedException e) {
		}
		// 停止 Timer,使它停止向其侦听器发送动作事件。
		timer.stop();
		frame.setVisible(false);
		frame.dispose();*/
		
	}
	
	/**
	 * 面板构造函数,初始化面板。包括Timer 的场景。
	 */
	public DoRun() {
		timer = new Timer(DELAY, new ReboundListener());
		timer.start();
	}

	/**
	 * 动画效果:不断的更新图像的位置,以达到动画的效果。
	 */
	private class ReboundListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			if (x < 360) {
				//控制每个DELAY周期旋转的角度,+ 为逆时针  - 为顺时针
				x = x - 5;
			} else {
				x = 0;
			}
			repaint();
		}
	}

	/**
	 * 绘出图像在面板中的位置
	 */
	public void paintComponent(Graphics page) {
		super.paintComponent(page);
		drawArc(page);
	}
	
	/**
	 * 画图形
	 */
	private void drawArc(Graphics g) {
		Graphics2D g2d = (Graphics2D) g.create();
		//抗锯齿 
		//JDK文档:http://tool.oschina.net/uploads/apidocs/jdk-zh/java/awt/RenderingHints.html
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		int width = getWidth();
		int height = getHeight();
		//设置画笔颜色
		g2d.setColor(Color.BLACK);
		g2d.drawArc(width / 2 - 110, height / 2 - 110, 10 + 200, 10 + 200, 0, 360);
		g2d.setColor(Color.RED);
		g2d.fillArc(width / 2 - 110, height / 2 - 110, 10 + 200, 10 + 200, x, 240);
		g2d.setColor(Color.BLACK);
		g2d.fillArc(width / 2 - 90, height / 2 - 90, 10 + 160, 10 + 160, 0, 360);
		g2d.dispose();
	}
}

附上运行效果图:

在这里插入图片描述

在原作者的基础上进行修改

注释就不写啦,只是修改fillArc的startAngle和arcAngle而已

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class LodingPanel extends JPanel {
	
	private static final long serialVersionUID = 1551571546L;
	
	private Timer timer;
	private int delay;
	private int startAngle;
	private int arcAngle = 0;
	private int orientation;
	
	public static final int CLOCKWISE = 0;
	public static final int ANTICLOCKWISE = 1;

	public LodingPanel() {
		this.delay = 50;
		this.orientation = CLOCKWISE;
		init();
	}
	
	public LodingPanel(int delay) {
		this.delay = delay;
		this.orientation = CLOCKWISE;
		init();
	}
	
	public LodingPanel(int delay, int orientation) {
		this.delay = delay;
		this.orientation = orientation;
		init();
	}
	
	@Override
	public void show() {
		this.timer.start();
	}
	
	/**
	 * @param orientation	set the direction of rotation
	 * 
	 * @beaninfo
	 *        enum: CLOCKWISE LodingPanel.CLOCKWISE
	 *        		ANTICLOCKWISE LodingPanel.ANTICLOCKWISE
	 */
	public void setOrientation(int orientation) {
		this.orientation = orientation;
	}
	
	private void init() {
		this.timer = new Timer(delay, new ReboundListener());
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		drawArc(g);
	}
	
	private void drawArc(Graphics g) {
		Graphics2D g2d = (Graphics2D) g.create();
		//抗锯齿 
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		int width = getWidth();
		int height = getHeight();
		//设置画笔颜色
		g2d.setColor(Color.WHITE);
		g2d.drawArc(width / 2 - 110, height / 2 - 110, 20 + 200, 20 + 200, 0, 360);
		g2d.setColor(Color.RED);
		g2d.fillArc(width / 2 - 110, height / 2 - 110, 20 + 200, 20 + 200, startAngle, arcAngle);
		g2d.setColor(Color.WHITE);
		g2d.fillArc(width / 2 - 105, height / 2 - 105, 20 + 190, 20 + 190, 0, 360);
		g2d.dispose();
	}
	
	private class ReboundListener implements ActionListener {

		private int o = 0;
		
		@Override
		public void actionPerformed(ActionEvent e) {
			if (startAngle < 360) {
				//控制每个DELAY周期旋转的角度,+ 为逆时针  - 为顺时针
				switch (orientation) {
				case CLOCKWISE:
					startAngle = startAngle + 5;
					break;
				case ANTICLOCKWISE:
					startAngle = startAngle - 5;
					break;
				default:
					startAngle = startAngle + 5;
					break;
				}
			} else {
				startAngle = 0;
			}
			
			if (o == 0) {
				if (arcAngle >= 355) {
					o = 1;
					orientation = ANTICLOCKWISE;
				}else {
					if (orientation == CLOCKWISE) {
						arcAngle += 5;
					}
				}
			}else {
				if (arcAngle <= 5) {
					o = 0;
					orientation = CLOCKWISE;
				}else {
					if (orientation == ANTICLOCKWISE) {
						arcAngle -= 5;
					}
				}
			}
			
			repaint();
		}
	}
	
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(800, 600);
		frame.setLocationRelativeTo(null);
		LodingPanel lodingPanel = new LodingPanel();
		lodingPanel.setBackground(Color.WHITE);
		lodingPanel.show();
		frame.add(lodingPanel);
		frame.setVisible(true);
	}
}

附上效果图

修改旋转方向和填充的arcAngle

发布了4 篇原创文章 · 获赞 3 · 访问量 974

猜你喜欢

转载自blog.csdn.net/qq_18878455/article/details/88831345