Java实现动态切换ubuntu壁纸功能

1.在一个文件夹放好图片

2.读取文件夹的图片路径,放入数组

3.调用命令将图片逐个设置为壁纸

使用 Java 在 Ubuntu Linux 系统中实现自动切换壁纸的示例程序。这个程序使用了gnome-desktop-item-edit命令来设置壁纸,并通过定时任务来定期切换壁纸

java 复制代码
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Timer;
import java.util.TimerTask;

public class WallpaperChangerGUI extends JFrame {

    private Timer timer;
    private String[] imagePaths;
    private int currentImageIndex;
    private Point initialClick;
    public WallpaperChangerGUI() {
        setTitle("Wallpaper Changer");

        // 去掉标题栏
        setUndecorated(true);

        // 设置窗口半透明
        setOpacity(0.3f);

        setSize(300, 150);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setLayout(new FlowLayout());

        JButton startButton = new JButton("Start");
        JButton stopButton = new JButton("Stop");

        startButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                startChangingWallpaper();
            }
        });

        stopButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                stopChangingWallpaper();
            }
        });

        add(startButton);
        add(stopButton);

        JButton exitButton = new JButton("Exit");
        exitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        add(exitButton);

        // 添加鼠标拖动功能
        addMouseListener(new MouseAdapter() {


            @Override
            public void mousePressed(MouseEvent e) {
                initialClick = e.getPoint();
                //System.out.println("press");
            }

        });

        addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                if (initialClick!= null) {
                    Point currentPos = e.getLocationOnScreen();
                    setLocation(currentPos.x - initialClick.x, currentPos.y - initialClick.y);
                }
            }
        });

        // 假设你的图片路径数组
        imagePaths = new String[]{"/home/xxx/图片/壁纸/No.2358/0009.jpg",
                "/home/xxx/图片/壁纸/No.2358/0010.jpg",
                "/home/xxx/图片/壁纸/No.2358/0022.jpg"

            };
        currentImageIndex = 0;

    }

    public void startChangingWallpaper() {
        if (timer == null) {
            timer = new Timer();
            timer.scheduleAtFixedRate(new TimerTask() {
                @Override
                public void run() {
                    setWallpaper(imagePaths[currentImageIndex]);
                    currentImageIndex = (currentImageIndex + 1) % imagePaths.length;
                }
            }, 0, 5*1000); // 每一分钟切换一次壁纸,可以根据需要调整时间间隔
        }
    }

    public void stopChangingWallpaper() {
        if (timer!= null) {
            timer.cancel();
            timer = null;
        }
    }

    public static void setWallpaper(String imagePath) {
        try {
            // 使用 gnome-desktop-item-edit 命令设置壁纸
            Process process = Runtime.getRuntime().exec(new String[]{
                    "gsettings", "set", "org.gnome.desktop.background", "picture-uri", "file://" + imagePath
            });
            process.waitFor();
            if (process.exitValue() == 0) {
                System.out.println("Wallpaper set successfully to " + imagePath);
            } else {
                System.out.println("Failed to set wallpaper.");
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            WallpaperChangerGUI gui = new WallpaperChangerGUI();
            gui.setVisible(true);
        });
    }
}
相关推荐
听情歌落俗4 分钟前
MATLAB3-2数据存储-台大郭彦甫
开发语言·数学建模·matlab·矩阵
失散134 分钟前
分布式专题——10.5 ShardingSphere的CosID主键生成框架
java·分布式·架构·分库分表·shadingsphere
云天徽上27 分钟前
【数据可视化-112】使用PyEcharts绘制TreeMap(矩形树图)完全指南及电商销售数据TreeMap绘制实战
开发语言·python·信息可视化·数据分析·pyecharts
2501_926227941 小时前
UDP网络编程:【Java】无连接通信到Socket实战(二)
java·网络·udp
NG WING YIN1 小时前
Golang關於信件的
开发语言·深度学习·golang
Sunny_yiyi1 小时前
Java根据模版导出PDF文件
java·开发语言·pdf
麦兜*2 小时前
MongoDB 与 GraphQL 结合:现代 API 开发新范式
java·数据库·spring boot·mongodb·spring·maven·graphql
橘子132 小时前
C++实战:搜索引擎项目(二)
开发语言·c++·搜索引擎
shan&cen2 小时前
Day02 集合 | 30. 串联所有单词的子串、146. LRU 缓存、811. 子域名访问计数
java·数据结构·算法·缓存