实现图片的动态连续展示 JAVA

news2024/9/25 15:21:43

目录

  • 1、前言:
  • 2、图片的展示以及自动关闭:
  • 3、动画的连续展示:

1、前言:

要实现动画的流畅展示需要在能展示图片的基础上对图片进行关闭,再切换下一张图片,这要关闭窗口,与延时函数以及while函数的配合使用


2、图片的展示以及自动关闭:

图片的展示以及关闭代码如下:

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
         water w = new water("C:\\Users\\86153\\Pictures\\Saved Pictures\\微信图片_20220211134809.jpg");
         try {
			Robot r = new Robot();
			r.delay(3000);
		} catch (AWTException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
         w.dispatchEvent(new WindowEvent(w, WindowEvent.WINDOW_CLOSING));//关闭窗口
	}

}

class water extends JFrame{//打印图片类
	water(String PATH){
		 //图片大小设置
   	 this.setSize(1000, 1000);
   	 
   	 
   	 //手动关闭窗口程序自动结束
   	 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   	 
   	 
   	 //加载图片
   	 ImageIcon imageIcon = new ImageIcon(PATH);
   	 
   	 
   	 //创建的JLabel设置其图标为上方加载的图片
   	 JLabel label = new JLabel(imageIcon);
   	 
   	 //将label添加到窗口中
   	 this.getContentPane().add(label);
   	 
   	 
   	 //图片居中
   	 this.setLocationRelativeTo(null);
   	 
   	 
   	 //窗口显示
   	 this.setVisible(true);
   	
   	 
	}
}

Java中使用frame.dispose()时,该行代码被标记了横线,可能是因为该方法被标记为过时方法(deprecated method)所以取而代之使用 w.dispatchEvent(new WindowEvent(w, WindowEvent.WINDOW_CLOSING));方法

3、动画的连续展示:

在这里插入图片描述
以上述图为例,理论上从0出发可以到达任意的其他三个点且有多种路径,若上述0,1,2,3分别代表一张图的话,配合随机数的生成理论上可以产生不同变换且流畅的动画

将上述图转成矩阵有路径的坐标分别为(0,1)、(1,3)、(1,2)、(2,3)、(2,0)、(3,0)
变换成01矩阵入下:
[
0,1,0,0
0,0,1,1
1,0,0,1
1,0,0,0
]
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.WindowEvent;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Main {
	
	public static String path[] = {
			"C:\\Users\\86153\\Pictures\\Saved Pictures\\0.jpg",
			"C:\\Users\\86153\\Pictures\\Saved Pictures\\1.jpg",
			"C:\\Users\\86153\\Pictures\\Saved Pictures\\2.jpg",
			"C:\\Users\\86153\\Pictures\\Saved Pictures\\3.jpg"
			};

	public static void main(String[] args) {
		 int index = 0;//初始化变量
		    while(true) {//死循环
			 
			 int next = Math.abs(new Random().nextInt()) % 4;
			 while(a[index][next] != 1) next = Math.abs(new Random().nextInt()) % 4;
			 
			// System.out.println(index + " " + next);
			 
			 Water w = new Water(path[next]);
			 
			 index = next;
			 
			 try {
				new Robot().delay(500);
				//w.dispatchEvent(new WindowEvent(w, WindowEvent.WINDOW_CLOSING));//关闭窗口
			} catch (Exception e) { 
		    
			}
		} 
	}
	
	public static int a[][] = {
			{0, 1, 0, 0},
			{0, 0, 1, 1},
			{1, 0, 0, 1},
			{1, 0, 0, 0}
	};
}

class Water extends JFrame{//打印图片类
	Water(String PATH){
		 //图片大小设置
   	 this.setSize(2000, 2000);
   	 
   	 
   	 //手动关闭窗口程序自动结束
   	 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   	 
   	 
   	 //加载图片
   	 ImageIcon imageIcon = new ImageIcon(PATH);
   	 
   	 
   	 //创建的JLabel设置其图标为上方加载的图片
   	 JLabel label = new JLabel(imageIcon);
   	 
   	 //将label添加到窗口中
   	 this.getContentPane().add(label);
   	 
   	 
   	 //图片居中
   	 this.setLocationRelativeTo(null);
   	 
   	 
   	 //窗口显示
   	 this.setVisible(true);
   	
   	 
	}
}

