Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: Java 3D Problems - Canvas 3d and QuadArray rendering

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    10
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Java 3D Problems - Canvas 3d and QuadArray rendering

    Hi, I am currently having two problems with my code. The first problem is:

    I have a SimpleUniverse. I wanted to add a Canvas3D in order to accept a KeyAdapter. When I did this, the SimpleUniverse is not displayed; there is no frame.

    I am not sure exactly what the problem is; I could not find much information with the problem on the web.

    My Second problem is:

    I have two Nodes that I am adding to my SimpleUniverse: A QuadArray and a PointArray. The PointArray Works Great. The QuadArray is not rendered correctly though. Whenever it is rotated, It is obvious that only parts are being rendered.

    The idea for the program is to make a rubiks cube. I have not gotten to far before reaching the problem, so I do realize that there are things that will not work in the future such as the fact that each face is a single Quadralateral.


    Oh, and a Bonus, if you can figure out the for loop on RubiksCube so that the Stickers generate automatically, that would be great xD. Completely unneccesary as I can do it myself.

    My Code is as following. The Driver Class is:
     
       import java.awt.Color;
       import com.sun.j3d.utils.geometry.GeometryInfo;
       import com.sun.j3d.utils.geometry.NormalGenerator;
       import com.sun.j3d.utils.universe.SimpleUniverse;
       import javax.media.j3d.*;
       import javax.vecmath.*;
       import java.io.*;
       import java.awt.event.*;
    // An Egyptian pyramid
    // Base divided into two triangles
       public class PyramidExample {
     
          public static PrintStream a = System.out;
          public static Canvas3D canvas = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
     
          public static Vector3f viewTranslation = new Vector3f();
          public static  Transform3D viewTransform = new Transform3D();
     
          public static Transform3D rotation = new Transform3D();
          public static	SimpleUniverse universe = null;
     
          public static void main(String[] args) throws Exception{
             canvas.addKeyListener(new Rotator());
     
         universe = new SimpleUniverse(canvas); // Doesnt Work; No frame display4ed
    		//universe = new SimpleUniverse(); // Does Work; No canvas (No keyAdapter)
    		canvas.preRender();
    	 universe.getViewingPlatform().setNominalViewingTransform();
     
             RubiksCube joe = new RubiksCube(.2, .01, null);	
     
             viewTranslation.z = 3;
             viewTranslation.x = 0f;
             viewTranslation.y = .3f;
     
     
             initializeChild(universe, joe.getCubies() );
             a.println("Added");
             initializeChild(universe, joe.cubiesP);
     
     
     
             rotateX(universe, 10, 1);
             Thread.sleep(1000);
             rotateY(universe, 10, -1);
             Thread.sleep(1000);
             rotateZ(universe, 10, 1);
             Thread.sleep(1000);
             rotateZ(universe, 10, -1);
             Thread.sleep(1000);
             rotateX(universe, 10, 1);
     
     
          }
          public static void initializeChild(SimpleUniverse universe, GeometryArray results)
          {
             BranchGroup group = new BranchGroup();
             GeometryArray result = results;
             if(!(results instanceof PointArray))
             {
     
                IndexedQuadArray indexedCube = (IndexedQuadArray)results;
     
                Vector3f[] normals = { new Vector3f(0.0f, 0.0f, 1.0f),
                      new Vector3f(0.0f, 0.0f, -1.0f),
                      new Vector3f(1.0f, 0.0f, 0.0f),
                      new Vector3f(-1.0f, 0.0f, 0.0f),
                      new Vector3f(0.0f, 1.0f, 0.0f), new Vector3f(0.0f, -1.0f, 0.0f) };
             //Define the indices used to reference vertex array
                int coordIndices[] = { 0, 1, 2, 3, 7, 6, 5, 4, 0, 3, 7, 4, 5, 6, 2, 1,
                      0, 4, 5, 1, 6, 7, 3, 2 };
             //Define the indices used to reference normal array
                int normalIndices[] = { 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3,
                      4, 4, 4, 4, 5, 5, 5, 5};
             //Set the data
                indexedCube.setNormals(0, normals);
                indexedCube.setCoordinateIndices(0, coordIndices);
                indexedCube.setNormalIndices(0, normalIndices);
             //Define an appearance for the shape
     
             //Create and return the shape
                group.addChild(new Shape3D(indexedCube));
                universe.addBranchGraph(group);
     
     
             }
             else
             {
                Appearance pointApp = new Appearance();
     
             // enlarge the points
                pointApp.setPointAttributes(new PointAttributes(50, true));
                Shape3D pointShape = new Shape3D(results, pointApp);
                group.addChild(pointShape);
                universe.addBranchGraph(group);
             }
     
          }
          public static void rotateX(SimpleUniverse universe, int speed, int prime) throws Exception // prime is either negative or positive 1
          {
             for(int i = 0;i<(Math.PI*50);i++)
             {
                rotation.rotX(((double)i)/100*prime);
                rotation.mul(viewTransform);
                u();
                v(universe);
                Thread.sleep(speed);
             }		
          }
          public static void rotateY(SimpleUniverse universe, int speed, int prime) throws Exception // prime is either negative or positive 1
          {
             for(int i = 0;i<(Math.PI*50);i++)
             {
                rotation.rotY(((double)i)/100*prime);
                rotation.mul(viewTransform);
                u();
                v(universe);
                Thread.sleep(speed);
             }		
          }
          public static void rotateZ(SimpleUniverse universe, int speed, int prime) throws Exception // prime is either negative or positive 1
          {
             for(int i = 0;i<(Math.PI*50);i++)
             {
                rotation.rotZ(((double)i)/100*prime);
                rotation.mul(viewTransform);
                u();
                v(universe);
                Thread.sleep(speed);
             }		
          }
          public static void v(SimpleUniverse universe)
          {
             universe.getViewingPlatform().getViewPlatformTransform().setTransform(rotation);
          }
          public static void u()
          {
             viewTransform.setTranslation(viewTranslation);
          }
          private static class Rotator extends KeyAdapter
          {
             public void KeyPressed(KeyEvent e)
             {
                try{
                   switch(e.getKeyCode())
                   {
                      case(KeyEvent.VK_LEFT):
                         rotateZ(universe, 10, -1);
                         break;
                      case(KeyEvent.VK_RIGHT):
                         rotateZ(universe, 10, 1);
                         break;
                      case(KeyEvent.VK_UP):
                         rotateY(universe, 10, 1);
                         break;
                      case(KeyEvent.VK_DOWN):
                         rotateY(universe, 10, -1);
                         break;
                   }
                }
                   catch(Exception WTF){}
     
             }
          }
       }


    My RubiksCube class is

     import java.awt.Color;
    import com.sun.j3d.utils.geometry.GeometryInfo;
    import com.sun.j3d.utils.geometry.NormalGenerator;
    import com.sun.j3d.utils.universe.SimpleUniverse;
    import javax.media.j3d.*;
    import javax.vecmath.*;  
    	public class RubiksCube
       {
    	int place = 0;
          public IndexedQuadArray cubies = new IndexedQuadArray(24,GeometryArray.COORDINATES | GeometryArray.COLOR_3|IndexedQuadArray.NORMALS,24);
    		public PointArray cubiesP = new PointArray(216, PointArray.COORDINATES);
    		public Point3f[][][] getPoints(float width, float distance)
    		{
    		Point3f[][][] points = new Point3f[6][6][6];
    		int place = 0;
    		for(int right = 0;right<6;right++)
    		for(int up = 0;up<6;up++)
    		for(int front = 0;front<6;front++)
    		points[right][up][front]=new Point3f(getNumb(right, width, distance),getNumb(up, width, distance) ,getNumb(front, width, distance));
    		return points;
    		}
    		public IndexedQuadArray getCubies()
    		{
    		return cubies;
    		}
    		public float getNumb(int numb, float width, float distance)
    		{
     
    		switch(numb)
    		{ 		case(0):
    		return (float)(-1.5 * width -distance);
    		case(1):
    		return (float)(-.5 * width - distance);
    		case(2):
    		return (float)(-.5 * width);
    		case(3):
    		return (float)(.5 * width);
    		case(4):
    		return (float)(.5 * width + distance);
    		default:
    		return (float)(1.5 * width + distance);
    		}
     
    		}
     
    		public void init(double width2, double distance2, Color[] colors)
    		{
     
    		int place= 0;
    		float width = (float)width2;
    		float distance = (float)distance2;
    		Point3f[][][] points = getPoints(width,distance);
    	 	int a = 0;
    		Color3b white = new Color3b((byte)255,(byte)255,(byte)255);
    		Color3b red = new Color3b((byte)255,(byte)5,(byte)10);
    		Color3b orange = new Color3b((byte)170,(byte)30,(byte)30);
    		Color3b green = new Color3b((byte)5,(byte)255,(byte)10);
    		Color3b yellow = new Color3b((byte)20,(byte)70,(byte)70);
    		Color3b blue = new Color3b((byte)1,(byte)10,(byte)255);
     
    		Point3f[] paint = {points[0][0][0], points[0][0][5], points[0][5][0], points[0][5][5], points[5][0][0], points[5][0][5], points[5][5][0], points[5][5][5]};
    		cubies.setColor(a, red);
    		cubies.setCoordinate(a++,paint[0] );// 4      5  
    		cubies.setColor(a, red);
    		cubies.setCoordinate(a++,paint[1] );//    0     1
    		cubies.setColor(a, red);
    		cubies.setCoordinate(a++,paint[3] ); //6      7
    		cubies.setColor(a, red);
    		cubies.setCoordinate(a++,paint[2] );  //  2     3
     
    		cubies.setColor(a, white);
    		cubies.setCoordinate(a++,paint[0] );
    		cubies.setColor(a, white);
    		cubies.setCoordinate(a++,paint[1] );
    		cubies.setColor(a, white);
    		cubies.setCoordinate(a++,paint[5] );
    		cubies.setColor(a, white);
    		cubies.setCoordinate(a++,paint[4] );
     
    		cubies.setColor(a, blue);
    		cubies.setCoordinate(a++,paint[2] );
    		cubies.setColor(a, blue);
    		cubies.setCoordinate(a++,paint[6] );
    		cubies.setColor(a, blue);
    		cubies.setCoordinate(a++,paint[7] );
    		cubies.setColor(a, blue);
    		cubies.setCoordinate(a++,paint[3] );
     
    		cubies.setColor(a, yellow);
    		cubies.setCoordinate(a++,paint[2] );
    		cubies.setColor(a, yellow);
    		cubies.setCoordinate(a++,paint[0] );
    		cubies.setColor(a, yellow);
    		cubies.setCoordinate(a++,paint[4] );
    		cubies.setColor(a, yellow);
    		cubies.setCoordinate(a++,paint[6] );
     
    		cubies.setColor(a, orange);
    		cubies.setCoordinate(a++,paint[1] );
    		cubies.setColor(a, orange);
    		cubies.setCoordinate(a++,paint[3] );
    		cubies.setColor(a, orange);
    		cubies.setCoordinate(a++,paint[7] );
    		cubies.setColor(a, orange);
    		cubies.setCoordinate(a++,paint[5] );
     
    		cubies.setColor(a, green);
    		cubies.setCoordinate(a++,paint[6] );
    		cubies.setColor(a, green);
    		cubies.setCoordinate(a++,paint[7] );
    		cubies.setColor(a, green);
    		cubies.setCoordinate(a++,paint[5] );
    		cubies.setColor(a, green);
    		cubies.setCoordinate(a++,paint[4] );
     
     
     
     
    // 		for(int p = 0;p<5;p++)
    // 		for(int i = 0;i<6;i+=2) 
    //  		for(int r = 0;r<6;r+=2) 
    //  		{
    //  		cubies.setCoordinate(a++,points[p][i][r]);	//One that goes front,
    //  		cubies.setCoordinate(a++,points[p][i][1 + r]);
    //  		cubies.setCoordinate(a++,points[p][1 + i][1 + r]);
    //  		cubies.setCoordinate(a++,points[p][1 + i][r]);
    // 		
    // 				cubies.setCoordinate(a++,points[p][i][r]);	//One that goes Left
    //  		cubies.setCoordinate(a++,points[p][i+1][r]);
    //  		cubies.setCoordinate(a++,points[p+1][1 + i][r]);
    //  		cubies.setCoordinate(a++,points[p+1][i][r]);
    // 		
    // 				cubies.setCoordinate(a++,points[p][i][r]);	//One that goes Up
    //  		cubies.setCoordinate(a++,points[p][i][1 + r]);
    //  		cubies.setCoordinate(a++,points[p+1][i][1 + r]);
    //  		cubies.setCoordinate(a++,points[p+1][i][r]);
    // 		
    // 			 		
    //  		}	
    a = 0;
    		for(int p = 0;p<6;p++)
    		for(int i = 0;i<6;i++) 
    		for(int r = 0;r<6;r++) 
    		cubiesP.setCoordinate(a++,points[p][i][r]);
    			}
     
     
     
     
     
          public RubiksCube(double width, double distance, Color[] colors) 
          {
    		init(width, distance, colors);
            }
       }


    Thank you so much for your help. (Note that you need to download the 3d api in order to compile it).


    Oh and something quickly: In RubiksCube class, the width is the width of a cubie (sticker) and the distance is the distance between the stickers (The line between pieces.)
    Last edited by minime12358; May 26th, 2011 at 08:35 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java 3D Problems - Canvas 3d and QuadArray rendering

    the width is the width of a cubie (sticker) and the distance is the distance between the stickers
    Why isn't this documented in the code?

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    10
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Java 3D Problems - Canvas 3d and QuadArray rendering

    Sorry, I was not originally working with other people... :/ I generally comment my code at the end so that I can look back on it in the future, but this is a project I will only be working on for the next ~2-3 weeks (I am implementing a 2 step algorithm solver as well as a brute force solver, I am also making this into a fully functional keyboard cube)

    Please though, if you have any questions, pleasea ask away, I know the code might not be clear in some parts.

    I have also Partially figured out my problem.... Ill post the snippets that half solve the canvas3D problem. It works, except the keylistener doesnt work.

      canvas.addKeyListener(new Rotator());
    		canvas.startRenderer();
    		canvas.setFocusable(true);
        // universe = new SimpleUniverse(canvas); // Doesnt Work; No frame display4ed
    		universe = new SimpleUniverse(); // Does Work; No canvas (No keyAdapter)
    		canvas.preRender();
     
    ...
     
         View v = universe.getViewer().getView();
        v.addCanvas3D(canvas);
         v.getCanvas3D(0).requestFocus(); // I have tried both individually and I am not sure which one works
    	  v.getCanvas3D(0).requestFocusInWindow();

    Again, I do not know why the KeyListener is not working.... And I dont know why it does not render correctly.

    Thanks

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java 3D Problems - Canvas 3d and QuadArray rendering

    Ok, I'll wait for the commented version.

  5. #5
    Junior Member
    Join Date
    Mar 2011
    Posts
    10
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Java 3D Problems - Canvas 3d and QuadArray rendering

    * sigh *

     
       import java.awt.Color;
       import com.sun.j3d.utils.geometry.GeometryInfo;
       import com.sun.j3d.utils.geometry.NormalGenerator;
       import com.sun.j3d.utils.universe.SimpleUniverse;
       import javax.media.j3d.*;
       import javax.vecmath.*;
       import java.io.*;
       import java.awt.event.*;
    	import javax.swing.*;
    // An Egyptian pyramid
    // Base divided into two triangles
       public class PyramidExample {
     
          public static PrintStream a = null;
          public static Canvas3D canvas = null;
     
          public static Vector3f viewTranslation = null;
          public static  Transform3D viewTransform = null;
     
          public static Transform3D rotation = null;
          public static	SimpleUniverse universe = null;
     
          public static void main(String[] args) throws Exception{
    		   a = System.out;
    			a.println("Print Output Stream Initialized");
         canvas = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
            a.println("Canvas Initialized");
         viewTranslation = new Vector3f();
    	  a.println("Vector Initialized");
          viewTransform = new Transform3D();
    		a.println("Print Output Stream Initialized");
     
         rotation = new Transform3D();
     
     
    		a.println("Initialized Public Variables");
             canvas.addKeyListener(new Rotator());
    		canvas.startRenderer();
    		canvas.setFocusable(true);
    		a.println("Initialized canvas");
        // universe = new SimpleUniverse(canvas); // Doesnt Work; No frame display4ed
    		universe = new SimpleUniverse(); // Does Work; No canvas (No keyAdapter)
    		a.println("Instantiated universe");
    		canvas.preRender();
    		a.println("Rendered canvas");
     
    	 universe.getViewingPlatform().setNominalViewingTransform();
    a.println("Set Nominal Viewing Transform");
             RubiksCube joe = new RubiksCube(.2, .01, null);	
    			a.println("Instantiated Rubiks Cube");
     
             viewTranslation.z = 3;
             viewTranslation.x = 0f;
             viewTranslation.y = .3f;
          a.println("Set View Translation Variables");
     
             initializeChild(universe, joe.getCubies() );
    			a.println("Initialized Cubies");
     
             initializeChild(universe, joe.cubiesP);
    			a.println("Initialized Points");
    			a.println("Changing");
    			Thread.sleep(1000);
          View v = universe.getViewer().getView();
        v.addCanvas3D(canvas);
     
    	  v.getCanvas3D(0).requestFocus();
     
              //rotateX(universe, 10, 1);
    //          Thread.sleep(1000);
    //          rotateY(universe, 10, -1);
    //          Thread.sleep(1000);
    //          rotateZ(universe, 10, 1);
    //          Thread.sleep(1000);
    //          rotateZ(universe, 10, -1);
    //          Thread.sleep(1000);
    //          rotateX(universe, 10, 1);
     
     
          }
          public static void initializeChild(SimpleUniverse universe, GeometryArray results) // This method adds an array to the SimpleUniverse
          {
             BranchGroup group = new BranchGroup();
             GeometryArray result = results;
             if(!(results instanceof PointArray))
             {
     
                IndexedQuadArray indexedCube = (IndexedQuadArray)results;
     
                Vector3f[] normals = { new Vector3f(0.0f, 0.0f, 1.0f),
                      new Vector3f(0.0f, 0.0f, -1.0f),
                      new Vector3f(1.0f, 0.0f, 0.0f),
                      new Vector3f(-1.0f, 0.0f, 0.0f),
                      new Vector3f(0.0f, 1.0f, 0.0f), new Vector3f(0.0f, -1.0f, 0.0f) };
             //Define the indices used to reference vertex array
                int coordIndices[] = { 0, 1, 2, 3, 7, 6, 5, 4, 0, 3, 7, 4, 5, 6, 2, 1,
                      0, 4, 5, 1, 6, 7, 3, 2 };
             //Define the indices used to reference normal array
                int normalIndices[] = { 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3,
                      4, 4, 4, 4, 5, 5, 5, 5};
             //Set the data
                indexedCube.setNormals(0, normals);
                indexedCube.setCoordinateIndices(0, coordIndices);
                indexedCube.setNormalIndices(0, normalIndices);
             //Define an appearance for the shape
     
             //Create and return the shape
                group.addChild(new Shape3D(indexedCube));
                universe.addBranchGraph(group);
     
     
             }
             else
             {
                Appearance pointApp = new Appearance();
     
             // enlarge the points
                pointApp.setPointAttributes(new PointAttributes(50, true));
                Shape3D pointShape = new Shape3D(results, pointApp);
                group.addChild(pointShape);
                universe.addBranchGraph(group);
             }
     
          }
     
    		// Note that all rotate methods rotate the universe. Speed is how fast it is turning and prime is which way it turns.
    		// There is no need for comments here as it works, trust me.
          public static void rotateX(SimpleUniverse universe, int speed, int prime) throws Exception // prime is either negative or positive 1
          {
             for(int i = 0;i<(Math.PI*50);i++)
             {
                rotation.rotX(((double)i)/100*prime);
                rotation.mul(viewTransform);
                u();
                v(universe);
                Thread.sleep(speed);
             }		
          }
          public static void rotateY(SimpleUniverse universe, int speed, int prime) throws Exception // prime is either negative or positive 1
          {
             for(int i = 0;i<(Math.PI*50);i++)
             {
                rotation.rotY(((double)i)/100*prime);
                rotation.mul(viewTransform);
                u();
                v(universe);
                Thread.sleep(speed);
             }		
          }
          public static void rotateZ(SimpleUniverse universe, int speed, int prime) throws Exception // prime is either negative or positive 1
          {
             for(int i = 0;i<(Math.PI*50);i++)
             {
                rotation.rotZ(((double)i)/100*prime);
                rotation.mul(viewTransform);
                u();
                v(universe);
                Thread.sleep(speed);
             }		
          }
          public static void v(SimpleUniverse universe) // Used to simplify the Rotates
          {
             universe.getViewingPlatform().getViewPlatformTransform().setTransform(rotation);
          }
          public static void u()
          {
             viewTransform.setTranslation(viewTranslation);// Used to simplify the Rotates
          }
          private static class Rotator implements KeyListener // Key Listener
          {
    		public void keyTyped(KeyEvent e)
    		{
    		}
    		public void keyReleased(KeyEvent e)
    		{
    		}
             public void keyPressed(KeyEvent e)
             {
    			System.out.println("Pressed"); // Turns the cube in the direction of the arrow pressed. To see if anything is pressed, I added the println
                try{
                   switch(e.getKeyCode())
                   {
                      case(KeyEvent.VK_LEFT):
                         rotateZ(universe, 10, -1);
                         break;
                      case(KeyEvent.VK_RIGHT):
                         rotateZ(universe, 10, 1);
                         break;
                      case(KeyEvent.VK_UP):
                         rotateY(universe, 10, 1);
                         break;
                      case(KeyEvent.VK_DOWN):
                         rotateY(universe, 10, -1);
                         break;
                   }
                }
                   catch(Exception WTF){}
     
             }
          }
       }


     import java.awt.Color;
    import com.sun.j3d.utils.geometry.GeometryInfo;
    import com.sun.j3d.utils.geometry.NormalGenerator;
    import com.sun.j3d.utils.universe.SimpleUniverse;
    import javax.media.j3d.*;
    import javax.vecmath.*;  
    	public class RubiksCube
       {
    	int place = 0;
          public IndexedQuadArray cubies = new IndexedQuadArray(24,GeometryArray.COORDINATES | GeometryArray.COLOR_3|IndexedQuadArray.NORMALS,24); // Initializes an array of size 24; 6 sides with vertices at the Top right, Top left, Bottom Right, and Bottom Left
    		public PointArray cubiesP = new PointArray(216, PointArray.COORDINATES);// Initializes an array with all points. This includes every sticker (9*6) * every corner of the sticker (4) = 216
    		public Point3f[][][] getPoints(float width, float distance)
    		{
    		Point3f[][][] points = new Point3f[6][6][6]; // This creates the grid. There are 6 sides, a 6x6 grid is on all sides.
    		int place = 0;
    		for(int right = 0;right<6;right++)
    		for(int up = 0;up<6;up++)
    		for(int front = 0;front<6;front++)
    		points[right][up][front]=new Point3f(getNumb(right, width, distance),getNumb(up, width, distance) ,getNumb(front, width, distance)); // Puts the point at the correct location
     
    		return points; // returns the array
    		}
    		public IndexedQuadArray getCubies() // accessor... No reason really to have it here, but we need it for the grade
    		{
    		return cubies;
    		}
    		public float getNumb(int numb, float width, float distance) // This returns the correct distance.
    		{
     
    		switch(numb)
    		{ 		case(0):
    		return (float)(-1.5 * width -distance); //Far edge
    		case(1):
    		return (float)(-.5 * width - distance); //far edge + width of the first cube
    		case(2):
    		return (float)(-.5 * width); // far edge + width of the first cube + distance between first cube and second
    		case(3):
    		return (float)(.5 * width); // the rest is a mirror of it. 
    		case(4):
    		return (float)(.5 * width + distance);
    		default:
    		return (float)(1.5 * width + distance);
    		}
     
    		}
     
    		public void init(double width2, double distance2, Color[] colors)
    		{
     
    		int place= 0;
    		float width = (float)width2; // Initializes them as floats
    		float distance = (float)distance2;
    		Point3f[][][] points = getPoints(width,distance);
    	 	int a = 0;
    		Color3b white = new Color3b((byte)255,(byte)255,(byte)255); // Initializes colors. I have not checked the colors.
    		Color3b red = new Color3b((byte)255,(byte)5,(byte)10);
    		Color3b orange = new Color3b((byte)170,(byte)30,(byte)30);
    		Color3b green = new Color3b((byte)5,(byte)255,(byte)10);
    		Color3b yellow = new Color3b((byte)20,(byte)70,(byte)70);
    		Color3b blue = new Color3b((byte)1,(byte)10,(byte)255);
     
    		Point3f[] paint = {points[0][0][0], points[0][0][5], points[0][5][0], points[0][5][5], points[5][0][0], points[5][0][5], points[5][5][0], points[5][5][5]};
    		cubies.setColor(a, red);// These add the points in along with their color. I have checked the coordinate system and it works. 
    		cubies.setCoordinate(a++,paint[0] );// 4      5  
    		cubies.setColor(a, red);
    		cubies.setCoordinate(a++,paint[1] );//    0     1
    		cubies.setColor(a, red);
    		cubies.setCoordinate(a++,paint[3] ); //6      7
    		cubies.setColor(a, red);
    		cubies.setCoordinate(a++,paint[2] );  //  2     3
     
    		cubies.setColor(a, white);
    		cubies.setCoordinate(a++,paint[0] );
    		cubies.setColor(a, white);
    		cubies.setCoordinate(a++,paint[1] );
    		cubies.setColor(a, white);
    		cubies.setCoordinate(a++,paint[5] );
    		cubies.setColor(a, white);
    		cubies.setCoordinate(a++,paint[4] );
     
    		cubies.setColor(a, blue);
    		cubies.setCoordinate(a++,paint[2] );
    		cubies.setColor(a, blue);
    		cubies.setCoordinate(a++,paint[6] );
    		cubies.setColor(a, blue);
    		cubies.setCoordinate(a++,paint[7] );
    		cubies.setColor(a, blue);
    		cubies.setCoordinate(a++,paint[3] );
     
    		cubies.setColor(a, yellow);
    		cubies.setCoordinate(a++,paint[2] );
    		cubies.setColor(a, yellow);
    		cubies.setCoordinate(a++,paint[0] );
    		cubies.setColor(a, yellow);
    		cubies.setCoordinate(a++,paint[4] );
    		cubies.setColor(a, yellow);
    		cubies.setCoordinate(a++,paint[6] );
     
    		cubies.setColor(a, orange);
    		cubies.setCoordinate(a++,paint[1] );
    		cubies.setColor(a, orange);
    		cubies.setCoordinate(a++,paint[3] );
    		cubies.setColor(a, orange);
    		cubies.setCoordinate(a++,paint[7] );
    		cubies.setColor(a, orange);
    		cubies.setCoordinate(a++,paint[5] );
     
    		cubies.setColor(a, green);
    		cubies.setCoordinate(a++,paint[6] );
    		cubies.setColor(a, green);
    		cubies.setCoordinate(a++,paint[7] );
    		cubies.setColor(a, green);
    		cubies.setCoordinate(a++,paint[5] );
    		cubies.setColor(a, green);
    		cubies.setCoordinate(a++,paint[4] );
     
     
     
     
    // 		for(int p = 0;p<5;p++)
    // 		for(int i = 0;i<6;i+=2) 
    //  		for(int r = 0;r<6;r+=2) 
    //  		{
    //  		cubies.setCoordinate(a++,points[p][i][r]);	//One that goes front,
    //  		cubies.setCoordinate(a++,points[p][i][1 + r]);
    //  		cubies.setCoordinate(a++,points[p][1 + i][1 + r]);
    //  		cubies.setCoordinate(a++,points[p][1 + i][r]);
    // 		
    // 				cubies.setCoordinate(a++,points[p][i][r]);	//One that goes Left
    //  		cubies.setCoordinate(a++,points[p][i+1][r]);
    //  		cubies.setCoordinate(a++,points[p+1][1 + i][r]);
    //  		cubies.setCoordinate(a++,points[p+1][i][r]);
    // 		
    // 				cubies.setCoordinate(a++,points[p][i][r]);	//One that goes Up
    //  		cubies.setCoordinate(a++,points[p][i][1 + r]);
    //  		cubies.setCoordinate(a++,points[p+1][i][1 + r]);
    //  		cubies.setCoordinate(a++,points[p+1][i][r]);
    // 		
    // 			 		
    //  		}	
    a = 0;
    		for(int p = 0;p<6;p++)
    		for(int i = 0;i<6;i++) 
    		for(int r = 0;r<6;r++) 
    		cubiesP.setCoordinate(a++,points[p][i][r]); // Puts all points in a PointArray
    			}
     
     
     
     
     
          public RubiksCube(double width, double distance, Color[] colors) 
          {
    		init(width, distance, colors); // Initializes the cube. No real point besides that it is simplified
            }
       }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java 3D Problems - Canvas 3d and QuadArray rendering

    I guess there should be a class on how to comment programs.
    A lot of your comments are similar to something like this:
    int i = 1; // Set i to 1

    Rather than
    int i = 1; // Start the search index at 1 vs being zero based

    the comment explains WHY i is being set to 1.

  7. #7
    Junior Member
    Join Date
    Mar 2011
    Posts
    10
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Java 3D Problems - Canvas 3d and QuadArray rendering

    Ok, I am sorry that it is not exactly what you wanted... Is there any chance you could

    A. Tell me which lines you are confused about

    or

    B. Help me fix the problem..?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java 3D Problems - Canvas 3d and QuadArray rendering

    Sorry, I took at look at installing the 3D stuff. It hits too many spots on my system.
    I don't have a separate system for testing, so I'll leave this to some one with 3D installed.

    Tell me which lines you are confused about
    here are a couple of uncommented pieces:
    (Math.PI*50)
    rotation.rotY(((double)i)/100*prime);

  9. #9
    Junior Member
    Join Date
    Mar 2011
    Posts
    10
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Java 3D Problems - Canvas 3d and QuadArray rendering

    Ok..

    And to the uncommented piece, It says in there that that part should be ignored as I am certain it works correctly.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java 3D Problems - Canvas 3d and QuadArray rendering

    I am certain it works correctly
    Famous last words.

  11. #11
    Junior Member
    Join Date
    Mar 2011
    Posts
    10
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Java 3D Problems - Canvas 3d and QuadArray rendering

    Quote Originally Posted by Norm View Post
    Famous last words.
    perhaps I should have said that I am certain it is irrelevant. It does this with and without the method.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java 3D Problems - Canvas 3d and QuadArray rendering

    Another look at your approach to commenting.
    There is a non obvious bit to your code that is not commented and you say: it is irrelevant
    It looks like the code is executed, so what does it do?
    If it is not executed, remove it from your code.

  13. #13
    Junior Member
    Join Date
    Mar 2011
    Posts
    10
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Java 3D Problems - Canvas 3d and QuadArray rendering

    It is a method which is put into the KeyListener under the switch statement. "Pressed" is printed before hand so that I know when a button is pressed..

    Simply put, All that it does is rotate the cube.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java 3D Problems - Canvas 3d and QuadArray rendering

    You're still missing the point. All this info about how your code works should be as comments in the code. Posting it here is a waste of time. Anyone reading your code won't be seeing it.

  15. #15
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Java 3D Problems - Canvas 3d and QuadArray rendering

    Norm is right. Although you should keep comments to a minimum because well-written code (e.g. informative variable/method/class names) should generally be clear, any code that is not obvious should be carefully commented.

    Of course, you don't need to comment code if you work alone and are quite happy you'll understand it all after 6 months (or even 6 weeks) away from it (although experienced coders generally do write comments for themselves because they have discovered it pays to do so).

    But in this particular case, you're asking other people to read and understand your code, so you're not working alone. If you want people to help you, you need to help them understand your code by clarifying it with appropriate comments.

Similar Threads

  1. How to shift a String in a rectangle which is type of Canvas
    By elenora in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: April 4th, 2011, 07:39 AM
  2. How to get scores from user in Canvas J2ME?
    By elenora in forum Java ME (Mobile Edition)
    Replies: 0
    Last Post: April 4th, 2011, 04:10 AM
  3. [SOLVED] JSF? not rendering.
    By newbie in forum Web Frameworks
    Replies: 2
    Last Post: March 21st, 2011, 08:45 PM
  4. Problems with rendering a jprogressbar
    By albertof in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 10th, 2010, 04:47 AM
  5. make textbox in canvas
    By mahdi in forum Java ME (Mobile Edition)
    Replies: 2
    Last Post: October 6th, 2009, 07:10 AM