2011-08-03 25 views
1



如何注入Java applet的getGraphics()方法?

首先我想說的是,這是我最喜歡的Java幫助站點。 (和其他語言)
現在我的問題是,我想截取一個Java小程序。
我已通過java.net.URLClassLoader加載小程序並將其添加到我的JFrame中。
我可以在我的JFrame中看到它,但我想將它渲染到截圖。
NORMAL小程序的解決方案在此處提及:Taking a "screenshot" of a java applet
但是,此Applet不使用paint或update方法,而是使用getGraphics()方法。
我已經注入了AppletStub,所以小程序認爲他在自己的網站上。
但是,我怎樣才能注入applet的getGraphics()方法來呈現它的截圖?

順便說一句,這是我的代碼:

@Override 
public void actionPerformed(final ActionEvent e) { 
    final String s = e.getActionCommand(); 
    if (s.equals("Screenshot")) { 
     final BufferedImage offScreen = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB); 
     Loader.getApplet().update(offScreen.createGraphics()); 
     try { 
      ImageIO.write(offScreen, "PNG", new File("C:/Users/Mitchell/Pictures/screenshot.png")); 
     } catch (Exception ex) { } 
    } 
} 

所有它創建一個黑色的800x600的PNG圖片(空的BufferedImage)。

回答

0

如果你能擺脫它,創造有問題的Applet的子類,並覆蓋getGraphics()這樣,它會回報你Graphics對象,然後用子類來代替原來的。

+0

所以你的意思是:public class GraphicsInjector extends Applet? – BlazeByte

+0

我的意思是'GraphicsInjector extends X',其中'X'是你正在嘗試使用的applet。 –

+0

我已經想出了,但Applet是一個變量...:P – BlazeByte