Add points while program in running

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Add points while program in running

click_24
This post was updated on .
Hello,

I have issues with my Java3D application.I load my class and XYZ axis show in the canvas.Then I want to load the method LoadPointClouds() while program is running which will show a circle of point clouds,but this doesn`t show up and I can`t figure it out why.

I get no exceptions and I added the TransformGroup from the method to the BranchGroup and I have the capabilities set.

The point clouds must show up when I click the jButton.Tryed different things but did not succeed.I can confirm the method LoadPointClouds() gets called when I click the button because I put a message to be printed in the console as confirmation that is calling it,but still nothing is showing up in the scene.

Please help me :|


Here is my code:

//------------------------------------------------------------------------------------------

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Others;

import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.behaviors.mouse.MouseTranslate;
import com.sun.j3d.utils.behaviors.mouse.MouseZoom;
import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.geometry.Text2D;
import com.sun.j3d.utils.universe.SimpleUniverse;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GraphicsConfiguration;
import static java.lang.Math.cos;
import static java.lang.Math.hypot;
import static java.lang.Math.sin;
import javax.media.j3d.Appearance;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.GeometryArray;
import javax.media.j3d.PointArray;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3f;

/**
 *
 * @author Me
 */
public class Java3D extends JPanel {

    public static Java3D instance;

    public static Java3D getInstance() {

        if (instance == null) {

            instance = new Java3D();

        }

        return instance;

    }

    BranchGroup branch;
    SimpleUniverse universe;
    BranchGroup b;

    public Java3D() {

        setLayout(new BorderLayout());

        GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();

        Canvas3D canvas = new Canvas3D(config);

        add("Center", canvas);

        universe = new SimpleUniverse(canvas);

        branch = new BranchGroup();
        branch.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
        branch.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);

        branch.addChild(setLights());
        branch.addChild(setAxis());

        universe.getViewingPlatform().setNominalViewingTransform();

