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

Aim: To demonstrate simple manipulations and user interaction with a 3D application within the lg3d framework.
Requirements: This tutorial builds upon Tutorial 1.
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 lg3d 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. Rotate the box
  3. Allow the application to be moved
  4. Set the application's cursor
  5. Add some visual feedback
  6. Add some user interaction
  7. Initialize the container
  8. Compile the code
  9. Run our application
Step 1: Initialize the 3D application
  Follow steps 1 - 3 in Tutorial 1 to initialize the application, creating a box. As a slight variation to that try making the box translucent by changing the alpha value in the SimpleAppearance constructor.
    Frame3D frame3d = new Frame3D();
    SimpleAppearance app = new SimpleAppearance(0.6f, 0.8f, 0.6f, 0.7f);
    Box box = new Box(0.05f, 0.04f, 0.03f, Primitive.GENERATE_NORMALS | Primitive.GEOMETRY_NOT_SHARED, app);
    Component3D comp = new Component3D();
    comp.addChild(box);
Step 2: Rotate the box
To prove that this is a box, we can rotate it and see its 3D nature.
Component3D provides utility methods to achieve this. First, we specify the axis around which we rotate the component, using setRotationAxis.
    comp.setRotationAxis(1.0f, 0.5f, 0.0f);
This specifies the rotation to be around the line running through the X axis at 1, and the Y axis at 0.5.
Now we can set the angle to of rotation, using setRotationAngle. The API uses radians, but degrees are more commonly used, so we use Math.toRadians()to simplify the process.
    comp.setRotationAngle((float)Math.toRadians(60));
Please note that this shows reasonable lighting on each surface. It is the SceneManager's responsibility to lighten the world in a reasonable manner. If you want to it would be possible to write a SceneManager that changes lighting depending on the time of day, or the workload of your machine.
Step 3: Allow the application to be moved
One of the basic functions that should be provided by most applications is the ability to be moved through the displayed scene. To facilitate this process a predefined event action class, ComponentMover has been implemented.
It implements mouse event listeners and translational operators under the covers in order to achieve the action. The usage is simple - just specify a component you would like to move and an argument to its constructor.
    new ComponentMover(comp);
Step 4: Set the application's cursor
Now that the application can be moved, we should change the cursor to indicate that the component can be moved.
To do this, we make use of predefined Cursor3D shapes and the setCursor() method in order to associate the cursor to the component.
    comp.setCursor(Cursor3D.MOVE_CURSOR);
Step 5: Add some visual feedback
As is commonly seen in desktops, and other applications, some sort of visual feedback is usually provided to indicate that an action can take place on any given object. Commonly a button will change its appearance when the mouse moves over it to indicate that it can be pressed. The Project Looking Glass API provides the basic building blocks to implement visual user feedback.
Two key concepts are "event adapter" and "action". An event adapter is a listener that listens to specific events and converts it into a form that can invoke "action" via the Action interface.
An action is a class that implements at least one sub-interface of the Action interface. It accepts converted events and performs predefined actions accordingly.
This example uses a MouseEnteredEventAdapter that listens to mouse enter and exit events, and invokes performAction(boolean) method (which is declared in ActionBoolean) to propagate the stimulus. ScaleAction in turn implements the ActionBoolean interface and the performAction(boolean) method, which is used to receive the stimulus. It scales the specified component by the factor of its float argument (1.2f). The duration of the animation is specified by the third argument in milliseconds.
    new MouseEnteredEventAdapter(comp, new ScaleAction(comp, 1.2f, 500));
Step 6: Add some user interaction
Another standard practice of desktops is that the application is brought to the front when it is clicked on. To demonstrate simple user interaction we will implement this functionality in our application.
To do this we must do two things. First, listen for mouse click events to know when our application has been clicked on, and second, fire an event to the SceneManager requesting that the application be brought to the front.
To perform the first step, a utility class MouseClickedEventAdapter needs to be created to listen to the component. The MouseClickedEventAdapter then fires a Frame3DToFrontEvent (via a GenericEventPostAction) with the Frame3D that was clicked on. Assuming the SceneManager implements this event, the frame should be brought to the front.
    new MouseClickedEventAdapter(comp, new GenericEventPostAction(Frame3DToFrontEvent.class,frame3d));
Step 7: 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 box has been rotated and scaled, a large hint size should be given to the SceneManager to avoid conflicts with other 3D apps.
    frame3d.addChild(comp);
    frame3d.setSize(0.06f, 0.06f, 0.06f);
    frame3d.setCapabilities();
    frame3d.setActive(true);
    frame3d.setVisible(true);
Step 8: Compile the code
We have finished writing the code. The full version includes the required import statements, and various constructor and main methods. Download Tutorial2.java
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" Tutorial2.java
Step 9: Run our application
To run the tutorial, simply execute the runtutorial script with the name of the class file (without the .class extension)
    ./runtutorial Tutorial2
The lg3d desktop should be displayed with a pale green (translucent) box in the center (as you see below). When the mouse is moved over it the cursor should change the movement cursor, and the box should increase in size for have a second. Play around with your new application before moving on to the next tutorial which will cover greater interaction with the user, and a simple example of textures.
The rotated pale green box from Tutorial2