Login  Register

Re: Resize of a JCanvas3D

Posted by Andreas on Jan 17, 2013; 8:15am
URL: https://forum.jogamp.org/Resize-of-a-JCanvas3D-tp4027842p4027888.html

I wrote a small example. If I run the following code as a Netbeans Platform Module a new Netbeans instance opens with the standard start TopComponent. When I open the test TopComponent anything works including resizing with two TopComponents open. Then I switch to the start TopComponent tab and resize the Netbeans window. If I switch back to the test TopComponent tab the Java3D part is still in the size before switching to the other TopComponent.

import com.sun.j3d.exp.swing.JCanvas3D;
import com.sun.j3d.utils.universe.SimpleUniverse;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import javax.media.j3d.Alpha;
import javax.media.j3d.AmbientLight;
import javax.media.j3d.Appearance;
import javax.media.j3d.Background;
import javax.media.j3d.BoundingBox;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.Font3D;
import javax.media.j3d.FontExtrusion;
import javax.media.j3d.Material;
import javax.media.j3d.PointLight;
import javax.media.j3d.RotationInterpolator;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Text3D;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3d;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.util.NbBundle.Messages;
import org.openide.windows.TopComponent;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(
    dtd = "-//de.test//Test//EN",
autostore = false)
@TopComponent.Description(
    preferredID = "TestTopComponent",
//iconBase="SET/PATH/TO/ICON/HERE",
persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(mode = "editor", openAtStartup = false)
@ActionID(category = "Window", id = "de.test.TestTopComponent")
@ActionReference(path = "Menu/Window" /*, position = 333 */)
@TopComponent.OpenActionRegistration(
    displayName = "#CTL_TestAction",
preferredID = "TestTopComponent")
@Messages({
    "CTL_TestAction=Test",
    "CTL_TestTopComponent=Test Window",
    "HINT_TestTopComponent=This is a Test window"
})
public final class TestTopComponent extends TopComponent {

    public TestTopComponent() {
        initComponents();
        setName(Bundle.CTL_TestTopComponent());
        setToolTipText(Bundle.HINT_TestTopComponent());
        init();
    }
   
    public void init(){
       
        JCanvas3D jCanvas = new JCanvas3D();
        jCanvas.setResizeMode(JCanvas3D.RESIZE_IMMEDIATELY);
        jCanvas.setSize(new Dimension(100, 100));
        add(jCanvas, BorderLayout.CENTER);  

        Canvas3D canvas3D = jCanvas.getOffscreenCanvas3D();
        SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
        simpleU.getViewingPlatform().setNominalViewingTransform();
       
        BranchGroup bg = createSceneGraph();
        bg.compile();
        simpleU.addBranchGraph(bg);
    }

    private BranchGroup createSceneGraph() {
        BranchGroup root = new BranchGroup();
        TransformGroup spin = new TransformGroup();
        spin.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        root.addChild(spin);
        Appearance ap = new Appearance();
        ap.setMaterial(new Material());
        FontExtrusion fontExtrusion = new FontExtrusion();
        double tessellationTolerance = 1e-2;
        Text3D text3d = new Text3D(
                new Font3D(
                new Font("Helvetica", Font.PLAIN, 48),
                tessellationTolerance,
                fontExtrusion),
                "01234567890 TEXT 3D");
        Shape3D text2 = new Shape3D(text3d);
        text2.setAppearance(ap);
        BoundingBox boundingBox = new BoundingBox();
        text3d.getBoundingBox(boundingBox);
        System.out.printf("boundingBox: %s\n", boundingBox);
        Point3d p1 = new Point3d();
        boundingBox.getUpper(p1);
        System.out.printf("upper: %s\n", p1);
        TransformGroup textTrans = new TransformGroup();
        Transform3D t3d = new Transform3D();
        t3d.setTranslation(new Vector3d(-(p1.x / 2), -(p1.y / 2), -(p1.z / 2)));
        textTrans.setTransform(t3d);
        textTrans.addChild(text2);
        Transform3D tr = new Transform3D();
        tr.setScale(0.05);
        TransformGroup tg = new TransformGroup(tr);
        spin.addChild(tg);
        tg.addChild(textTrans);
        Alpha alpha = new Alpha(-1, 1000000000);
        RotationInterpolator rotator = new RotationInterpolator(alpha, spin);
        BoundingSphere bounds = new BoundingSphere();
        rotator.setSchedulingBounds(bounds);
        spin.addChild(rotator);
        Background background = new Background(0.0f, 0.0f, 0.0f);
        background.setApplicationBounds(bounds);
        root.addChild(background);
        AmbientLight light = new AmbientLight(true, new Color3f(Color.white));
        light.setInfluencingBounds(bounds);
        root.addChild(light);
        PointLight ptlight = new PointLight(new Color3f(Color.white), new Point3f(3f, 3f, 3f), new Point3f(1f, 0f, 0f));
        ptlight.setInfluencingBounds(bounds);
        root.addChild(ptlight);
        PointLight ptlight2 = new PointLight(new Color3f(Color.white),
                new Point3f(-2f, 2f, 2f), new Point3f(1f, 0f, 0f));
        ptlight2.setInfluencingBounds(bounds);
        root.addChild(ptlight2);
        return root;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        setLayout(new java.awt.BorderLayout());
    }// </editor-fold>

    // Variables declaration - do not modify
    // End of variables declaration
    @Override
    public void componentOpened() {
        // TODO add custom code on component opening
    }

    @Override
    public void componentClosed() {
        // TODO add custom code on component closing
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    void readProperties(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }
}