Processing 3.5.3 - G4P library - print variable to 2nd window

I’m using the g4p_controls library. Pressing a button opens a second window. In the 2nd window I have placed some labels that correspond to the values of a sensor. I’m trying to display the sensor value next to the label. How can I display the value of a variable in this 2nd window ?? is there any example of displaying the variable at any point on the 2nd window surface ???

public void button1_click1(GButton source, GEvent event) { //_CODE_:button1:896367:
  window1 = GWindow.getWindow(this, "Window title", 0, 0, 240, 120, JAVA2D);
  window1.addDrawHandler(this, "win_draw1");

**// In this point i want to print to the 2nd window the float variable from the sensor**

  label1 = new GLabel(window1, 9, 14, 80, 20);
  label1.setTextAlign(GAlign.CENTER, GAlign.MIDDLE);
} //_CODE_:button1:896367:

synchronized public void win_draw1(PApplet appc, GWinData data) { //_CODE_:window1:810925:
} //_CODE_:window1:810925:

public void createGUI(){
  surface.setTitle("Sketch Window");
  button1 = new GButton(this, 14, 16, 120, 30);
  button1.setText("open window");
  button1.addEventHandler(this, "button1_click1");

GButton button1; 
GWindow window1;
GLabel label1;
1 Like

Use a label on the second window for the value e.g. labal.setText("" + value)


I managed to do what you suggested and it was a success.
Plus I managed to change the fonts and size. What I failed to do is change the color.
But the main problem is that I must find a way to keep the Labeltext up to date like the variable do . It seems difficult for the level of knowledge I have. Maybe I need to change the whole design and logic.
Thank you for your help

try this sketch out, the label text is constantly update as the mouse moves

import g4p_controls.*;

GLabel lbl0;
float dist; // distance from the centre of window

void setup(){
  lbl0 = new GLabel(this, 50,20,100,20,"?");


void draw(){
  lbl0.setText("" + dist);
  // DRaw lines showing window centre
  line(0, height/2, width, height/2);
  line(width/2, 0, width/2, height);

// This Processing method is executed everytime
// the mouse moves.
void mouseMoved(){
  // Calculate the distance of the mouse from the centre of the window
  float dx = mouseX - width/2;
  float dy = mouseY - height/2;
  dist = sqrt(dx*dx+dy*dy);

Look at the G4P guides on my website. See the guides under colour schemes. :smile:

1 Like

First of all thank you very much for your help. I have managed to solve the main problem of constantly updating the variable within the form by studying many examples and using my intuition.

I used this routine and it works great for all texts in labels that appear in the 2nd window.

synchronized public void windowDraw1 (PApplet appc, GWinData data) {
  appc.background (230);
  AirTemp_sensor1_Data.setText ("" + millis () / 1000);
  AirHum_sensor1_Data.setText ("" + millis () / 500);
  SoilTemp_sensor1_Data.setText ("" + millis () / 250);
  SoilHum_sensor1_Data.setText ("" + millis () / 125);
  Sun_sensor1_Data.setText ("" + millis () / 10);
  UVA_sensor1_Data.setText ("" + millis () / 5);
  UVB_sensor1_Data.setText ("" + millis ());

I started with the GUI gap builder but have made some modifications to the original code given by the gap builder. So from this point on, and then I try to embed code without opening the gap builder so that always generate the original code. Thanks again. :slight_smile:

Στις Παρ, 21 Φεβ 2020 στις 4:55 μ.μ., ο/η Peter Lager via Processing Foundation έγραψε:

1 Like