SoFunction
Updated on 2025-03-03

Dynamic switching of ubuntu wallpaper function based on Java

1. Put pictures in a folder

2. Read the image path of the folder and put it into an array

3. Call the command to set the pictures one by one as wallpaper

Sample program to automatically switch wallpapers in Ubuntu Linux systems using Java. This program uses the gnome-desktop-item-edit command to set the wallpaper and regularly switch the wallpaper through timed tasks

import .*;
import .*;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

public class WallpaperChangerGUI extends JFrame {

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

        // Remove the title bar        setUndecorated(true);

        // Set the window to translucent        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");

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

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

        add(startButton);
        add(stopButton);

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

        add(exitButton);

        // Add mouse drag function        addMouseListener(new MouseAdapter() {


            @Override
            public void mousePressed(MouseEvent e) {
                initialClick = ();
                //("press");
            }

        });

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

        // Suppose your image path array        imagePaths = new String[]{"/home/xxx/picture/wallpaper/No.2358/",
                "/home/xxx/picture/wallpaper/No.2358/",
                "/home/xxx/picture/wallpaper/No.2358/"

            };
        currentImageIndex = 0;

    }

    public void startChangingWallpaper() {
        if (timer == null) {
            timer = new Timer();
            (new TimerTask() {
                @Override
                public void run() {
                    setWallpaper(imagePaths[currentImageIndex]);
                    currentImageIndex = (currentImageIndex + 1) % ;
                }
            }, 0, 5*1000); // Switch wallpapers every minute, and you can adjust the time interval according to your needs        }
    }

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

    public static void setWallpaper(String imagePath) {
        try {
            // Use the gnome-desktop-item-edit command to set the wallpaper            Process process = ().exec(new String[]{
                    "gsettings", "set", "", "picture-uri", "file://" + imagePath
            });
            ();
            if (() == 0) {
                ("Wallpaper set successfully to " + imagePath);
            } else {
                ("Failed to set wallpaper.");
            }
        } catch (IOException | InterruptedException e) {
            ();
        }
    }

    public static void main(String[] args) {
        (() -> {
            WallpaperChangerGUI gui = new WallpaperChangerGUI();
            (true);
        });
    }
}

This is the article about implementing the dynamic switching of ubuntu wallpaper function based on Java. For more related content on Java dynamic switching of ubuntu wallpaper, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!