能够切换但不够丝滑

优化代码:

import java.awt.FlowLayout;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Robot;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Main {
	
	public static String path[] = {
			"C:\\Users\\86153\\Pictures\\Saved Pictures\\0.jpg",
			"C:\\Users\\86153\\Pictures\\Saved Pictures\\1.jpg",
			"C:\\Users\\86153\\Pictures\\Saved Pictures\\2.jpg",
			"C:\\Users\\86153\\Pictures\\Saved Pictures\\3.jpg"
			};

	public static void main(String[] args) {
		 int index = 0;//初始化变量
		 Water w = new Water();
		    while(true) {//死循环
			 
			 int next = Math.abs(new Random().nextInt()) % 4;
			 while(a[index][next] != 1) next = Math.abs(new Random().nextInt()) % 4;
			 
			// System.out.println(index + " " + next);
			 
			 w.Wate(path[next]);
			 
			 index = next;
			 
			 
		} 
	}
	
	public static int a[][] = {
			{0, 1, 0, 0},
			{0, 0, 1, 1},
			{1, 0, 0, 1},
			{1, 0, 0, 0}
	};
}

class Water extends JFrame{//打印图片类
	 public 	JLabel label = new JLabel();
	 public void Wate(String PATH){ 
		 //图片框大小设置
   //	setSize(2000, 1000);
    // 设置JFrame为全屏
     GraphicsDevice graphicsDevice = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
     graphicsDevice.setFullScreenWindow(this);
   	 
   	 //手动关闭窗口程序自动结束
   	 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   	 
   	 
   	 //加载图片
   	 ImageIcon imageIcon = new ImageIcon(PATH);
   
   	 
   	 
   	 //创建的JLabel设置其图标为上方加载的图片
   	 label.setIcon(imageIcon);//更改图标
     setLayout(new FlowLayout(FlowLayout.CENTER));//调图像在框内位置
    
   	 //将label添加到框中
   	 add(label);
   	 
   	 try {
		  new Robot().delay(300);
		} catch (Exception e) { 
	    
		}
   	 
   	 
   	 //窗口显示
   	 setVisible(true);
   	
   	 
	}
}

全屏、且切换丝滑

代码:

import java.awt.FlowLayout;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Robot;
import java.util.List;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.util.*;

public class Main {
	
	public static String path[] = {
			"C:\\Users\\86153\\Pictures\\Saved Pictures\\0.jpg",
			"C:\\Users\\86153\\Pictures\\Saved Pictures\\1.jpg",
			"C:\\Users\\86153\\Pictures\\Saved Pictures\\2.jpg",
			"C:\\Users\\86153\\Pictures\\Saved Pictures\\3.jpg"
			};
   public static List<ImageIcon> PATH = new ArrayList<ImageIcon>();

	public static void main(String[] args) {
		 for(int i = 0; i < path.length; i ++) PATH.add(new ImageIcon(path[i]));
		 int index = 0;//初始化变量
		 Water w = new Water();
		    while(true) {//死循环
			 
			 int next = Math.abs(new Random().nextInt()) % 4;
			 while(a[index][next] != 1) next = Math.abs(new Random().nextInt()) % 4;
			 
			// System.out.println(index + " " + next);
			 
			 w.Wate(PATH.get(next));
			 
			 index = next;
			 
			 
		} 
	}
	
	public static int a[][] = {
			{0, 1, 0, 0},
			{0, 0, 1, 1},
			{1, 0, 0, 1},
			{1, 0, 0, 0}
	};
}

