This tutorial is based upon the tutorial written by Hideya Kawahara.

Aim: To demonstrate more complex manipulations and user interaction with a 3D application within the lg3d framework. We will create a sphere with the Earth texture and a small handle to move it.
Requirements: This tutorial builds upon Tutorial 1 and Tutorial 2.
To use this tutorial you will need a functional installation of the of lg3d. The stable version is the best version to begin with. This version is located at the lg3d-core web site. Follow the "Getting started with Project Looking Glass" link for installation instructions.
Note: You do not need to run the lg3d-session version.
Additionally, you need to have the Java 1.5 SDK. This is available at the Sun Java web site.
Steps:
  1. Initialize the 3D application
  2. Create a Textured Sphere
  3. Setup Some Simple Behabiours for the Earth
  4. Add a more complexe action adapter
  5. Initialize the Container
  6. Compile the code
  7. Run our application
Step 1: Initialize the 3D application
Just like in Tutorial 1 the first step is to create a Frame3D.
    Frame3D frame3d = new Frame3D();
In this tutorial we will be demonstration two behaviors, RotateAction and ResilientRotateAction. These are conflicting actions (you cannot have an object rotate and rotate resiliently) therefore we must create a container to house the sphere and then we can apply the ResilientRotateAction to the container itself.
    Container3D top = new Container3D();
Step 2: Create a textured sphere
The SimpleAppearance class provides a useful constructor that takes the location of a texture to load.
    SimpleAppearance earthApp = null;
    try {
      earthApp = new SimpleAppearance("earth.jpg");
    } catch (Exception e) {
      System.err.println("Failed to load texture: " + e);
    }
Now we can create the sphere using this textured appearance. Be sure to specify Primitive.GENERATE_TEXTURE_COORDS so that the texture coordinates will be generated along with the position coordinates.
    Sphere earth = new Sphere(0.03f,
      Primitive.GENERATE_NORMALS | Primitive.GENERATE_TEXTURE_COORDS | Primitive.GEOMETRY_NOT_SHARED,
      36, earthApp);
Now the sphere can be added to a Component3D, set it's position, and set the cursor for it.
Note we can set a cursor for each Component3D.
    Component3D earthComp = new Component3D();
    earthComp.addChild(earth);
    earthComp.setTranslation(0.0f, 0.0f, -0.01f);
    earthComp.setCursor(Cursor3D.E_RESIZE_CURSOR);
Step 3: Setup some simple behaviors for the earth

A number of actions are provided by the API. I suggest having a look through the org.jdesktop.lg3d.utils.action, org.jdesktop.lg3d.utils.actionadapter and org.jdesktop.lg3d.utils.eventadapter packages.

For this tutorial we will be implementing a RotateAction, a ScaleAction and a GenericEventPostAction. The GenericEventPostAction is a generic action for posting LgEvents (have a look at the org.jdesktop.lg3d.wg.event package) to the display server.

    earthComp.setRotationAxis(0.0f, 1.0f, 0.0f);
    new MouseClickedEventAdapter(earthComp,
      new ToggleAdapter(new RotateAction(earthComp, (float)Math.PI, 1000)));

    new MouseClickedEventAdapter(earthComp,
      new GenericEventPostAction(
        Frame3DToFrontEvent.class, frame3d));

    top.addChild(earthComp);
Step 4: Add a more complex action adapter
To demonstrate a more complex example of an action adapter we will add a handle to the earth to aid in dragging the earth about.
    SimpleAppearance handleApp = new SimpleAppearance(1.0f, 0.0f, 0.0f, 0.5f);
    Sphere handle = new Sphere(0.003f, handleApp, Primitive.GENERATE_NORMALS | Primitive.GEOMETRY_NOT_SHARED);
    Component3D handleComp = new Component3D();
    handleComp.addChild(handle);
    handleComp.setCursor(Cursor3D.MOVE_CURSOR);
    handleComp.setTranslation(0.0f, 0.05f, 0.0f);
    new MouseEnteredEventAdapter(handleComp,
      new ScaleAction(handleComp, 1.5f, 200));
Now we need to allow the handle to be grabbed to drag the sphere about. We use the ComponentMover to enable this action. Note this time we use two arguments - the first one is the item that can be grabbed, and the second is the item moved by the one that we drag. Since we move the entire application, we specify the frame3d object.
    new ComponentMover(handleComp, frame3d);
We can now add the handle to the Container3D
    top.addChild(handleComp);
Moving the earth about should not be an easy task! Therefore we shall add some visual effects to show the struggle. The following achieves the wiggle motion when the application is dragged.
    top.setRotationAxis(0.0f, 1.0f, 0.0f);
    new MouseDraggedEventAdapter(handleComp,
      new Float2Splitter(
        new FloatDiffer(
          new FloatScaler(500f, (float)Math.toRadians(30),
            new ResilientRotateAction(top, 1500))),
        null));
First we set the rotation axis for the rotation movement, and use ResilientRotateAction to actually wiggle the application. The Float2Splitter, FloatDiffer and FloatScaler convert and adjust the x and y values generated by MouseDraggedEventAdapter into a float that can be used for the rotation action.
Step 5: Initialize the container
Finally, we can add the frame to the container and initialize it to make it visible, as described in Tutorial 1 step 4. Note, since the sphere has different dimension and has been rotated and scaled, a large hint size should be given to the SceneManager to avoid conflicts with other 3D applications.
    frame3d.addChild(top);
    frame3d.setSize(0.06f, 0.10f, 0.08f);
    frame3d.setCapabilities();
    frame3d.setActive(true);
    frame3d.setVisible(true);
Step 6: Compile the code
We have finished writing the code. The full version includes the required import statements, and various constructor and main methods. Download Tutorial3.java. Additionally you need the earth texture image. Download earth.jpg - note this needs to be in the same directory as the application is run from (the same directory as the class file if you follow these instructions).
Now we need to compile the source file. The tutorial requires the lg3d core library and Java 1.5 to compile. The core library can be found under the lib directory of the lg3d distribution (replace $LG3DHOME with the path to the lg3d distribution).
    javac -cp "$LG3DHOME/lib/ext/lg3d-core.jar" Tutorial3.java
Step 7: Run our application
To run the tutorial, simply execute the runtutorial script with the name of the class file (without the .class extension)
    ./runtutorial Tutorial3
The lg3d desktop should be displayed with the earth textured sphere centered as you see below. The handle should be visible and can be grabbed allowing movement of the sphere.
The earth textured sphere from Tutorial3