我希望我的应用程序截取屏幕截图并显示一个打印对话框,以便用户可以打印屏幕截图。但是,屏幕截图太慢/对话框出现得太快,导致对话框出现在屏幕截图中(如下所示)。
BufferedImage image = new Robot().createScreenCapture(new Rectangle((int) frame.getLocationOnScreen().getX(), (int) frame.getLocationOnScreen().getY(), frame.getWidth(), frame.getHeight()));
...
if (printJob.printDialog()) {
try {
printJob.print();
} catch (Exception PrintException) {
PrintException.printStackTrace();
}
}
有没有办法在显示打印对话框之前指示屏幕截图何时完成捕获,而不是使用人工 Thread.sleep()?
编辑 :
好吧,我想我知道我做错了什么。真正发生的是在打印对话框显示并且用户单击确定之后,但在打印对话框动画消失之前捕获屏幕。这是由于以下代码:
// Print button action listener
public class printButtonPressed implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
PrinterJob printJob = PrinterJob.getPrinterJob ();
Book book = new Book ();
PageFormat documentPageFormat = new PageFormat ();
documentPageFormat.setOrientation(PageFormat.LANDSCAPE);
book.append(new Page(), documentPageFormat);
printJob.setPageable(book);
if (printJob.printDialog()) {
try {
printJob.print();
} catch (Exception PrintException) {
PrintException.printStackTrace();
}
}
}
}
// Create printable page
private class Page implements Printable {
public int print (Graphics g, PageFormat pageFormat, int page) {
JFrame frame = Main.frame;
try { // THIS SHOULD NOT BE HERE
screenShot = new Robot().createScreenCapture(new Rectangle((int) frame.getLocationOnScreen().getX(), (int) frame.getLocationOnScreen().getY(), frame.getWidth(), frame.getHeight());
} catch (AWTException e) {
e.printStackTrace();
}
Graphics2D g2d = (Graphics2D) g;
g2d.translate (pageFormat.getImageableX (), pageFormat.getImageableY ());
int pageWidth = (int) pageFormat.getImageableWidth();
int newImageWidth = pageWidth;
int newImageHeight = (int)((double)newImageWidth/screenShot.getWidth(null)*screenShot.getHeight(null));
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g2d.drawImage(screenShot, 0, 0, newImageWidth, newImageHeight, 0, 0, screenShot.getWidth(), screenShot.getHeight(), null);
return PAGE_EXISTS;
}
}
相反,如果 Robot().createScreenCapture 在任何 PrinterJob 内容之前被调用,那么屏幕将在打印对话框出现之前被捕获,从而解决了这个问题。
// Print button action listener
public class printButtonPressed implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
JFrame frame = Main.frame;
try { // MOVE THIS UP HERE
screenShot = new Robot().createScreenCapture(new Rectangle((int) frame.getLocationOnScreen().getX(), (int) frame.getLocationOnScreen().getY(), frame.getWidth(), frame.getHeight());
} catch (AWTException e) {
e.printStackTrace();
}
PrinterJob printJob = PrinterJob.getPrinterJob ();
Book book = new Book ();
PageFormat documentPageFormat = new PageFormat ();
documentPageFormat.setOrientation(PageFormat.LANDSCAPE);
book.append(new Page(), documentPageFormat);
printJob.setPageable(book);
if (printJob.printDialog()) {
try {
printJob.print();
} catch (Exception PrintException) {
PrintException.printStackTrace();
}
}
}
}
// Create printable page
private class Page implements Printable {
public int print (Graphics g, PageFormat pageFormat, int page) {
Graphics2D g2d = (Graphics2D) g;
g2d.translate (pageFormat.getImageableX (), pageFormat.getImageableY ());
int pageWidth = (int) pageFormat.getImageableWidth();
int newImageWidth = pageWidth;
int newImageHeight = (int)((double)newImageWidth/screenShot.getWidth(null)*screenShot.getHeight(null));
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g2d.drawImage(screenShot, 0, 0, newImageWidth, newImageHeight, 0, 0, screenShot.getWidth(), screenShot.getHeight(), null);
return PAGE_EXISTS;
}
}