gpt4 book ai didi

java - 不使用 JFrame 打印屏幕

转载 作者:行者123 更新时间:2023-12-02 05:23:14 25 4
gpt4 key购买 nike

我想打印 JFrame 后面的内容,而不需要 JFrame 本身。我尝试的方法是关闭窗口,等待一段时间使其完全关闭,然后打印屏幕,然后再次打开它。当前的方法不适合我的需要,因为我需要更频繁地执行此操作,并且等待窗口关闭的时间太多。另外,我不想为此关闭窗口。

这是我当前的代码:

public class Offscreen {

private static BufferedImage img;

public static void main(String[] main)
throws AWTException, InterruptedException {
JFrame frame = new JFrame() {
@Override
public void paint(Graphics g) {
if (img != null)
// draw print screen
g.drawImage(img, 0, 0, null);
}
};
frame.setSize(500, 500);
frame.setLocation(700, 0);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

frame.setVisible(false);

Thread.sleep(200);

// take the picture
Robot r = new Robot();
img = r.createScreenCapture(frame.getBounds());

// show the source again
frame.setVisible(true);
// print on destiny
frame.repaint();
}
}

最佳答案

“假设”您只想捕获 Activity 框架后面的区域(正如您所说,没有框架),那么就像......

import java.awt.AWTException;
import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.image.BufferedImage;
import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class PrintScreen01 {

public static void main(String[] args) {
new PrintScreen01();
}

private Timer timer;

public PrintScreen01() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}

final PrintPane printPane = new PrintPane();

final JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(printPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.addComponentListener(new ComponentAdapter() {

@Override
public void componentMoved(ComponentEvent e) {
timer.restart();
}

@Override
public void componentResized(ComponentEvent e) {
timer.restart();
}

});
timer = new Timer(250, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (frame.isVisible()) {
takeSnapShot(frame, printPane);
}
}
});
timer.setRepeats(false);
frame.setVisible(true);
// takeSnapShot(frame, printPane);
}
});
}

public void takeSnapShot(JFrame frame, PrintPane pane) {

Rectangle bounds = new Rectangle(pane.getLocationOnScreen(), pane.getSize());
frame.setVisible(false);
new SnapShotWorker(frame, pane, bounds).execute();

}

public class SnapShotWorker extends SwingWorker<BufferedImage, BufferedImage> {

private JFrame frame;
private PrintPane pane;
private Rectangle captureBounds;

public SnapShotWorker(JFrame frame, PrintPane pane, Rectangle bounds) {
this.frame = frame;
this.pane = pane;
captureBounds = bounds;
}

@Override
protected BufferedImage doInBackground() throws Exception {

Thread.sleep(125);

BufferedImage snapShot = null;
try {
Robot bot = new Robot();
snapShot = bot.createScreenCapture(captureBounds);
} catch (AWTException ex) {
ex.printStackTrace();
}
Thread.sleep(125);
return snapShot;
}

@Override
protected void done() {
try {
BufferedImage snapShot = get();
pane.setSnapShot(snapShot);
frame.setVisible(true);
} catch (InterruptedException ex) {
ex.printStackTrace();
} catch (ExecutionException ex) {
ex.printStackTrace();
}
}



}

public class PrintPane extends JPanel {

private BufferedImage background;

public PrintPane() {
}

@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}

public void setSnapShot(BufferedImage img) {
background = img;
repaint();
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (background != null) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.setComposite(AlphaComposite.SrcOver.derive(0.5f));
g2d.drawImage(background, 0, 0, this);
g2d.dispose();
}
}

}

}

可能有帮助...

现在,我对 Robot 的计时有一些问题,似乎部分操作以某种方式线程化,这意味着除非您让帧的计时在不可见和不可见之间变化可见“正好”正确,您实际上会重新捕获帧...烦人...

为了尝试解决此问题,并减少 try catch 屏幕的次数,我使用了 javax.swing.Timer (以减少捕获尝试)和 SwingWorker 来控制实际的快照过程,将其从事件调度线程中取出,从而确保(在某种程度上)窗口不可见。

我还添加了一些额外的延迟,以确保帧离开屏幕的时间足够长,以防止它被快照捕获...<​​/p>

关于java - 不使用 JFrame 打印屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26319646/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com