首页
统计
留言板
直播
更多
壁纸
推荐
Git仓库
Search
1
IntelliJ IDEA激活 - 至2022年10月 (持续更新)
645 阅读
2
GitLab企业版搭建(附生成证书)
626 阅读
3
淘宝 京东秒杀脚本
422 阅读
4
Groovy模板引擎 API 构建动态脚本
324 阅读
5
欢迎使用 Typecho
294 阅读
杂货间
开发
Java
JavaScript
Android
JQuery
MySQL
PHP
Groovy
Git
运维
CentOS
Red Heat
Ubuntu
Debian
运行环境
登录
/
注册
Search
标签搜索
开发
Java
Android
MySQL8
CentOS
CentOS8
Linux
Git
Swing
JavaScript
JQuery
MySQL
临时手机号
IDEA
Steam
YouTube
订阅
激活码
GitLab
nginx
Dotdotmaples
累计撰写
30
篇文章
累计收到
7
条评论
首页
栏目
杂货间
开发
Java
JavaScript
Android
JQuery
MySQL
PHP
Groovy
Git
运维
CentOS
Red Heat
Ubuntu
Debian
运行环境
页面
统计
留言板
直播
壁纸
推荐
Git仓库
搜索到
1
篇与
Swing
的结果
2019-03-26
Swing实现动画效果(实现Loding动画)【转载】
申明:以下代码非本人原创,原创地址:原创链接本人只是在原作者博客的基础上进行理解、修改、添加注释思路说明:概览:利用Timer(计时器)更改变量X的值和重写paintComponent方法实现动画旋转效果详细:设定Timer的delay和定义一个ActionListener在paintComponent方法中绘制一个图形(圆)再更改画笔颜色去填充,填充时根据变量X设定填充开始位置(角度)在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); } }附上效果图
2019年03月26日
69 阅读
0 评论
0 点赞