class Water extends JFrame{//打印图片类
	 public 	JLabel label = new JLabel();
	 public void Wate(ImageIcon PATH){ 
		 //图片框大小设置
   //	setSize(2000, 1000);
    // 设置JFrame为全屏
     GraphicsDevice graphicsDevice = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
     graphicsDevice.setFullScreenWindow(this);
   	 
   	 //手动关闭窗口程序自动结束
   	 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   	 
   	 
   	 //加载图片
   	 //ImageIcon imageIcon = new ImageIcon(PATH);
   
   	 
   	 
   	 //创建的JLabel设置其图标为上方加载的图片
   	 label.setIcon(PATH);//更改图标
     setLayout(new FlowLayout(FlowLayout.CENTER));//调图像在框内位置
    
   	 //将label添加到框中
   	 add(label);
   	 
   	 try {
		  new Robot().delay(300);
		} catch (Exception e) { 
	    
		}
   	 
   	 
   	 //窗口显示
   	 setVisible(true);
   	
   	 
	}
}

以空间换时间将图片解压提前放入List线性表中

代码:

import java.awt.FlowLayout;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Robot;
import java.util.List;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.util.*;

public class Main {
	
	public static String path[] = {
			"C:\\Users\\86153\\Pictures\\Saved Pictures\\14.jpg",
			"C:\\Users\\86153\\Pictures\\Saved Pictures\\15.jpg",
			"C:\\Users\\86153\\Pictures\\Saved Pictures\\16.jpg",
			"C:\\Users\\86153\\Pictures\\Saved Pictures\\17.jpg",
			"C:\\Users\\86153\\Pictures\\Saved Pictures\\18.jpg"
			};
   public static List<ImageIcon> PATH = new ArrayList<ImageIcon>();

	public static void main(String[] args) {
		 for(int i = 0; i < path.length; i ++) PATH.add(new ImageIcon(path[i]));
		 int index = 0;//初始化变量
		 Water w = new Water();
		    while(true) {//死循环
			 
			 int next = Math.abs(new Random().nextInt()) % 5;
			 while(a[index][next] != 1) next = Math.abs(new Random().nextInt()) % 5;
			 
			// System.out.println(index + " " + next);
			 
			 w.Wate(PATH.get(next));
			 
			 index = next;
			 
			 
		} 
	}
	
	public static int a[][] = {
			{0, 1, 1, 1, 1},
			{0, 0, 1, 0, 0},
			{0, 0, 0, 1, 0},
			{1, 0, 0, 0, 1},
			{1, 0, 0, 0, 0}
	};
}

class Water extends JFrame{//打印图片类
	 public 	JLabel label = new JLabel();
	 public void Wate(ImageIcon PATH){ 
		 //图片框大小设置
   //	setSize(2000, 1000);
    // 设置JFrame为全屏
     GraphicsDevice graphicsDevice = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
     graphicsDevice.setFullScreenWindow(this);
   	 
   	 //手动关闭窗口程序自动结束
   	 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   	 
   	 
   	 //加载图片
   	 //ImageIcon imageIcon = new ImageIcon(PATH);
   
   	 
   	 
   	 //创建的JLabel设置其图标为上方加载的图片
   	 label.setIcon(PATH);//更改图标
     setLayout(new FlowLayout(FlowLayout.CENTER));//调图像在框内位置
    
   	 //将label添加到框中
   	 add(label);
   	 
   	 try {
		  new Robot().delay(200);
		} catch (Exception e) { 
	    
		}
   	 
   	 
   	 //窗口显示
   	 setVisible(true);
   	
   	 
	}
}

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/907670.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

从SVG到Canvas:选择最适合你的Web图形技术

SVG 和 Canvas 都是可以在 Web 浏览器中绘制图形的技术。 众所周知&#xff0c; icon 通常使用 svg&#xff08;如 iconfont&#xff09;&#xff0c;而交互式游戏采用 Canvas。二者具体的区别是什么&#xff1f;该如何选择&#xff1f; 声明式还是命令式&#xff1f;绘制的图形…

C语言:库函数atoi及其模拟实现

