0
我已經在此之前成功完成,但現在運行程序無數次失敗後自帶的截圖是太快了。而且它不會把它把它作爲JFrame中的衰落正確的圖像。我該如何解決這個問題?如何從一個JFrame捕捉圖像?(過快)
編輯:所以基本上我試圖通過調用它拍攝的Excel宏的圖像捕捉JFrame的內部,但在這樣做的過程中Jpanels的圖像原來是一上面而不是我需要的圖像。
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
public class TimeTableGraphicsRunner extends JFrame
{
public TimeTableGraphicsRunner()
{
getContentPane().setLayout(new BoxLayout(getContentPane(),BoxLayout.Y_AXIS));
}
public void load(TimeTablePanel pan)
{
pan.setTitle(x.getTitle(TimeTable.titleCount));
getContentPane().add(pan);
}
public void run()throws Exception
{
System.out.println("Creating image in panel");
//setSize(TimeTable.width,TimeTable.height);
getContentPane().setPreferredSize(new java.awt.Dimension(TimeTable.width,TimeTable.height));
pack();
setVisible(true);
System.out.println("Image is in panel");
grabScreenShot();
System.out.println("Cleaning up");
setVisible(false);
System.out.println("Finished");
System.exit(0);
}
private void grabScreenShot() throws Exception
{
BufferedImage image = (BufferedImage)createImage(getContentPane().getSize().width,getContentPane().getSize().height);
getContentPane().paint(image.getGraphics());
try{
ImageIO.write(image, "png", new File("C:\\Users\\"+TimeTable.user+"\\AppData\\TimeLineMacroProgram\\TimeLine.png"));
System.out.println("Image was created");
}
catch (IOException e){
System.out.println("Had trouble writing the image.");
throw e;
}
}
}
吧?謹慎地闡述或提供一些代碼?順便說一句,截圖的用途是什麼? – mre 2011-06-13 17:09:15
如果截圖是太快了爲什麼不睡(1000),你正在服用的截圖與線程?此外,你看看'java.awt.Robot'? – 2011-06-13 17:10:38
['SwingUtilities.invokeAndWait(...)'](http://download.oracle.com/javase/6/docs/api/javax/swing/SwingUtilities.html#invokeAndWait%28java.lang.Runnable%29)可能會證明是有用的,但我們確實需要詳細闡述,否則我們都會在黑暗中被槍殺。 – mre 2011-06-13 17:11:41