Java 3d - moveable grid by mouse

Posted on 2004-11-24
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
    LVL 8

    Expert Comment

    LVL 2

    Expert Comment

    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

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

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

    Accepted Solution

    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.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);


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Live - One-on-One Java Help from Top Experts

    Solve your toughest problems, fast.
    Java experts are online now and ready to help you.

    For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
    Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
    Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now