Reputation: 213
Say that I use a UrlClassLoader to load an applet from a website and attach it as a component to a frame. How can I gain control over the canvas so that I can draw to it myself? There is not much information about this as far as I can tell. Someone mentioned something about XBOOTING, but I have no idea what that is and I can't find anything about it.
Upvotes: 0
Views: 321
Reputation: 168835
The problem is that 'every applet is different'. That applet for instance, has no Canvas
in it, but instead draws directly to the applet surface. As soon as you have an instance of the applet, you might draw directly to that, but it will be overwritten the moment that the user selects a button.
import java.applet.Applet;
import java.awt.*;
import java.net.*;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
public class GrabThatCanvas {
GrabThatCanvas() {
try {
String string = "http://mainline.brynmawr.edu/Courses/cs110/spring2002/Applets/Smiley/";
URL[] urls = {
new URL(string)
};
URLClassLoader urlcl = new URLClassLoader(urls);
Class<?> clss = urlcl.loadClass("Smiley");
Object o = clss.newInstance();
Applet applet = (Applet)o;
applet.init();
applet.start();
applet.setPreferredSize(new Dimension(200,200));
Canvas canvas = findFirstCanvas(applet);
if (canvas!=null) {
System.out.println("We have the Canvas!");
} else {
System.out.println("No Canvas found!");
}
JOptionPane.showMessageDialog(null, applet);
} catch (Exception e) {
e.printStackTrace();
}
}
/* Very naive implementation that assumes the canvas is added
* directly to the applet. */
public Canvas findFirstCanvas(Container parent) {
Canvas canvas = null;
Component[] components = parent.getComponents();
for (Component c : components) {
System.out.println(c);
if (c instanceof Canvas) {
canvas = (Canvas)c;
break;
}
}
return canvas;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new GrabThatCanvas();
}
});
}
}
java.awt.Button[button0,0,0,0x0,invalid,label=Smile]
java.awt.Button[button1,0,0,0x0,invalid,label=Sad]
No Canvas found!
Of course, you might put the applet in a Swing GUI, account for the weird mix of Swing and AWT, and use the layered pane to draw 'over the top' of the entire applet. But that introduces new problems.
Upvotes: 2