p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_java thread: RE: Java3D hassles


Message #1 by "John Owen" <johnowen11@c...> on Tue, 23 Apr 2002 09:33:43 -0500
Which version of the Java3D API are you using?

-----Original Message-----
From: chris green [mailto:son_of_s8n@h...] 
Sent: Tuesday, April 23, 2002 4:20 PM
To: Beginning Java
Subject: [beginning_java] Java3D hassles


Hi!

I am trying to write a program to display a raster of height values in 
Java3D.  I posted an earlier version of my program before, but now I
have 
a new problem...  The program seems to want to output a stack of cuboids

rather than the grid I want it to.  If anyone knows how to solve this I 
would be ecstatic... 

Thanx

Here is my program:


import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.io.*; // For File Input
import java.util.StringTokenizer; // For String Tokeniser

import javax.media.j3d.*;
import javax.vecmath.*;

import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.*;

import common.*;


/***********************************************************************
***
**************************
 * DemViewer -	Program to create DEM viewer, based on MouseNavigateTest

by Daniel Selman
 * Author - 	Chris Green
 * Date -		14-28 April 2002
 * Version - 	1.0
 *
 
************************************************************************
***
*************************/


public class DemViewer extends Java3dApplet implements 
RotationChangeListener, TranslationChangeListener, Serializable {
	private static int 			m_kWidth = 800;
	private static int 			m_kHeight = 600;

    public String	    		fileName = "DEM.grid";
    public int[][]	 			raster;
    public int   	  			sizeX = 10,
   		         				sizeY = 10,
   		         				row,
   		         				col;

    public double				zoomer = -50.0;

	Label						m_RotationLabel
= 
null;
	TextField					m_RotationFieldX
= 
null;
	TextField					m_RotationFieldY
= 
null;
	TextField					m_RotationFieldZ
= 
null;

	Label
m_TranslationLabel 
= null;
	TextField				
	m_TranslationFieldX = null;
	TextField				
	m_TranslationFieldY = null;
	TextField				
	m_TranslationFieldZ = null;

	public DemViewer( )
	{
		initJava3d( );
	}

	protected void addCanvas3D( Canvas3D c3d )
	{
   	setLayout( new BorderLayout() );
		add( c3d, BorderLayout.CENTER );

       Panel controlPanel = new Panel();

		// add the UI to the frame
		m_RotationLabel = new Label( "Rotation: " );
		m_RotationFieldX = new TextField( "0.00" );
		m_RotationFieldY = new TextField( "0.00" );
		m_RotationFieldZ = new TextField( "0.00" );
		controlPanel.add( m_RotationLabel );
		controlPanel.add( m_RotationFieldX );
		controlPanel.add( m_RotationFieldY );
		controlPanel.add( m_RotationFieldZ );

		m_TranslationLabel = new Label( "Translation: " );
		m_TranslationFieldX = new TextField( "0.00" );
		m_TranslationFieldY = new TextField( "0.00" );
		m_TranslationFieldZ = new TextField( "0.00" );
		controlPanel.add( m_TranslationLabel );
		controlPanel.add( m_TranslationFieldX );
		controlPanel.add( m_TranslationFieldY );
		controlPanel.add( m_TranslationFieldZ );

	    add( controlPanel, BorderLayout.SOUTH );

		doLayout( );
	}

	protected double getScale( )
	{
		return 1.0;
	}

	// do nothing for these notifications from the mouse behaviors
	public void onStartDrag( Object target )
	{
	}
	public void onEndDrag( Object target )
	{
	}
	public void onApplyTransform( Object target )
	{
	}
	public void onAdjustTransform( Object target, int xpos, int ypos
)
	{
	}

	// called by TornadoMouseRotate
	// yes, those really are Euler angles for the objects rotation
	public void onRotate( Object target, Point3d point3d )
	{
		m_RotationFieldX.setText( String.valueOf( (int) 
java.lang.Math.toDegrees( point3d.x ) ) );
		m_RotationFieldY.setText( String.valueOf( (int) 
java.lang.Math.toDegrees( point3d.y ) ) );
		m_RotationFieldZ.setText( String.valueOf( (int) 
java.lang.Math.toDegrees( point3d.z ) ) );
	}

	// called by TornadoMouseTranslate
	public void onTranslate( Object target, Vector3d vTranslation )
	{
		m_TranslationFieldX.setText( String.valueOf( 
vTranslation.x ) );
		m_TranslationFieldY.setText( String.valueOf( 
vTranslation.y ) );
		m_TranslationFieldZ.setText( String.valueOf( 
vTranslation.z ) );
	}

	// we want a black background
	protected Background createBackground( )
	{
		return null;
	}


	protected BranchGroup createSceneBranchGroup( )
	{
		BranchGroup objRoot = super.createSceneBranchGroup( );

		TransformGroup objTrans1 = new TransformGroup( );
		Transform3D t3d = new Transform3D( );
		objTrans1.getTransform( t3d );
		t3d.setTranslation( new Vector3d( 0.0,0.0,zoomer) );
		objTrans1.setTransform( t3d );

		TransformGroup objTrans = new TransformGroup( );
		objTrans.setCapability( 
TransformGroup.ALLOW_TRANSFORM_WRITE );
		objTrans.setCapability( 
TransformGroup.ALLOW_TRANSFORM_READ );

		// create the mouse rotate behavior
		TornadoMouseRotate mouseRotate = new TornadoMouseRotate(

0.001, 0.001 );
		mouseRotate.setInvert( true );
		mouseRotate.setObject( objTrans );
		mouseRotate.setChangeListener( this );
		mouseRotate.setSchedulingBounds( getApplicationBounds( )
);
		objTrans.addChild( mouseRotate );

		// create the mouse translate behavior and set limits
		TornadoMouseTranslate mouseTrans = new 
TornadoMouseTranslate( 0.005f );
		mouseTrans.setObject( objTrans );
		mouseTrans.setChangeListener( this );
		mouseTrans.setMinTranslate( new Point3d( -4,-4,-4 ) );
		mouseTrans.setMaxTranslate( new Point3d( 4,4,4 ) );
		mouseTrans.setSchedulingBounds( getApplicationBounds( )
);
		objTrans.addChild( mouseTrans );

		Appearance ap = new Appearance( );

		PolygonAttributes polyAttrbutes = new PolygonAttributes(
);
		polyAttrbutes.setPolygonMode( 
PolygonAttributes.POLYGON_LINE );
		polyAttrbutes.setCullFace( PolygonAttributes.CULL_NONE )
;
		ap.setPolygonAttributes( polyAttrbutes );

	    try
	    {
	    	BufferedReader inFile = new BufferedReader(new
FileReader (fileName)); // Reads in & buffers file

			raster = 	new int[sizeX][sizeY]; // 
Initialises 2D array

			String		s,
	   					v;

			while (inFile.ready())
			{
				s = inFile.readLine(); // Reads in line
of 
file
				StringTokenizer T = new StringTokenizer 
(s); // Tokenises line of file

				row = 0;
				col = 0;

				while (T.hasMoreTokens())
				{
					v = T.nextToken();
					Integer ZValue = new Integer
(v); // Envelopes token as integer
					raster[row][col] 
ZValue.intValue
(); // Enters integer into array
					col++; // Increases col variable

by 1
					System.out.print(".");
//Displays 
processing info
				}
				row++; // Increases row variable by 1
				col=0; // Resets col variable
				System.out.print("\n");
			}
	    	inFile.close();
	    }
	    catch (FileNotFoundException e) // Catches file not found
error
	    {
	    	System.err.println("Can't find file<" + fileName + "> :"
+ 
e);
	    }
	    catch (IOException e) // Catches file format error
	    {
	    	System.err.println("Error reading <" + fileName + "> :"
+ 
e);
	    }

		TransformGroup objTrans2 = new TransformGroup( );
		Transform3D demTrans = new Transform3D( );

		for(row=0; row<sizeY; row++)
		{
			for(col=0; col<sizeX; col++)
			{
				demTrans = new Transform3D( );
				demTrans.setTranslation( new Vector3d( 
row,0.0,col ) );
				objTrans2.getTransform( demTrans );
				objTrans2.addChild( new Cuboid (
1,raster
[col][row],1, ap ) );
			}
		}

		objTrans.addChild( objTrans2 );
		objTrans1.addChild( objTrans );
		objRoot.addChild( objTrans1 );

		return objRoot;
	}


	public static void main( String[] args )
	{
		DemViewer dView = new DemViewer( );

		new MainFrame( dView, m_kWidth, m_kHeight );
	}
}
---
Do you need true END-TO-END (e2e) Java Messaging (JMS)?  Softwired
offers industry's ONLY complete 100% JMS messaging solutions for your
needs TODAY. Visit
http://adtracking.wrox.com/track.asp?x=p2p%2Fe%2Fjava%2Dsoftwired&url=ww
w.softwired-inc.com Our unique product range includes: iBus//Mobile
(wireless data), iBus//MessageBus (IP Multicast), iBus//MessageServer
(store and forward) and much more!


