Hello everybody,
I was wondering… is it possible to get two different JMECanvas running in parallel embbeded in the same JFrame?? Let me explain my case. I have a class, called TDRenderer, which is implementing SimpleCanvasImpl. The code is:
public class TDRenderer extends SimpleCanvasImpl {
private static final Logger logger = Logger.getLogger(JMESwingTest.class
.getName());
private ControlTDRenderer controller;
public TDRenderer(ControlTDRenderer controller) {
super(300, 300);
this.controller = controller;
}
@Override
public void simpleSetup() {
// Normal Scene setup stuff…
Quaternion rotQuat = new Quaternion();
Vector3f axis = new Vector3f(1, 1, 0.5f);
axis.normalizeLocal();
Vector3f max = new Vector3f(5, 5, 5);
Vector3f min = new Vector3f(-5, -5, -5);
// ---- LIGHTS
/** Attach the light to a lightState and the lightState to rootNode. /
DirectionalLight light3 = new DirectionalLight();
light3.setAmbient(new ColorRGBA(0.5f, 0.5f, 0.5f, 0.5f));
light3.setDirection(new Vector3f(1, 0, 1).normalize());
light3.setEnabled(true);
LightState lightState = renderer.createLightState();
lightState.setEnabled(true);
lightState.attach(light3);
rootNode.setRenderState(lightState);
rootNode.updateRenderState();
controller.setRootNode(rootNode);
}
public ControlTDRenderer getController() {
return controller;
}
@Override
public void simpleUpdate() {
controller.runAllActions();
rootNode.updateRenderState();
}
Then there's a class, named TDRCanvas, which creates a TDRenderer object and a canvas to paint the TDRenderer object.
public class TDRCanvas extends Canvas {
private static final long serialVersionUID = 1L;
private static boolean initialized = false;
// JPanel contentPane;
// JPanel mainPanel = new JPanel();
private static Canvas comp = null;
private static JMECanvasImplementor impl;
private static ControlTDRenderer controller;
private static ArrayList<Canvas> allCanvas;
public static ControlTDRenderer getController() {
return controller;
}
/
- public static Canvas getCanvas() { if (comp == null) { init(); // comp =
- new TDRCanvas(WIDTH, HEIGHT); } return comp; }
*/
private static int WIDTH = 800;
private static int HEIGHT = 600;
private static void init() {
allCanvas = new ArrayList<Canvas>();
new Thread() { { setDaemon(true); }
@Override public void run() { while (true) {
for (Canvas canvas : allCanvas) { canvas.repaint(); }
yield(); } } }.start();
}
// Component initialization
public static Canvas createTDRCanvas() {
//
GL STUFF
// make the canvas:
comp = DisplaySystem.getDisplaySystem("lwjgl").createCanvas(WIDTH,
HEIGHT);
// add a listener... if window is resized, we can do something about
// it.
comp.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent ce) {
doResize();
}
});
if (!initialized) {
KeyInput.destroyIfInitalized();
KeyInput.setProvider(KeyInput.INPUT_AWT);
AWTMouseInput.destroyIfInitalized();
AWTMouseInput.setup(comp, false);
init();
initialized = true;
}
// AWTMouseInput.destroyIfInitalized();
// Important! Here is where we add the guts to the panel:
controller = new ControlTDRenderer();
impl = controller.getRenderer();
JMECanvas jmeCanvas = (JMECanvas) comp;
jmeCanvas.setImplementor(impl);
jmeCanvas.setUpdateInput(true);
//
END OF GL STUFF
comp.setBounds(0, 0, WIDTH, HEIGHT);
allCanvas.add(comp);
// controller = ((TDRenderer) impl).getController();
return comp;
}
protected static void doResize() {
impl.resizeCanvas(comp.getWidth(), comp.getHeight());
}
public static int getWIDTH() {
return WIDTH;
}
public static void setWIDTH(int width) {
WIDTH = width;
}
public static int getHEIGHT() {
return HEIGHT;
}
public static void setHEIGHT(int height) {
HEIGHT = height;
}
}
The case is I'm creating two different Canvas, putting them in two JPanels and everthing is oK, but It seems when doing doRender an exception is thrown:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.jme.scene.state.lwjgl.LWJGLZBufferState.enableDepthTest(Unknown Source)
at com.jme.scene.state.lwjgl.LWJGLZBufferState.apply(Unknown Source)
at com.jme.renderer.lwjgl.LWJGLRenderer.clearBuffers(Unknown Source)
at com.jmex.awt.SimpleCanvasImpl.doRender(Unknown Source)
at com.jmex.awt.lwjgl.LWJGLCanvas.paintGL(Unknown Source)
at org.lwjgl.opengl.AWTGLCanvas.paint(AWTGLCanvas.java:308)
at sun.awt.RepaintArea.paintComponent(Unknown Source)
at sun.awt.RepaintArea.paint(Unknown Source)
at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
While testing, I've tried to load the same model in both canvas and those where the results:
![](http://img444.imageshack.us/img444/8202/problemjmecanvas2bb4.th.png)
I was just wondering if what I'm trying to do is possible, or if I should leave the idea aside.
Thank you in advance!!!