Login  Register

Re: Java3D stereo

Posted by babor on Mar 07, 2014; 2:34pm
URL: https://forum.jogamp.org/Java3D-stereo-tp4029914p4031795.html

gouessej wrote
Your source code is available on Github but I haven't found yet the classes that rely on Java3D. Several JOGL users have already switched from Java3D to Ardor3D or use them both, this is still a challenge but it's worth the change.
The most important class that uses Canvas3D is pl.edu.icm.visnow.geometries.viewer3d.Display3DPanel

gouessej wrote
Can't you use an existing method of SwingUtilities instead of getTopmostComponent?
I assume you mean SwingUtilities.getWindowAncestor? Sure I can :)
I'd also add some security for backward compatibility on Java 6.

As a result we have:

import java.awt.Container;
import java.awt.GraphicsConfiguration;
import java.awt.Window;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;
import java.lang.reflect.Method;
import javax.media.j3d.Canvas3D;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;

/**
 * @author  Bartosz Borucki (babor@icm.edu.pl)
 * University of Warsaw, Interdisciplinary Centre
 * for Mathematical and Computational Modelling
 */
public class WrappedCanvas3D extends Canvas3D {
    
    private GraphicsConfiguration initialGC;
    
    public WrappedCanvas3D(GraphicsConfiguration graphicsConfiguration) {
        super(graphicsConfiguration);
        initialGC = this.getGraphicsConfiguration();        
        this.addHierarchyListener(new HierarchyListener() {
            @Override
            public void hierarchyChanged(HierarchyEvent e) {
                if(e.getChangeFlags() == HierarchyEvent.PARENT_CHANGED) {
                    Container parent = e.getChangedParent();                    
                    try {                        
                        Class<?> c = java.awt.Component.class;
                        Method method = c.getDeclaredMethod("setGraphicsConfiguration", GraphicsConfiguration.class);
                        if(method == null)
                            return;
                        method.setAccessible(true);
                        if(parent == null)
                            return;
                        Window parentWindow = SwingUtilities.getWindowAncestor(parent);
                        if(parentWindow != null && initialGC != null)
                            method.invoke(parentWindow, initialGC);                                                
                    } catch (NoSuchMethodException ex) {
                        //in case of Java 6 ignore this exception
                    } catch (Exception ex) {
                        throw new RuntimeException(ex.getMessage());
                    }                    
                    if(parent instanceof JComponent)
                        ((JComponent)parent).revalidate();
                }                
            }
        });
    }
    
}