Creating the rotating cube listener (3D)

There is virtually no difference between listeners for 2D and 3D views so this guide will only cover material used in the previous guide where neccessary.

Just as before we start with describing what the listener should be able to do (requirements).

Requirements for listener class

  1. the cube will rotate in 3D continuously i.e. an animation.
  2. a mouse click over the view will pause / restart the animation.
  3. be able to reverse the rotation direction.

Next we create the class outline and add some attributes / fields for the view's data.

public class ShowBox extends GViewListener {

  float a = 0;         // current angle of rotation
  float delta = 0.02f; // the change in angle for each frame
  boolean is_rotating = true;
  
  // Put any methods here in the class body

  
} // end of class body

Like the previous example this class extends the GViewListener class.

Before going any further it is time to add our code to display the view contents. We need to add a method called update to our class. Here is the code

  public void update() {
    PGraphics3D v = (PGraphics3D) getGraphics();
    v.beginDraw();
    v.ambientLight(100, 100, 10);
    v.directionalLight(255, 255, 0, 0.5f, 1, -2f);
    v.background(0);
    v.translate(v.width/2, v.height/2);
    v.rotateX(0.19f * a);
    v.rotateY(1.101f * a);
    v.rotateZ(1.357f * a);
    v.fill(255, 200, 128);
    v.stroke(255, 0, 0);
    v.strokeWeight(4);
    v.box(80);
    a += delta;
    v.endDraw();
  }

Important: All drawing commands view must be done inside this method.

Most of the code is self explanatory but I want to point out two differences between this and the previous listener.

The first difference is in line 2

PGraphics3D v = (PGraphics3D) getGraphics();

We now have a 3D view therefore we need to cast the graphics object to PGraphics3D so we can access the extra methods available to P3D.

In our previous listener the last statement was validate() but in this case we have an animation and we want to update the cube every frame. So leaving this statement out meets our first requirement.

Let's implement the second requirement 'a mouse click over the view will pause / restart the animation'. We do this by adding a method to handle the mouse click event...

  public void mouseClicked() {
    if (is_rotating) {
      validate(); // will stop update() and pause the animation
    } else {
      invalidate(); // will enable update() and restart the animation
    }
    is_rotating = !is_rotating;
  }

The final requirement 'be able to reverse the rotation direction' can be implented with a simple method ...

  public void reverseRotation() {
    delta *= -1;
  }

We can now reverse the direction of rotation by calling this method. This is demonstarted in the full eaxmple.

Again the listener is created we can simply add it to the view in setup() ...

  // Setup 3D view and listener
  view3D = new GView(this, 340, 24, 200, 200, P3D);
  viewer3D = new ShowBox();
  view3D.addListener(viewer3D);