i&#xff1a; atof是C语言标准库中的一个函数&#xff0c;用于将字符串转换为对应的浮点数/整形数。 函数接受一个参数str&#xff0c;该参数是一个指向以null结尾的字符串的指针。atof函数会尝试将这个字符串转换为一个浮点数&#xff0c;并返回转换后的结果。 要注意的是&a…

【3D激光SLAM】LOAM源代码解析--laserOdometry.cpp

系列文章目录 【3D激光SLAM】LOAM源代码解析–scanRegistration.cpp 【3D激光SLAM】LOAM源代码解析–laserOdometry.cpp 写在前面 本系列文章将对LOAM源代码进行讲解&#xff0c;在讲解过程中&#xff0c;涉及到论文中提到的部分&#xff0c;会结合论文以及我自己的理解进行解…

Numpy入门(4)— 保存和导入文件

NumPy保存和导入文件 4.1 文件读写 NumPy可以方便的进行文件读写&#xff0c;如下面这种格式的文本文件&#xff1a; # 使用np.fromfile从文本文件a.data读入数据 # 这里要设置参数sep &#xff0c;表示使用空白字符来分隔数据 # 空格或者回车都属于空白字符&#xff0c;读…

leetcode 542. 01 Matrix(01矩阵)

矩阵中只有0&#xff0c;1值&#xff0c;返回每个cell到最近的0的距离。 思路&#xff1a; 0元素到它自己的距离是0&#xff0c; 只需考虑1到最近的0是多少距离。 BFS. 先把元素1处的距离更新为无穷大。 0的位置装入queue。 从每个0出发&#xff0c;走上下左右4个方向&…

Redis的8种数据结构和应用场景介绍,面试题答案

面试原题&#xff1a;你用过Redis哪些数据结构&#xff1f;&#xff08;网易一面 2023&#xff09;(面试题来自牛客网) 参考答案 后面有 详细答案解析&#xff0c;帮助更快记忆~ 参考答案共652字符&#xff0c;阅读约需1分8秒&#xff1b;全文共8694字符&#xff0c;阅读约需…

vscode打开nvue,vue3语法文件爆红

由于之前一直是用的vue2&#xff0c;也没用过nvue文件&#xff0c;这次下了hbulider的vue3云模板练手&#xff0c;图快直接在vscode的设置里的关联语言加上了 "files.associations": {// 文件关联语言的优先级配置"*.vue": "vue","*.nvue&…

解决多模块开发中的问题(聚合继承)

&#x1f40c;个人主页&#xff1a; &#x1f40c; 叶落闲庭 &#x1f4a8;我的专栏&#xff1a;&#x1f4a8; c语言 数据结构 javaweb 石可破也&#xff0c;而不可夺坚&#xff1b;丹可磨也&#xff0c;而不可夺赤。 Maven 一、聚合1.1创建Maven模块&#xff0c;设置打包类型…

特斯拉Cybertruck卡车实物照流出,今年 9月可交付?配置报价待定

根据最新的消息&#xff0c;特斯拉的Cybertruck目前正在冰岛的一座冰川上进行路测&#xff0c;并且据称特斯拉的团队还在利用这辆车拍摄相关的广告海报。这进一步表明特斯拉已经进入了Cybertruck的最后测试阶段&#xff0c;并且准备在今年9月底开始交付。然而&#xff0c;有外媒…

MyBatis的基本入门及Idea搭建MyBatis坏境且如何一步骤实现增删改查(CRUD)---详细介绍

一&#xff0c;MaBatis是什么&#xff1f; 首先是一个开源的Java持久化框架&#xff0c;它可以帮助开发人员简化数据库访问的过程并提供了一种将SQL语句与Java代码进行解耦的方式&#xff0c;使得开发人员可以更加灵活地进行数据库操作。 1.1 Mabatis 受欢迎的点 MyBatis不仅是…

使用 OpenTelemetry 构建可观测性 04 - 收集器

在之前的博文中&#xff0c;我们讨论了如何使用 SDK 和链路追踪生产者来导出进程中的遥测数据。尽管有多种类型的导出器可供选择&#xff0c;但其中一个常见的目标是将数据导出到 OpenTelemetry Collector。本篇文章将深入探讨收集器以及如何使用它。 选 OTel Collector 还是…

