Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Java 3d - moveable grid by mouse

Posted on 2004-11-24
Medium Priority
Last Modified: 2009-07-29
Hi, can anyone help?

I am trying to write a program in java that requires a 3D grid to be rotated about all three axes using the mouse - and the ability to scroll and pan.

Would anyone know how to go about this? It just needs to be a simple grid.

Thanks in advance....
Question by:bexMarriott
  • 2

Expert Comment

ID: 12671800
Are you making the geometry of the 3d grid by yourself( i mean manually drawing lines in a 3d space) or importing contents from a .obj file created by Maya???

Whatever ur method is, I think it will help you a lot if you go through the the tutorial for java3d from http://java.sun.com/products/java-media/3D/collateral/

Download 7 chapters from the site and go through them.....it's easy and fun....

Chapter 2, (Creating Content)
Chapter 3, (Easier Content Creation)
Chapter 4, (Interaction)
will help u do your job.

Accepted Solution

shah1d1698 earned 375 total points
ID: 12671847
Once I tried to navigate through a 3d grid and to rotate it with mouse. I built the 3d grid by drawing lines in the 3d space. I used the KeyNavigatorBehavior class to navigate through that 3d grid.

My code looked something like....
(You may need to modify the following code to meet your needs. But I suggest u first compile & run it.)

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;

import java.awt.event.*;
import java.awt.AWTEvent;
import java.util.Enumeration;
import com.sun.j3d.utils.behaviors.keyboard.*;
import com.sun.j3d.utils.behaviors.mouse.*;

