Positioning the Objects : 3D Basics « 3D « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » 3D » 3D BasicsScreenshots 
Positioning the Objects
Positioning the Objects

/*
The Joy of Java 3D

by Greg Hopkins

Copyright Copyright 2001


*/

/*
Positioning the Objects

In Java 3D, locations are described by using x, y, z coordinates. Increasing coordinates 
go along the x-axis to the right, along the y-axis  upwards, and along the z-axis out of 
the screen. This is called a "right-handed" coordinate system because the thumb and first
two fingers of your right hand can be used to represent the three directions. All the 
distances are measured in meters.


To place your objects in the scene, you start at point (0,0,0), and then move the objects 
wherever you want. Moving the objects is called a "transformation", so the classes you 
use are: TransformGroup and Transform3D.  You add both the object and the Transform3D to 
a TransformGroup  before adding the TransformGroup to the rest of your scend



Step                                                             Example
1.  Create a transform, a transform                         Transform = new Transform3D();
group and an object                                          transformGroup tg  = new TransformGroup();
                                                 
                                                            Cone cone = new Cone(0.5f, 0.5f);
--------------------------------------------------------------------------------------------
2.  Specify a location for the object                        Vector3f vector = new Vector3f(-.2f,.1f , -.4f);
--------------------------------------------------------------------------------------------
3.  Set the transform to move (translate)                   Transform.setTranslation(vector); 
the object to that location  
--------------------------------------------------------------------------------------------
4.  Add the transform to the transform group                tg.setTransform(transform);
--------------------------------------------------------------------------------------------
5.  Add the object to the transform group                    tg.addChild(cone);


This example displays the different objects on each axis.
*/

import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3f;

import com.sun.j3d.utils.geometry.Cone;
import com.sun.j3d.utils.geometry.Cylinder;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.universe.SimpleUniverse;

public class Position {

  public Position() {
    SimpleUniverse universe = new SimpleUniverse();
    BranchGroup group = new BranchGroup();
    // X axis made of spheres
    for (float x = -1.0f; x <= 1.0f; x = x + 0.1f) {
      Sphere sphere = new Sphere(0.05f);
      TransformGroup tg = new TransformGroup();
      Transform3D transform = new Transform3D();
      Vector3f vector = new Vector3f(x, .0f.0f);
      transform.setTranslation(vector);
      tg.setTransform(transform);
      tg.addChild(sphere);
      group.addChild(tg);
    }
    // Y axis made of cones
    for (float y = -1.0f; y <= 1.0f; y = y + 0.1f) {
      TransformGroup tg = new TransformGroup();
      Transform3D transform = new Transform3D();
      Cone cone = new Cone(0.05f0.1f);
      Vector3f vector = new Vector3f(.0f, y, .0f);
      transform.setTranslation(vector);
      tg.setTransform(transform);
      tg.addChild(cone);
      group.addChild(tg);
    }
    // Z axis made of cylinders
    for (float z = -1.0f; z <= 1.0f; z = z + 0.1f) {
      TransformGroup tg = new TransformGroup();
      Transform3D transform = new Transform3D();
      Cylinder cylinder = new Cylinder(0.05f0.1f);
      Vector3f vector = new Vector3f(.0f.0f, z);
      transform.setTranslation(vector);
      tg.setTransform(transform);
      tg.addChild(cylinder);
      group.addChild(tg);
    }

    Color3f light1Color = new Color3f(.1f1.4f.1f)// green light
    BoundingSphere bounds = new BoundingSphere(new Point3d(0.00.00.0),
        100.0);
    Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
    DirectionalLight light1 = new DirectionalLight(light1Color,
        light1Direction);
    light1.setInfluencingBounds(bounds);
    group.addChild(light1);
    universe.getViewingPlatform().setNominalViewingTransform();

    // add the group of objects to the Universe
    universe.addBranchGraph(group);
  }

  public static void main(String[] args) {
    new Position();
  }
}

           
       
Related examples in the same category
1. Simple 3D DemoSimple 3D Demo
2. Basic steps needed to display 3D objectsBasic steps needed to display 3D objects
3. Basic ConstructBasic Construct
4. Appearance Scope
5. AppearanceAppearance
6. Appearance Mixed
7. Getting Started with the Java 3D API written in Java 3DGetting Started with the Java 3D API written in Java 3D
8. Simple RotationSimple Rotation
9. HelloJava3Db renders a single, rotated cubeHelloJava3Db renders a single, rotated cube
10. User Interfaces with Java
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.