激活函数总结(十五):振荡系列激活函数补充(SQU、NCU、DSU、SSU)

激活函数总结&#xff08;十五&#xff09;&#xff1a;激活函数补充 1 引言2 激活函数2.1 Shifted Quadratic Unit (SQU) 激活函数2.2 Non-Monotonic Cubic Unit (NCU) 激活函数2.3 Decaying Sine Unit (DSU) 激活函数2.4 Shifted Sinc Unit (SSU) 激活函数 3. 总结 1 引言 在…

豪越科技受邀出席2023中国算力大会

2023年8月17日-8月20日&#xff0c;“算汇银川 数创未来”创新中国行走进银川暨2023中国算力大会在银川中关村创新中心召开。政府领导、行业领袖、专家学者、以及大型科技企业负责人齐聚大会现场&#xff0c;围绕算力基础设施建设、创新应用和产业发展成果等方面开展广泛交流与…

NSF拨款3800万美元让更多机构参与量子科学与工程

近日&#xff0c;美国国家科学基金会&#xff08;National Science Foundation&#xff0c;NSF&#xff09;宣布对“量子信息科学与工程扩展能力”&#xff08;Expanding Capacity in Quantum Information Science and Engineering&#xff0c;ExpandQISE&#xff09;计划拨款3…

好消息,微信又有免费提现活动了

​明天就是一年一度的七夕佳节&#xff0c;微信推出了「浪漫七夕&#xff0c;情寄明灯」活动&#xff0c;凡参与活动都可获得免费提现券等奖励。 01 活动时间 8 月 21 日 10 点至 8 月 24 日 24 点。 02 如何参与 活动入口&#xff1a; 在「微信支付有优惠」小程序专属入口…

解读2023年上半年财报:继续押注儿童业务的361°,有着怎样的野心?

“足球热”的风还是吹到了青少年身边&#xff0c;近日&#xff0c;济南历城二中女足问鼎2023世界中学生足球锦标赛女子组冠军&#xff0c;中国球队时隔16年再次获得世界中学生足球锦标赛冠军&#xff0c;点燃了不少足球爱好者的热情。 少儿体育热之下&#xff0c;与之相关的运…

Php“牵手”淘宝商品销量数据采集方法,淘宝API接口申请指南

淘宝天猫商品销量接口 API 是开放平台提供的一种 API 接口&#xff0c;它可以帮助开发者获取商品的详细信息&#xff0c;包括商品的标题、描述、图片&#xff0c;月销量&#xff0c;总销量等信息。在电商平台的开发中&#xff0c;销量接口API是非常常用的 API&#xff0c;因此本…

20英镑以上免费吗?英国亚马逊上调当日达订单免配送费门槛!

据外媒报道&#xff0c;英国亚马逊向Prime会员发送了一封电子邮件&#xff0c;通知他们从下个月开始必须为小额当日达订单支付运费。 亚马逊在向Prime用户发送的电子邮件中称&#xff0c;目前&#xff0c;其向符合条件的邮政编码内的Prime会员提供免费当日送达服务。 不过&am…

机器学习,过拟合与欠拟合,正则化与交叉验证

目录 机器学习 过拟合与欠拟合 正则化与交叉验证 正则化 交叉验证 机器学习 的目的是使学到的模型不仅对已知数据而且对未知数据都能有很好的预测能力。 不同的机器学习方法会给出不同的模型。当损失函数给定时&#xff0c;基于损失函数的模型的训练误差&#xff08;tra…

Gear_Of_War靶机

靶场下载 https://www.vulnhub.com/entry/gears-of-war-ep1%2C382/ 一、信息收集 发现主机 nmap -sP 192.168.16.0/24获取开放端口等信息 nmap -sV -sC -A -p 1-65535 192.168.16.207扫描目录没有发现 二、445端口利用 工具一&#xff1a;smbma 扫描共享状态信息 smbm…