public class KeyNav1 extends Applet

  public KeyNav1()
    setLayout(new BorderLayout());
    GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
    Canvas3D canvas = new Canvas3D(config);
    add("Center", canvas);

    SimpleUniverse su = new SimpleUniverse(canvas);
    BranchGroup scene = createSceneGraph(su);

  public BranchGroup createSceneGraph(SimpleUniverse su)
    BranchGroup objRoot = new BranchGroup();
    Geometry geo = createGeometry();
    Appearance app = new Appearance();
    ColoringAttributes ca = new ColoringAttributes();
    ca.setColor(0.0f, 0.0f, 1.0f);

    Shape3D shape = new Shape3D(geo, app);
    TransformGroup tg1 = new TransformGroup();

    MouseRotate rotate = new MouseRotate();
    rotate.setSchedulingBounds(new BoundingSphere());


    Background bkg = new Background();
    bkg.setApplicationBounds(new BoundingSphere());
    bkg.setColor(1.0f, 1.0f, 1.0f);

    TransformGroup tg = null;
    Transform3D translate = new Transform3D();
    translate.setTranslation(new Vector3f(0.0f, 0.3f, 0.0f));
    tg = su.getViewingPlatform().getViewPlatformTransform();

    KeyNavigatorBehavior knb = new KeyNavigatorBehavior(tg);
    knb.setSchedulingBounds(new BoundingSphere(new Point3d(0.0d, 0.0d, 0.0d), 1000.0d));

    return objRoot;

  public Geometry createGeometry()
    LineArray line = new LineArray(54,LineArray.COORDINATES);
    line.setCoordinate(0, new Point3f(5.0f, 0.0f, 0.0f));
    line.setCoordinate(1, new Point3f(-5.0f, 0.0f, 0.0f));
    line.setCoordinate(2, new Point3f(0.0f, 5.0f, 0.0f));
    line.setCoordinate(3, new Point3f(0.0f, -5.0f, 0.0f));
    line.setCoordinate(4, new Point3f(0.0f, 0.0f, 5.0f));
    line.setCoordinate(5, new Point3f(0.0f, 0.0f, -5.0f));
    line.setCoordinate(6, new Point3f(0.1f, 0.0f, 5.0f));
    line.setCoordinate(7, new Point3f(0.1f, 0.0f, -5.0f));
    line.setCoordinate(8, new Point3f(0.2f, 0.0f, 5.0f));
    line.setCoordinate(9, new Point3f(0.2f, 0.0f, -5.0f));
    line.setCoordinate(10, new Point3f(0.3f, 0.0f, 5.0f));
    line.setCoordinate(11, new Point3f(0.3f, 0.0f, -5.0f));
    line.setCoordinate(12, new Point3f(0.4f, 0.0f, 5.0f));
    line.setCoordinate(13, new Point3f(0.4f, 0.0f, -5.0f));
    line.setCoordinate(14, new Point3f(0.5f, 0.0f, 5.0f));
    line.setCoordinate(15, new Point3f(0.5f, 0.0f, -5.0f));
    line.setCoordinate(16, new Point3f(0.6f, 0.0f, 5.0f));
    line.setCoordinate(17, new Point3f(0.6f, 0.0f, -5.0f));
    line.setCoordinate(18, new Point3f(-0.1f, 0.0f, 5.0f));
    line.setCoordinate(19, new Point3f(-0.1f, 0.0f, -5.0f));
    line.setCoordinate(20, new Point3f(-0.2f, 0.0f, 5.0f));
    line.setCoordinate(21, new Point3f(-0.2f, 0.0f, -5.0f));
    line.setCoordinate(22, new Point3f(-0.3f, 0.0f, 5.0f));
    line.setCoordinate(23, new Point3f(-0.3f, 0.0f, -5.0f));
    line.setCoordinate(24, new Point3f(-0.4f, 0.0f, 5.0f));
    line.setCoordinate(25, new Point3f(-0.4f, 0.0f, -5.0f));
    line.setCoordinate(26, new Point3f(-0.5f, 0.0f, 5.0f));
    line.setCoordinate(27, new Point3f(-0.5f, 0.0f, -5.0f));
    line.setCoordinate(28, new Point3f(-0.6f, 0.0f, 5.0f));
    line.setCoordinate(29, new Point3f(-0.6f, 0.0f, -5.0f));

    line.setCoordinate(30, new Point3f(5.0f, 0.0f, -0.1f));
    line.setCoordinate(31, new Point3f(-5.0f, 0.0f, -0.1f));
    line.setCoordinate(32, new Point3f(5.0f, 0.0f, -0.2f));
    line.setCoordinate(33, new Point3f(-5.0f, 0.0f, -0.2f));
    line.setCoordinate(34, new Point3f(5.0f, 0.0f, -0.3f));
    line.setCoordinate(35, new Point3f(-5.0f, 0.0f, -0.3f));
    line.setCoordinate(36, new Point3f(5.0f, 0.0f, -0.4f));
    line.setCoordinate(37, new Point3f(-5.0f, 0.0f, -0.4f));
    line.setCoordinate(38, new Point3f(5.0f, 0.0f, -0.5f));
    line.setCoordinate(39, new Point3f(-5.0f, 0.0f, -0.5f));
    line.setCoordinate(40, new Point3f(5.0f, 0.0f, -0.6f));
    line.setCoordinate(41, new Point3f(-5.0f, 0.0f, -0.6f));

    line.setCoordinate(42, new Point3f(5.0f, 0.0f, 0.1f));
    line.setCoordinate(43, new Point3f(-5.0f, 0.0f, 0.1f));
    line.setCoordinate(44, new Point3f(5.0f, 0.0f, 0.2f));
    line.setCoordinate(45, new Point3f(-5.0f, 0.0f, 0.2f));
    line.setCoordinate(46, new Point3f(5.0f, 0.0f, 0.3f));
    line.setCoordinate(47, new Point3f(-5.0f, 0.0f, 0.3f));
    line.setCoordinate(48, new Point3f(5.0f, 0.0f, 0.4f));
    line.setCoordinate(49, new Point3f(-5.0f, 0.0f, 0.4f));
    line.setCoordinate(50, new Point3f(5.0f, 0.0f, 0.5f));
    line.setCoordinate(51, new Point3f(-5.0f, 0.0f, 0.5f));
    line.setCoordinate(52, new Point3f(5.0f, 0.0f, 0.6f));
    line.setCoordinate(53, new Point3f(-5.0f, 0.0f, 0.6f));

    return line;

  public static void main(String args[])
    Frame frame = new MainFrame(new KeyNav1(), 500, 500);


Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses
Course of the Month10 days, 13 hours left to enroll

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question