Message #2 by "chris green" <son_of_s8n@h...> on Tue, 23 Apr 2002 16:19:53
Hi!

I am trying to write a program to display a raster of height values in 
Java3D.  I posted an earlier version of my program before, but now I have 
a new problem...  The program seems to want to output a stack of cuboids 
rather than the grid I want it to.  If anyone knows how to solve this I 
would be ecstatic... 

Thanx

Here is my program:


import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.io.*; // For File Input
import java.util.StringTokenizer; // For String Tokeniser

import javax.media.j3d.*;
import javax.vecmath.*;

import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.*;

import common.*;


/**************************************************************************
**************************
 * DemViewer -	Program to create DEM viewer, based on MouseNavigateTest 
by Daniel Selman
 * Author - 	Chris Green
 * Date -		14-28 April 2002
 * Version - 	1.0
 *
 
***************************************************************************
*************************/


public class DemViewer extends Java3dApplet implements 
RotationChangeListener, TranslationChangeListener, Serializable
{
	private static int 			m_kWidth = 800;
	private static int 			m_kHeight = 600;

    public String	    		fileName = "DEM.grid";
    public int[][]	 			raster;
    public int   	  			sizeX = 10,
   		         				sizeY = 10,
   		         				row,
   		         				col;

    public double				zoomer = -50.0;

	Label						m_RotationLabel = 
null;
	TextField					m_RotationFieldX = 
null;
	TextField					m_RotationFieldY = 
null;
	TextField					m_RotationFieldZ = 
null;

	Label						m_TranslationLabel 
= null;
	TextField				
	m_TranslationFieldX = null;
	TextField				
	m_TranslationFieldY = null;
	TextField				
	m_TranslationFieldZ = null;

	public DemViewer( )
	{
		initJava3d( );
	}

	protected void addCanvas3D( Canvas3D c3d )
	{
   	setLayout( new BorderLayout() );
		add( c3d, BorderLayout.CENTER );

       Panel controlPanel = new Panel();

		// add the UI to the frame
		m_RotationLabel = new Label( "Rotation: " );
		m_RotationFieldX = new TextField( "0.00" );
		m_RotationFieldY = new TextField( "0.00" );
		m_RotationFieldZ = new TextField( "0.00" );
		controlPanel.add( m_RotationLabel );
		controlPanel.add( m_RotationFieldX );
		controlPanel.add( m_RotationFieldY );
		controlPanel.add( m_RotationFieldZ );

		m_TranslationLabel = new Label( "Translation: " );
		m_TranslationFieldX = new TextField( "0.00" );
		m_TranslationFieldY = new TextField( "0.00" );
		m_TranslationFieldZ = new TextField( "0.00" );
		controlPanel.add( m_TranslationLabel );
		controlPanel.add( m_TranslationFieldX );
		controlPanel.add( m_TranslationFieldY );
		controlPanel.add( m_TranslationFieldZ );

	    add( controlPanel, BorderLayout.SOUTH );

		doLayout( );
	}

	protected double getScale( )
	{
		return 1.0;
	}

	// do nothing for these notifications from the mouse behaviors
	public void onStartDrag( Object target )
	{
	}
	public void onEndDrag( Object target )
	{
	}
	public void onApplyTransform( Object target )
	{
	}
	public void onAdjustTransform( Object target, int xpos, int ypos )
	{
	}

	// called by TornadoMouseRotate
	// yes, those really are Euler angles for the objects rotation
	public void onRotate( Object target, Point3d point3d )
	{
		m_RotationFieldX.setText( String.valueOf( (int) 
java.lang.Math.toDegrees( point3d.x ) ) );
		m_RotationFieldY.setText( String.valueOf( (int) 
java.lang.Math.toDegrees( point3d.y ) ) );
		m_RotationFieldZ.setText( String.valueOf( (int) 
java.lang.Math.toDegrees( point3d.z ) ) );
	}

	// called by TornadoMouseTranslate
	public void onTranslate( Object target, Vector3d vTranslation )
	{
		m_TranslationFieldX.setText( String.valueOf( 
vTranslation.x ) );
		m_TranslationFieldY.setText( String.valueOf( 
vTranslation.y ) );
		m_TranslationFieldZ.setText( String.valueOf( 
vTranslation.z ) );
	}

	// we want a black background
	protected Background createBackground( )
	{
		return null;
	}


	protected BranchGroup createSceneBranchGroup( )
	{
		BranchGroup objRoot = super.createSceneBranchGroup( );

		TransformGroup objTrans1 = new TransformGroup( );
		Transform3D t3d = new Transform3D( );
		objTrans1.getTransform( t3d );
		t3d.setTranslation( new Vector3d( 0.0,0.0,zoomer) );
		objTrans1.setTransform( t3d );

		TransformGroup objTrans = new TransformGroup( );
		objTrans.setCapability( 
TransformGroup.ALLOW_TRANSFORM_WRITE );
		objTrans.setCapability( 
TransformGroup.ALLOW_TRANSFORM_READ );

		// create the mouse rotate behavior
		TornadoMouseRotate mouseRotate = new TornadoMouseRotate( 
0.001, 0.001 );
		mouseRotate.setInvert( true );
		mouseRotate.setObject( objTrans );
		mouseRotate.setChangeListener( this );
		mouseRotate.setSchedulingBounds( getApplicationBounds( ) );
		objTrans.addChild( mouseRotate );

		// create the mouse translate behavior and set limits
		TornadoMouseTranslate mouseTrans = new 
TornadoMouseTranslate( 0.005f );
		mouseTrans.setObject( objTrans );
		mouseTrans.setChangeListener( this );
		mouseTrans.setMinTranslate( new Point3d( -4,-4,-4 ) );
		mouseTrans.setMaxTranslate( new Point3d( 4,4,4 ) );
		mouseTrans.setSchedulingBounds( getApplicationBounds( ) );
		objTrans.addChild( mouseTrans );

		Appearance ap = new Appearance( );

		PolygonAttributes polyAttrbutes = new PolygonAttributes( );
		polyAttrbutes.setPolygonMode( 
PolygonAttributes.POLYGON_LINE );
		polyAttrbutes.setCullFace( PolygonAttributes.CULL_NONE ) ;
		ap.setPolygonAttributes( polyAttrbutes );

	    try
	    {
	    	BufferedReader inFile = new BufferedReader(new FileReader
(fileName)); // Reads in & buffers file

			raster = 	new int[sizeX][sizeY]; // 
Initialises 2D array

			String		s,
	   					v;

			while (inFile.ready())
			{
				s = inFile.readLine(); // Reads in line of 
file
				StringTokenizer T = new StringTokenizer 
(s); // Tokenises line of file

				row = 0;
				col = 0;

				while (T.hasMoreTokens())
				{
					v = T.nextToken();
					Integer ZValue = new Integer
(v); // Envelopes token as integer
					raster[row][col] = ZValue.intValue
(); // Enters integer into array
					col++; // Increases col variable 
by 1
					System.out.print("."); //Displays 
processing info
				}
				row++; // Increases row variable by 1
				col=0; // Resets col variable
				System.out.print("\n");
			}
	    	inFile.close();
	    }
	    catch (FileNotFoundException e) // Catches file not found error
	    {
	    	System.err.println("Can't find file<" + fileName + "> :" + 
e);
	    }
	    catch (IOException e) // Catches file format error
	    {
	    	System.err.println("Error reading <" + fileName + "> :" + 
e);
	    }

		TransformGroup objTrans2 = new TransformGroup( );
		Transform3D demTrans = new Transform3D( );

		for(row=0; row<sizeY; row++)
		{
			for(col=0; col<sizeX; col++)
			{
				demTrans = new Transform3D( );
				demTrans.setTranslation( new Vector3d( 
row,0.0,col ) );
				objTrans2.getTransform( demTrans );
				objTrans2.addChild( new Cuboid ( 1,raster
[col][row],1, ap ) );
			}
		}

		objTrans.addChild( objTrans2 );
		objTrans1.addChild( objTrans );
		objRoot.addChild( objTrans1 );

		return objRoot;
	}


	public static void main( String[] args )
	{
		DemViewer dView = new DemViewer( );

		new MainFrame( dView, m_kWidth, m_kHeight );
	}
}
Message #3 by "chris green" <son_of_s8n@h...> on Tue, 23 Apr 2002 16:46:38
I am using Java3D v. 1.2.1_04 for Direct3D

  Return to Index