How to use Grafica objects/methods in an independent window generated by G4P GUI builder

FRVFRV
edited May 2018 in Library Questions

Dear all, I'm a beginer with Processing so my question might not be posed in the correct manner.

Still, I tried to use the Gplot objects and the related methods provided in the library Grafica. It works fine when the GPlot are manipulated in the MAIN window (i.e. main Draw function). I'm now trying to get the same graphs in an independent window generated with G4P builder. For that, I tried to place the Gplot related code (Gplot objects and manipulation) in the draw handler (win_draw) generated by G4P GUI builder. However, when I'm trying to use the same Gplot objects/methods there, I get consitently the following error :

_An error occured during execution of the eventhandler: CLASS: DataLogger1_1_PDE METHOD: win_draw Caused by java.lang.NullPointerException)

This makes sometime the computer (OSx10.8 / processing3) crashing.

Can anybody share experience/examples on how to pass a Gplot object to the draw handler / independent window + getting the plot drawn there and not in the main.

Thank you in advance for your hep !!!

Answers

  • Answer ✓

    Hi @FRV!

    Here is an example that displays two plots in two separate windows. I hope it helps you!

    /*
      Based on
      https://gist.github.com/atduskgreg/666e46c8408e2a33b09a
      https://forum.processing.org/two/discussion/12319/using-papplet-runsketch-to-create-multiple-windows-in-a-ps3-sketch
    */
    
    import grafica.*;
    
    GPlot plot1;
    GPlot plot2;
    
    void settings() {
      size(450, 300);
    }
    
    void setup() {
      // Start a new Processing applet
      MyPApplet newApplet = new MyPApplet();
      String[] args = {newApplet.getClass().getSimpleName()};
      PApplet.runSketch(args, newApplet);
    
      // Prepare the points for the first plot
      int nPoints = 100;
      GPointsArray points = new GPointsArray(nPoints);
    
      for (int i = 0; i < nPoints; i++) {
        points.add(i, 10*noise(0.1*i));
      }
    
      // Create the first plot
      plot1 = new GPlot(this);
    
      // Set the plot title and the axis labels
      plot1.setTitleText("Plot 1 in main Processing applet");
      plot1.getXAxis().setAxisLabelText("x axis");
      plot1.getYAxis().setAxisLabelText("y axis");
    
      // Add the points
      plot1.setPoints(points);
    
      // Activate the zooming and panning
      plot1.activateZooming();
      plot1.activatePanning();
    }
    
    void draw() {
      // Draw the first plot
      background(255);
      plot1.defaultDraw();
    }     
    
    
    public class MyPApplet extends PApplet {
    
      public void settings() {
        size(450, 300);
      }
    
      public void setup() {
        // Prepare the points for the second plot
        int nPoints = 100;
        GPointsArray points = new GPointsArray(nPoints);
    
        for (int i = 0; i < nPoints; i++) {
          points.add(i, 10*noise(0.1*i));
        }
    
        // Create the second plot ("this" reffers to the newApplet instance)
        plot2 = new GPlot(this);
    
        // Set the plot title and the axis labels
        plot2.setTitleText("Plot 2 in newApplet");
        plot2.getXAxis().setAxisLabelText("x axis");
        plot2.getYAxis().setAxisLabelText("y axis");
    
        // Add the points
        plot2.setPoints(points);
    
        // Activate the zooming and panning
        plot2.activateZooming();
        plot2.activatePanning();
      }
    
      public void draw() {
        // Draw the second plot
        background(255);
        plot2.defaultDraw();
      }
    }
    
  • edited May 2018 Answer ✓

    I have modified the DefaultPlot example that comes with Graphica to show the plot in a GWindow.

    Things to note -

    1) The GWindow must be created before the GPlot
    2) The parameter used to create the plot must be the GWindow instance (line 27)
    3) In the GWindow draw handler we must make sure the plot has been created to avoid a NPE (line 43)

    import g4p_controls.*;
    import grafica.*;
    
    GWindow window;
    GPlot plot;
    
    void setup() {
      size(300, 200);
      createGUI();
      createPlot();
    }
    
    void draw() {
      background(255, 255, 200);
    }
    
    void createPlot() {
      // Prepare the points for the plot
      int nPoints = 100;
      GPointsArray points = new GPointsArray(nPoints);
    
      for (int i = 0; i < nPoints; i++) {
        points.add(i, 10*noise(0.1*i));
      }
    
      // Create a new plot and set its position on the screen
      plot = new GPlot(window);
      plot.setPos(25, 25);
      // or all in one go
      // GPlot plot = new GPlot(this, 25, 25);
    
      // Set the plot title and the axis labels
      plot.setTitleText("A very simple example");
      plot.getXAxis().setAxisLabelText("x axis");
      plot.getYAxis().setAxisLabelText("y axis");
    
      // Add the points
      plot.setPoints(points);
    }
    
    synchronized public void win_draw(PApplet appc, GWinData data) {
      appc.background(230);
      if (plot != null) {
        plot.defaultDraw();
      }
    }
    
    public void createGUI() {
      G4P.messagesEnabled(false);
      G4P.setGlobalColorScheme(GCScheme.BLUE_SCHEME);
      G4P.setCursor(ARROW);
      surface.setTitle("Sketch window");
      window = GWindow.getWindow(this, "GWindow with Graphica plot", 0, 0, 500, 350, JAVA2D);
      window.noLoop();
      window.addDrawHandler(this, "win_draw");
      window.loop();
    }
    
  • hi quark,

    Work indeed - thanks for that !

Sign In or Register to comment.