Hello

I am student of BSCS 4th Semester.
we studied basics of java in OOP subject
i want to make a 3D Paint in java simple free hand diagrams
by using sphere..
drawing on different coordinates..

/**
 *
 * @author MuDi
 */
import com.sun.j3d.utils.geometry.*;

import com.sun.j3d.utils.universe.*;
import java.util.Scanner;

import javax.media.j3d.*;

import javax.vecmath.*;

public class Position {
boolean flag=true;
public Position() {
    boolean ch = true;
while(ch)
   {
   SimpleUniverse universe = new SimpleUniverse();

   BranchGroup group = new BranchGroup();

   // X axis made of spheres

   //for (float x = -1.0f; x <= 1.0f; x = x + 0.0005f)
   
   Color3f light1Color = new Color3f(.1f, 1.4f, .1f); // green light

   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);
   universe.getViewingPlatform().setNominalViewingTransform();
   group.addChild(light1);
   
      Sphere sphere = new Sphere(0.05f);
      TransformGroup tg = new TransformGroup();
      Scanner sc = new Scanner(System.in);
      float a = sc.nextFloat();
      float b = sc.nextFloat();
      float c = sc.nextFloat();
      int ac = sc.nextInt();
      if(ac==0)
      {
          ch=false;
      }
      Transform3D transform = new Transform3D();

      Vector3f vector = new Vector3f( a, b, c);

      transform.setTranslation(vector);
      
      tg.setTransform(transform);

      tg.addChild(sphere);
      
      group.removeAllChildren();
      group.addChild(tg);
      
      
   // add the group of objects to the Universe

      universe.addBranchGraph(group);
      //universe.getCanvas().invalidate();
      //universe.getCanvas().validate();

   }
   
   

}

public static void main(String[] args) {

   new Position();

}

}

I tried this
by reading values of coordinates..
it draw one sphere but when i enter 2nd.. it gives me error

please help me this

it will be usefull for me in some 3d project in future..

and if there is some other method of 3d drawing (free hand) which draw points on each coordinate given.. please let me know i want to learn this..

Thanks alot

Recommended Answers

All 6 Replies

it draw one sphere but when i enter 2nd.. it gives me error

It would help if you told us exactly what the error was.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package XML;

/**
 *
 * @author MuDi
 */
import com.sun.j3d.utils.geometry.*;

import com.sun.j3d.utils.universe.*;
import java.util.Scanner;

import javax.media.j3d.*;

import javax.vecmath.*;

public class Position {
boolean flag=true;
public Position() {
    boolean ch = true;
    SimpleUniverse universe = new SimpleUniverse();

   BranchGroup group = new BranchGroup();
   
   Color3f light1Color = new Color3f(.1f, 1.4f, .1f); // green light

   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);
   universe.getViewingPlatform().setNominalViewingTransform();
   group.addChild(light1);
   
while(ch)
   {
   
      Sphere sphere = new Sphere(0.05f);
      TransformGroup tg = new TransformGroup();
      Scanner sc = new Scanner(System.in);
      float a = sc.nextFloat();
      float b = sc.nextFloat();
      float c = sc.nextFloat();
      int ac = sc.nextInt();
      if(ac==0)
      {
          ch=false;
      }
      Transform3D transform = new Transform3D();

      Vector3f vector = new Vector3f( a, b, c);

      transform.setTranslation(vector);
      
      tg.setTransform(transform);

      tg.addChild(sphere);
      group.addChild(tg);
      
      
      universe.addBranchGraph(group);

   }
   
   

}

public static void main(String[] args) {

   new Position();

}

}

1st Code is wrong this one is correct.

I face this error when i input coordinates for second sphere

Exception in thread "main" javax.media.j3d.RestrictedAccessException: Group: only a BranchGroup node may be added
at javax.media.j3d.Group.addChild(Group.java:284)
at XML.Position.<init>(Position.java:67)
at XML.Position.main(Position.java:80)

Please i need your help

RestrictedAccessException: Group: only a BranchGroup node may be added

Have you read the API doc for the method you are trying to use? group.addChild(tg);
And for the Exception you are getting?
The message seems pretty specific.

Thanks ;)
i resolved my problem

can you please post the code after resolving the problem .
thanks in advance.

group.setCapability(BranchGroup.ALLOW_DETACH);
    group.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
    group.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
    group.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);  
      
     BranchGroup temp = new BranchGroup();
     Sphere sphere = new Sphere(0.02f);
     TransformGroup tg = new TransformGroup();
     Transform3D transform = new Transform3D();
      
      Vector3f vector = new Vector3f( a,b,0.5f);
      transform.setTranslation(vector);
      
      tg.setTransform(transform);

      tg.addChild(sphere);
      temp.addChild(tg);
      group.detach();
      group.addChild(temp);
      
      universe.addBranchGraph(group);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.