        universe.addBranchGraph(branch);

    }
    Shape3D plShape;

    public TransformGroup LoadPointCloud() {

        Appearance app = new Appearance();

        Point3f[] plaPts = new Point3f[361];
        Color3f[] colPts = new Color3f[361]; //parallel to coordinates, colors.

        plaPts[0] = new Point3f(0.0f, 0.0f, 0.0f);
        colPts[0] = new Color3f(3.0f, 3.0f, 3.0f);

        float j = 0;
        for (int i = 0; i < 360; i++) {

            Point3f p3f1 = calc(0.2f, 0.0f, 0.2f, j++);
            plaPts[i + 1] = new Point3f(p3f1.x, p3f1.y, p3f1.z);
            colPts[i + 1] = new Color3f(0.8f, 1.0f, 5.0f);

        }

        PointArray pla = new PointArray(361, GeometryArray.COORDINATES | GeometryArray.COLOR_3);
        pla.setColors(0, colPts); //this is the color-array setting
        pla.setCoordinates(0, plaPts);

        plShape = new Shape3D(pla, app);

        TransformGroup transfPointCloud = new TransformGroup();
        transfPointCloud.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        transfPointCloud.addChild(plShape);

        MouseRotate mouseRotate = new MouseRotate();
        mouseRotate.setTransformGroup(transfPointCloud);
        mouseRotate.setSchedulingBounds(new BoundingSphere());
        transfPointCloud.addChild(mouseRotate);

        System.out.println("Works");

        return transfPointCloud;

    }

    public Point3f calc(float x, float y, float z, float deg) {

        float r = (float) hypot(x, y);//radical (x2+y2)

        float xx = (float) (r * sin(deg));
        float yy = (float) y;
        float zz = (float) (r * cos(deg));

        Point3f p3 = new Point3f(xx, yy, zz);

        return p3;

    }

    public TransformGroup setAxis() {

        TransformGroup tgg = new TransformGroup();

        float x;
        TransformGroup tgX = null;
        float y;
        TransformGroup tgY = null;
        float z;
        TransformGroup tgZ = null;

        for (x = -1.0f; x <= 1.0f; x = x + 0.01f) {

            Sphere sphere = new Sphere(0.01f);

            tgX = new TransformGroup();

            Transform3D transform = new Transform3D();

            Vector3f vector = new Vector3f(x, .0f, .0f);

            transform.setTranslation(vector);

            tgX.setTransform(transform);

            tgX.addChild(sphere);
            tgg.addChild(tgX);

        }

        Text2D textX = new Text2D("X", new Color3f(0.9f, 1.0f, 1.0f), "Helvetica", 24, Font.BOLD);
        Transform3D tX = new Transform3D();
        tX.set(2, new Vector3f(x - 1.0f, 0.0f, 0.0f));
        TransformGroup tgTemp = new TransformGroup(tX);
        tgTemp.addChild(textX);
        tgX.addChild(tgTemp);

        for (y = -1.0f; y <= 1.0f; y = y + 0.01f) {

            Sphere sphere = new Sphere(0.01f);

            tgY = new TransformGroup();

            Transform3D transform = new Transform3D();

            Vector3f vector = new Vector3f(.0f, y, .0f);

            transform.setTranslation(vector);

            tgY.setTransform(transform);

            tgY.addChild(sphere);
            tgg.addChild(tgY);

        }
        Text2D textY = new Text2D("Y", new Color3f(0.9f, 1.0f, 1.0f), "Helvetica", 24, Font.BOLD);
        Transform3D tY = new Transform3D();
        tY.set(2, new Vector3f(0.0f, y - 1.0f, 0.0f));
        TransformGroup tgTempY = new TransformGroup(tY);
        tgTempY.addChild(textY);
        tgY.addChild(tgTempY);

        for (z = -1.0f; z <= 1.0f; z = z + 0.01f) {

            Sphere sphere = new Sphere(0.01f);

            tgZ = new TransformGroup();

            Transform3D transform = new Transform3D();

            Vector3f vector = new Vector3f(.0f, .0f, z);

            transform.setTranslation(vector);

            tgZ.setTransform(transform);

            tgZ.addChild(sphere);
            tgg.addChild(tgZ);

        }

        Text2D textZ = new Text2D("Z", new Color3f(0.9f, 1.0f, 1.0f), "Helvetica", 24, Font.BOLD);
        Transform3D tZ = new Transform3D();
        tZ.set(2, new Vector3f(0.0f, 0.0f, z - 1.0f));
        TransformGroup tgTempZ = new TransformGroup(tZ);
        tgTempZ.addChild(textZ);
        tgZ.addChild(tgTempZ);

        tgg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

        MouseRotate mouseRotate = new MouseRotate();
        mouseRotate.setTransformGroup(tgg);
        mouseRotate.setSchedulingBounds(new BoundingSphere());
        tgg.addChild(mouseRotate);

        MouseZoom mouseDepth = new MouseZoom();
        mouseDepth.setTransformGroup(tgg);
        mouseDepth.setSchedulingBounds(new BoundingSphere());
        tgg.addChild(mouseDepth);

        MouseTranslate mouseTranslate = new MouseTranslate();
        mouseTranslate.setTransformGroup(tgg);
        mouseTranslate.setSchedulingBounds(new BoundingSphere());
        tgg.addChild(mouseTranslate);

        return tgg;

    }

    public DirectionalLight setLights() {

        Color3f light1Color = new Color3f(.1f, 1.4f, .1f);

        BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);

        Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);

        DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction);

        light1.setInfluencingBounds(bounds);

        return light1;

    }

    public static void main(String[] args) {

        JFrame frame = new JFrame();
        Java3D j = new Java3D();
        frame.add(new JScrollPane(j));
        frame.setSize(800, 800);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Button b = new Button();
        b.setVisible(true);
    }

}

and here is the method I use to call and set the pointclouds from  another JForm  class

//----------------------------------------------------------------------------------
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:

        Java3D j3d = Java3D.getInstance();
        BranchGroup b = new BranchGroup();
        b.addChild(j3d.LoadPointCloud());
        j3d.universe.addBranchGraph(b);
         
       

    }          
Reply | Threaded
Open this post in threaded view
|

Re: Add points while program in running

gouessej
Administrator
Hi

I have moved your post here as it concerns Java3D.

Maybe your points aren't in the frustum.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Add points while program in running

click_24


Well if I add the PointCloudMethod to the branch before running the program it shows up,but it does not show up if I put it on the branch while the program is running :|(button click to add it to the branchgroup)

Don`t know what to do :|

Please help me
Reply | Threaded
Open this post in threaded view
|

Re: Add points while program in running

hharrison
See:

http://www.java-tips.org/other-api-tips/java3d/how-to-use-the-capability.html

In your code, you are adding the pointcloud node directly to the universe at runtime....instead, add a node when building
the universe that has appropriate capabilities, then add you node to _that_ node at runtime.


Harvey
Reply | Threaded
Open this post in threaded view
|

Re: Add points while program in running

click_24
OMG!!!


It Works!!!

Thank you very much Harvey! I love you :D

BIG HUG :D