I am injecting a Applet that way that 开发者_如何学Pythonit can't check on which website it is.
The purpose is to load an external unmodified applet into mine so I can embed it into mine. final URL[] appletURL = { new URL(Main.getBase() + "/loader.jar") };
final URLClassLoader classLoader = new URLClassLoader(appletURL); // gets the class
final Class<?> loader = classLoader.loadClass("loader"); // loads the class
applet = (Applet) loader.newInstance(); // creates the applet
applet.setStub(new Injector()); // injects the getdocumentbase, so it thinks he is on his own website
applet.init(); // start applet
applet.start();
applet.setPreferredSize(new Dimension(800, 600)); // to be able to add to jframe properly
The "applet.setStub(new Injector());" part is to prevent navigating away or not loading because it's not the applet's own website.
But now I want to render the external applet to a screenshot. I tried this:@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);
applet.update(offScreen.createGraphics());
try {
ImageIO.write(offScreen, "PNG", new File("C:/Users/Mitchell/Pictures/screenshot.png"));
} catch (Exception ex) { }
}
}
But it doesn't use the paint nor updat method. It uses getGraphics() instead, I got tipped to extend the applet that I want to screenshot. But the applet is dynamically loaded, so how do I extend a class with a loaded class (or applet)?
Can someone provide a code sample? I am about to freak out because this is taking all my time and I can't continue without this.
Take a look at cglib.
If instead of a class you want to extend, you want to implement an interface, take a look at java.lang.reflect.Proxy
.
If you just add a field dynamically or otherwise, there won't be any code which uses it which is rather pointless,(except for access via reflection)
If you want a object with dynamic field names, perhaps a Map<String, Object>
is what you really want.
You can extends a class which has an additional fields any number of ways provided its not final.
You can do this at compile time, you can compile code at runtime, you can add a class from generated byte code at runtime.
If you know you need to extend a class, write the code to do that and you don't need to generate anything.
精彩评论