G4P_2nd window_draw lines,circle etc

Is it possible to draw lines or circles and squares in a 2nd window we created with G4P ???

1 Like

Yes provided you added a draw handler. The first parameter in the draw handler is a PApplet instance so if the the parameter name is wapp then can do

waap.background(200,200,255); // light blue
wapp.ellipse(100,100,40,40);
// ...

Are you using GUI Builder?

2 Likes

Yes at the beginning, after making several edits, I have created different windows in which I want to display some simple charts.

1 Like

If you have further questions about this ask in this discussion. :smile:

Thanks you, i will do… :slight_smile:

In the 2nd window the conrols are constantly updated, and have I used the appc.pushMatrix () commands to design the chart.
appc.translate (1050,800);
code…
appc.popMatrix ();

the use of cpu goes up a lot, i’m thinking of building as a backround a picture ready to draw the chart. Do you think this technician will help the cpu ??? How can I add this picture ??? It’s possible??? I found on google some conversations you had but couldn’t figure out exactly what to do. i need a little push.Thanks! As you can see the 2nd window has grown quite a bit :slight_smile: . Screenshot_4|593x500

1 Like

G4P uses lazy updates for the controls. This means that the control visual is only updated if the state of the control changes.

So looking at the image you attached the only time consuming aspect would be the drawing of the graph and that should not be causing your CPU any problems.

Using pushMatrix(), translate() and popMatrix() is really basic stuff and does not strain the CPU at all.

Overall I’m very pleased with the result. The sensor values shown are at 5 / sec in the labels, I use the checkboxes to select the category of sensors I want to display. With one window and all sensors selected the cpu usage goes from 20% to 60% for the graph, I just thought that if it was 4 windows open it would significantly affect the cpu. However for both history and general knowledge, image placement as backround is technically feasible ??? Thanks.

It might do depending on the amount of CPU is used by each window draw.

There is a technique that I used in the Mandelbrot example that comes with G4P. In that sketch you can have multiple windows but only one can be active at any one time, the trick is to reduce the frameRate on the inactive windows and increase it when it becomes active. I have tried Mandelbrot with over 20 windows without any problems.

The sketch code presented here opens two G4P windows and shows the current frame count for each one. As the mouse pointer moves over a window the frame rate is increased and the counter is faster. When it moves off the window it slows down again.

This technique might be useful in your sketch.

import g4p_controls.*;

// Variable declarations 
GWindow win1, win2;

public void setup() {
  size(480, 320, JAVA2D);
  createGUI();
}

public void draw() {
  background(230);
}

// I have synchronised these methods because they are shared 
// by more than one window
synchronized public void win_draw(PApplet appc, GWinData data) { 
  appc.background(240);
  appc.fill(0);
  appc.textSize(48);
  appc.textAlign(CENTER, CENTER);
  appc.text("" + appc.frameCount, 0, 0, appc.width, appc.height);
}

synchronized public void win_mouse(PApplet appc, GWinData data, MouseEvent mevent) {
  switch(mevent.getAction()) {
  case MouseEvent.ENTER:
    appc.frameRate(100); // normally 60 did this to make the effect clearer
    break;
  case MouseEvent.EXIT:
    appc.frameRate(5);
    break;
  case MouseEvent.PRESS:
    break;
  case MouseEvent.RELEASE:
    break;
  case MouseEvent.DRAG:
    break;
  }
}

// Create the two extra windows. 
public void createGUI() {
  G4P.messagesEnabled(false);
  G4P.setMouseOverEnabled(false);
  surface.setTitle("Sketch Window");
  // Create two windows notice that they both use the same draw and
  // mouse handlers. This is done to reduce the amount of code, in your
  // application you probably have each window with their own.
  // Window 1
  win1 = GWindow.getWindow(this, "Window 1", 20, 30, 400, 100, JAVA2D);
  win1.noLoop();
  win1.setActionOnClose(G4P.KEEP_OPEN);
  win1.addDrawHandler(this, "win_draw");
  win1.addMouseHandler(this, "win_mouse");
  win1.frameRate(5);
  win1.loop();
  // Window 2
  win2 = GWindow.getWindow(this, "Window 2", 20, 200, 400, 100, JAVA2D);
  win2.noLoop();
  win2.setActionOnClose(G4P.KEEP_OPEN);
  win2.addDrawHandler(this, "win_draw");
  win2.addMouseHandler(this, "win_mouse");
  win2.frameRate(5);
  win2.loop();
}
2 Likes