昨天看CSDN论坛,看到一个可以送给女朋友做生日礼物的程序,今天将程序完善了一下,实现了从指定目录读取文件内的所有图片在屏幕上显示。废话不多说直接上效果图:
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowStateListener;
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import com.sun.awt.AWTUtilities;
/**
* 实现从指定目录读取.gif,.jpg,.png文件,并在屏幕上随机移动
* 可修改 num speed 控制图片的数量和下降速度
* 修改path变量指定图片文件夹的绝对路径或相对路径
*
* @author ZhaoYidong
*
*/
public class FlowerRain extends JFrame {
private static final long serialVersionUID = 1L;
private int num = 99;// 花朵数量
private int speed = 3;// 下降速度
private boolean flag = true;
// 定义集合存储文件下的多张图片
private List<JLabel> labelList = new ArrayList<JLabel>(num);
private Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
private ImageIcon[] icon;
private String[] images;
public FlowerRain() throws Exception {
String path = "H:/图标"; // 图片所在文件夹路径
images = this.getImages(path);
icon = new ImageIcon[images.length];
for (int i = 0; i < images.length; i++) {
/*
* 用于读取包中的当前目录下的文件 icon[i] = new
* ImageIcon(ImageIO.read(this.getClass()
* .getResourceAsStream(images[i])));
*/
icon[i] = new ImageIcon(path + "/" + images[i]);
}
getContentPane().setLayout(null);
setTitle("漫天花雨");
setSize(screenSize);
setResizable(false);
setUndecorated(true);
setAlwaysOnTop(true);
setLocationRelativeTo(null);
setIconImage(icon[0].getImage());
AWTUtilities.setWindowOpaque(this, false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 防止最小化
addWindowStateListener(new WindowStateListener() {
public void windowStateChanged(WindowEvent e) {
if (getState() == 1) {
setState(0);
}
}
});
// Ctrl + E 关闭窗口
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.isControlDown() && e.getKeyCode() == KeyEvent.VK_E) {
dispatchEvent(new WindowEvent(FlowerRain.this,
WindowEvent.WINDOW_CLOSING));
}
}
});
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
flag = false;// 将线程循环标志置为false
}
});
/**
* 将不同图标平均添加到指定数量的标签中
*/
byte id = 0;
for (int i = 0; i < num; i++) {
if (id >= images.length) {
id = 0;
}
JLabel jlbl = new JLabel(icon[id]);
// 调整标签大小为icon大小,icon大小为即为图片像素大小
jlbl.setSize(icon[id].getIconWidth(), icon[id].getIconHeight());
id++; // 下一张图片
// 标签jlbl的x,y坐标为随机值
jlbl.setLocation(random(screenSize.width),
random(screenSize.height));
labelList.add(jlbl); // 将图片标签加入集合
add(jlbl); // 将图片标签加入顶层容器JFame
}
}
// 遍历指定路径下的文件并使用FilenameFilter实现图片过滤
public String[] getImages(String pathname) {
File f = new File(pathname);
if (!f.isDirectory()) {
return null;
}
String[] images = f.list(new ImageFilter());
return images;
}
// 图片标签移动
public void move() {
new Thread() {
public void run() {
while (flag) {
try {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
for (int i = 0; i < labelList.size(); i++) {
JLabel jlbl = labelList.get(i);
Point location = jlbl.getLocation();
/**
* 控制jlbl的左右移动和向下移动的坐标 (i % 5 -
* 2)的取值为-2、-1、0、1、2 使各个标签水平移动的方向和速率各不相同
*/
jlbl.setLocation(location.x + (i % 5 - 2),
location.y + speed);
location = jlbl.getLocation();
if (location.y >= screenSize.height
|| location.x <= 0
|| location.x >= screenSize.width) {
// 标签jlbl移出屏幕后,从屏幕上方出现
jlbl.setLocation(
random(screenSize.width), 0);
}
}
}
});
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
}
};
}.start();
}
// 启动线程进行移动
public void start() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
setVisible(true);
move();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
// 产生随机值
public int random(int max) {
return (int) (Math.random() * max);
}
// 主函数
public static void main(String[] args) throws Exception {
new FlowerRain().start();
}
}
/**
* 使用FilenameFilter实现图片过滤,只要.gif,.jpg,.png文件
*/
class ImageFilter implements FilenameFilter {
public boolean isGif(String file) {
if (file.toLowerCase().endsWith(".gif")) {
return true;
} else {
return false;
}
}
public boolean isJpg(String file) {
if (file.toLowerCase().endsWith(".jpg")) {
return true;
} else {
return false;
}
}
public boolean isPng(String file) {
if (file.toLowerCase().endsWith(".png")) {
return true;
} else {
return false;
}
}
// 实现FilenameFilter接口中的accept方法
public boolean accept(File dir, String fname) {
return (isGif(fname) || isJpg(fname) || isPng(fname));
}
}
程序如下