G4P restore minimized window

Hi Quark (Peter),
I would like to know how I can restore a minimized window (not the main GWindow). Tried the “setVisible” property to simulate minimizing and restoring but is seems that the window will unload after the visible property is set to “Hidden”. Is there a way to build a toggle button that restores or simulates the visibility of a window or restores the minimized window?
Other quick question is that all my non main windows start as hidden, but at start up the windows are first quickly visible (drawn) before the are made “hidden”. Any thoughts on how to eliminate the flickering?
Cheers Marcel.

The setVisible method is used to set the window’s visible flag which determines whether the OS shows the window, it does not unload anything :thinking:

Provided the secondary GWindow uses the default JAVA2D renderer (not P2D or P3D) it is entirely possible to programatically minimize and restore secondary windows.

The following sketch creates a secondary window with a simple G4P label saying just that. The sketch window has 2 buttons to minimuze and restore the secondary window. I have create three utility functions to handle this so it can be used with multiple secondary windows.

/*
 Demonstrates fow to minimize and restore a G4P window using the
 default JAVA2D renderer.
 */
import g4p_controls.*;

GButton btnMinimize;
GButton btnRestore;
GWindowAWT win1;
GLabel label1;

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

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

public javax.swing.JFrame getFrame(GWindowAWT window) {
  return (javax.swing.JFrame) ((processing.awt.PSurfaceAWT.SmoothCanvas) window.getSurface().getNative()).getFrame();
}

public void minimizeWindow(GWindowAWT window) {
  javax.swing.JFrame frame = getFrame(window);
  frame.setExtendedState(java.awt.Frame.ICONIFIED);
}

public void restoreWindow(GWindowAWT window) {
  javax.swing.JFrame frame = getFrame(window);
  frame.setExtendedState(java.awt.Frame.NORMAL);
}

public void btnMinimize_click(GButton source, GEvent event) {
  minimizeWindow(win1);
}

public void btnRestore_click(GButton source, GEvent event) {
  restoreWindow(win1);
}

synchronized public void win1_draw(PApplet appc, GWinData data) {
  appc.background(255, 240, 160);
}

public void createGUI() {
  G4P.messagesEnabled(false);
  G4P.setDisplayFont("Arial", G4P.PLAIN, 16);
  surface.setTitle("Sketch Window");
  btnMinimize = new GButton(this, 20, 30, 280, 30);
  btnMinimize.setText("Minimize Window");
  btnMinimize.addEventHandler(this, "btnMinimize_click");
  btnRestore = new GButton(this, 20, 90, 280, 30);
  btnRestore.setText("Restore Window");
  btnRestore.addEventHandler(this, "btnRestore_click");
  win1 = (GWindowAWT)GWindow.getWindow(this, "Window title", 0, 0, 240, 120, JAVA2D);
  win1.setActionOnClose(G4P.KEEP_OPEN);
  win1.addDrawHandler(this, "win1_draw");
  label1 = new GLabel(win1, 20, 30, 200, 60);
  label1.setTextAlign(GAlign.CENTER, GAlign.MIDDLE);
  label1.setText("Secondary Window");
  label1.setLocalColorScheme(GCScheme.PURPLE_SCHEME);
  label1.setOpaque(true);
}

I think someone has managed to do this but I can’t remember how :slightly_frowning_face:

2 Likes

Thanks again Peter for taking the time and making an effort to write the sample code.
I’ve made minor changes so it works within the (auto) created code form the G4P GUI Tool.
Cheers Marcel

When you first create the windows, set them to null.