文章目录
- 一、效果
- 二、源码
- 1. pom依赖
- 2. 核心源码1
- 3. 核心源码2
一、效果
二、源码
1. pom依赖
<dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.9</version></dependency><!-- unirest --><dependency><groupId>com.konghq</groupId><artifactId>unirest-java</artifactId><version>3.14.1</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.10</version><scope>provided</scope></dependency>
2. 核心源码1
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;import org.apache.commons.lang3.RandomUtils;import kong.unirest.HttpResponse;
import kong.unirest.Unirest;
import lombok.extern.slf4j.Slf4j;/*** * 图片展示窗口* * @author 00fly* @version [版本号, 2023年3月3日]* @see [相关类/方法]* @since [产品/模块版本]*/
@Slf4j
public class ImageShowMove extends JDialog
{private static final long serialVersionUID = -8771307087530903406L;private static ImageShowMove dialog;private static Robot myRobot;public ImageShowMove(BufferedImage image){super();setTitle("图片");setSize(image.getWidth(), image.getHeight() + 30);Dimension screenSize = getToolkit().getScreenSize();Dimension dialogSize = getSize();dialogSize.height = Math.min(screenSize.height, dialogSize.height);dialogSize.width = Math.min(screenSize.width, dialogSize.width);setLocation(RandomUtils.nextInt(0, screenSize.width - dialogSize.height), RandomUtils.nextInt(0, screenSize.height - dialogSize.width));add(new JLabel(new ImageIcon(image)));setVisible(true);setResizable(false);setAlwaysOnTop(true);setDefaultCloseOperation(HIDE_ON_CLOSE); // 隐藏}/*** 打开图片* * @param resource*/private static void openImage(BufferedImage image){try{dialog = new ImageShowMove(image);myRobot = new Robot();}catch (AWTException e){log.error(e.getMessage(), e);}}/*** 位置移动,鼠标移动、点击*/private static void move(){Dimension screenSize = dialog.getToolkit().getScreenSize();Dimension dialogSize = dialog.getSize();dialogSize.height = Math.min(screenSize.height, dialogSize.height);dialogSize.width = Math.min(screenSize.width, dialogSize.width);dialog.setVisible(true); // 重新显示dialog.setLocation(RandomUtils.nextInt(0, screenSize.width - dialogSize.height), RandomUtils.nextInt(0, screenSize.height - dialogSize.width));// 移动鼠标myRobot.mouseMove(dialog.getX() + RandomUtils.nextInt(0, dialog.getWidth()), dialog.getY() + RandomUtils.nextInt(0, dialog.getHeight()));// 模拟按下鼠标左键后释放myRobot.mousePress(KeyEvent.BUTTON1_DOWN_MASK);myRobot.mouseRelease(KeyEvent.BUTTON1_DOWN_MASK);}public static void main(String[] args)throws IOException{HttpResponse<byte[]> response = Unirest.get("https://00fly.online/upload/2019/02/201902262129360274AKuFZcUfip.jpg").asBytes();BufferedImage image = ImageIO.read(new ByteArrayInputStream(response.getBody()));openImage(image);Executors.newScheduledThreadPool(2).scheduleAtFixedRate(() -> move(), 2, 10, TimeUnit.SECONDS);}
}
3. 核心源码2
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;import org.apache.commons.lang3.RandomUtils;import kong.unirest.HttpResponse;
import kong.unirest.Unirest;
import lombok.extern.slf4j.Slf4j;/*** * 图片展示窗口* * @author 00fly* @version [版本号, 2023年3月3日]* @see [相关类/方法]* @since [产品/模块版本]*/
@Slf4j
public class ImageShowDispose extends JDialog
{private static final long serialVersionUID = -7240357454480002551L;public ImageShowDispose(BufferedImage image){super();setTitle("图片");setSize(image.getWidth(), image.getHeight() + 30);Dimension screenSize = getToolkit().getScreenSize();Dimension dialogSize = getSize();dialogSize.height = Math.min(screenSize.height, dialogSize.height);dialogSize.width = Math.min(screenSize.width, dialogSize.width);setLocation(RandomUtils.nextInt(0, screenSize.width - dialogSize.height), RandomUtils.nextInt(0, screenSize.height - dialogSize.width));add(new JLabel(new ImageIcon(image)));setVisible(true);setResizable(false);setAlwaysOnTop(true);setDefaultCloseOperation(DISPOSE_ON_CLOSE);}/*** 打开图片后延时销毁* * @param image*/private static void openImage(BufferedImage image){try{ImageShowDispose dialog = new ImageShowDispose(image);Robot myRobot = new Robot();// 移动鼠标myRobot.mouseMove(dialog.getX() + RandomUtils.nextInt(0, dialog.getWidth()), dialog.getY() + RandomUtils.nextInt(0, dialog.getHeight()));// 模拟按下鼠标左键后释放myRobot.mousePress(KeyEvent.BUTTON1_DOWN_MASK);myRobot.mouseRelease(KeyEvent.BUTTON1_DOWN_MASK);TimeUnit.SECONDS.sleep(8);dialog.dispose();}catch (AWTException | InterruptedException e){log.error(e.getMessage(), e);}}public static void main(String[] args)throws IOException{HttpResponse<byte[]> response = Unirest.get("https://00fly.online/upload/2019/02/201902262129360274AKuFZcUfip.jpg").asBytes();BufferedImage image = ImageIO.read(new ByteArrayInputStream(response.getBody()));Executors.newScheduledThreadPool(2).scheduleAtFixedRate(() -> openImage(image), 2, 10, TimeUnit.SECONDS);}
}
有任何问题和建议,都可以向我提问讨论,大家一起进步,谢谢!
-over-