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

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);
   	
   	 
	}
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值