PS3 :: G4P multiple windows progress report

For those who use G4P and/or interested in its inner workings the following 3-tab sketch shows how far I have got. Still a long way to go yet but it all looks promising. :)

The main window uses JAVA2D and creates a second window that uses P3D.

This is the main tab and shows the code the sketcher would enter to get a second window

// These are used by the secondary window to test mouse event
int x, y;
float a;

public void settings() {
  size(100, 100, JAVA2D);
}

public void setup() {
  // This code is provided by the sketcher
  G3Window g3w = G3Window.getWindow("My window", 20, 40, 480, 320, P3D);
  g3w.addDrawHandler(this, "g3wDraw");
  g3w.addMouseHandler(this, "g3wMouse");
}

public void draw() {
  background(0);
  ellipse(mouseX, mouseY, 20, 20);
}     

// Draw and mouse event handlers for secondary window
public void g3wDraw(PApplet app, GWinData data) {
  app.background(255);
  app.fill(0);
  app.ellipse(x, y, 10, 10);
  app.translate(app.width/2, app.height/2);
  app.rotateX(a);
  app.rotateY(a/1.23f);
  app.rotateZ(a/1.79f);
  a += 0.01;
  app.stroke(0);
  app.noFill();
  app.box(40);
}

public void g3wMouse(PApplet app, GWinData data, MouseEvent event) {
  x = event.getX();
  y = event.getY();
}

The next two tabs is code that would be in the G4P library so invisible to the user.

G3Window.java tab

import java.lang.reflect.Method;

import processing.core.*;
import processing.event.KeyEvent;
import processing.event.MouseEvent;

public final class G3Window extends PApplet {

  /**
   * Factory method to create and start a new window
   * 
   * @param title text to appear in frame title bar
   * @param px horizontal position of top-left corner
   * @param py vertical position of top-left corner
   * @param w width of drawing surface
   * @param h height of surface
   * @param r renderer e.g. JAVA2D, P3D etc.
   * @return the window created (in case the user wants its.
   */
  public static G3Window getWindow(String title, int px, int py, int w, int h, String r) {
    G3Window g3w = new G3Window(title, w, h, r);
    String loc = "--location=" + px + "," + py;
    String className = g3w.getClass().getName();
    String[] args = { loc, className};
    PApplet.runSketch(args, g3w);
    return g3w;
  }

  /** The object to handle the draw event */
  private Object drawHandlerObject = null;
  /** The method in drawHandlerObject to execute */
  private Method drawHandlerMethod = null;
  /** the name of the method to handle the event */
  private String drawHandlerMethodName;

  /** The object to handle the mouse event */
  private Object mouseHandlerObject = null;
  /** The method in mouseHandlerObject to execute */
  private Method mouseHandlerMethod = null;
  /** the name of the method to handle the event */
  private String mouseHandlerMethodName;


  public GWinData data;

  private final int w, h;
  private final String r;
  private final String title;

  // Might not need these now
  private boolean app_g_3d;
  public PMatrix orgMatrix = null;

  private G3Window(String title, int w, int h, String renderer) {
    this.title = title;
    this.w = w;
    this.h = h;
    this.r = renderer;
    // Not sure about this yet need to do more experiments
    // JAVA2D does not appear to have a matrix ???
    if (g != null && g.is3D()) {
      app_g_3d = g.is3D(); // could set true here
      orgMatrix = getMatrix((PMatrix3D)null);
      System.out.println("3D");
    }
    registerMethod("draw", this);
    registerMethod("mouseEvent", this);
  }

  public void settings() {
    size(w, h, r);
  }

  public void setup() {
    surface.setTitle(title); // does not like this in settings
  }

  public void draw() {
    pushMatrix();
    if (drawHandlerObject != null) {
      try {
        drawHandlerMethod.invoke(drawHandlerObject, new Object[] { this, data });
      } 
      catch (Exception e) {
        // Error message
      }
    }
    popMatrix();
  }

  public void mouseEvent(MouseEvent event) {
    if (mouseHandlerObject != null) {
      try {
        mouseHandlerMethod.invoke(mouseHandlerObject, new Object[] { this, data, event });
      } 
      catch (Exception e) {
        // Error message
      }
    }
  }

  /**
   * Attempt to add the 'draw' handler method. 
   * The default event handler is a method that returns void and has two
   * parameters PApplet and GWinData
   * 
   * @param obj the object to handle the event
   * @param methodName the method to execute in the object handler class
   */
  public void addDrawHandler(Object obj, String methodName) {
    try {
      drawHandlerMethod = obj.getClass().getMethod(methodName, new Class<?>[] {PApplet.class, GWinData.class } );
      drawHandlerObject = obj;
      drawHandlerMethodName = methodName;
    } 
    catch (Exception e) {
      // Error message
    }
  }

  /**
   * Attempt to add the 'mouse' handler method. 
   * The default event handler is a method that returns void and has three
   * parameters GWinApplet, GWinData and a MouseEvent
   * 
   * @param obj the object to handle the event
   * @param methodName the method to execute in the object handler class
   */
  public void addMouseHandler(Object obj, String methodName) {
    try {
      mouseHandlerMethod = obj.getClass().getMethod(methodName, 
        new Class<?>[] {PApplet.class, GWinData.class, MouseEvent.class } );
      mouseHandlerObject = obj;
      mouseHandlerMethodName = methodName;
    } 
    catch (Exception e) {
      // Error message
    }
  }
}

GWinData.java tab

// Dummy class to get things going
public class GWinData{ }

Comments

Sign In or Register to comment.