Developer Tutorial 2

Updated for release version: 1.0.0-alpha

Hello Objects

This tutorial introduces some of the more common interface objects and how to use them to do some basic things. The implementations of these methods are located in the class mgui.tutorials.DevTutorial2.

Interface Objects

All interface objects in mgui inherit the Java interface InterfaceObject. This interface specifies only four methods, which allow an object's name to be set or retrieved, or to destroy an object. The main function of the interface, however, is as a placemarker for all methods which deal with an interface object, regardless of its type.

Shapes

InterfaceShape is a subclass of InterfaceObject which provides an interface between geometric shapes (instances of Shape) and the modelGUI environment. It will also be referred to as a "ShapeInt". Some of its functions are:

  • provides methods to set and access Attributes, which specify how a shape should be rendered
  • allows data to be associated with a shape's nodes
  • stores a list of ShapeListeners, which allow a ShapeInt to issue events when it is modified or destroyed

Box3D

A Box3D is specified by a 3D base point and three orthogonal vectors which define its dimensions. Thus a Box3D can have any orientation in R3. For our tutorial, let's get started by creating a few simple shapes and querying them. For this we create a class called mgui.tutorials.DevTutorial2, which inherits the abstract class mgui.command.CommandInterpreter. The latter is simply a convenience class for doing mgui tasks from the command line, which provides services like a timer and parameter extraction method. You can safely ignore these for now; they occur in the source code only for the sake of completeness. We can start our new class by adding a main method, and an if clause which responds to the command line argument "create_box_and_do_stuff":

package mgui.tutorials;
 
import java.util.HashMap;
 
import javax.vecmath.Point3f;
import javax.vecmath.Vector3f;
 
import mgui.command.CommandInterpreter;
import mgui.geometry.Box3D;
import mgui.geometry.util.GeometryFunctions;
 
/*****************************************************************
 * This class is the implementation of the mgui Developer Tutorial 2, located at 
 * <a href="http://mgui.wikidot.com/dev-tutorial-2">http://mgui.wikidot.com/dev-tutorial-2</a>.
 * 
 * @author Andrew Reid
 * @version 1.0
 * @since 1.0
 *
 */
public class DevTutorial2 extends CommandInterpreter {
 
    public static void main(String args[]){
 
        //start the timer to keep track of how long tasks take
        startTimer();
 
        String command = args[0];
        HashMap<String, String> params = getParameters(args);
 
        //create a box and do stuff to it
        if (command.equals("create-box")){
            create_box_and_do_stuff();
        }
 
        //output the elapsed time
        System.out.println("Elapsed time: " + getTimer() + "ms");
 
    }
 
}

..which can be called by at the command line by typing: java DevTutorial2 -classpath (<classpath entries>) create-box. Of course we have still to implement that method. Let's make a method which creates a Box3D object, outputs it, outputs its center point, rotates it, and outputs its new state:

/***************************************
 * Creates a simple box and does stuff to it.
 * 
 */
static void create_box_and_do_stuff(){
 
    //create the box
    Point3f base_pt = new Point3f(5, 6, 7);        //box's base point
    Vector3f s_axis = new Vector3f(5, 0, 0);    //S-axis
    Vector3f t_axis = new Vector3f(0, 10, 0);    //T-axis
    Vector3f r_axis = new Vector3f(0, 0, 3);    //R-axis
    Box3D my_box = new Box3D(base_pt, s_axis, t_axis, r_axis);
 
    //output to console
    System.out.println("**Before rotation**");
    System.out.println(my_box);
 
    //output its center point
    System.out.println("Center point: " + my_box.getCenter());
 
    //rotate it about its base point and output change
    GeometryFunctions.rotate(my_box, my_box.basePt, new Vector3f(1, 1, 0), Math.PI);
    System.out.println("\n**After rotation**");
    System.out.println(my_box);
    System.out.println("Center point: " + my_box.getCenter());
 
    //prettify
    System.out.println();
 
}

Note that the rotation is performed by the GeometryFunctions class, which is a utility class for all geometry-related operations. Running this code results in the following console output:

**Before rotation**
Box3D: origin (5.0, 6.0, 7.0), x-axis (5.0, 0.0, 0.0), y-axis (0.0, 10.0, 0.0), z-axis (0.0, 0.0, 3.0)
Center point: (7.5, 11.0, 8.5)

**After rotation**
Box3D: origin (5.0, 6.0, 7.0), x-axis (0.0, 5.0, 0.0), y-axis (10.0, 0.0, 0.0), z-axis (0.0, 0.0, -3.0)
Center point: (10.0, 8.5, 5.5)

Elapsed time: 62 ms

I/O

All I/O operations in mgui are performed by classes which inherit the interface InterfaceIO.

Data Sources

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License