Using GWindow Effectively
The Processing developers provided a framework for programmers to create contributed libraries such as G4P. All of the controls except GWindow work nicely within the framework because Processing does not support multiple windows directly. So if you are using GWindow I strongly recommend reading this page and following the techniques described here.
The Life cycle of a GWindow
// First declare an instance of GWindow>
GWindow window;
// Then create the window(480x320 pixels) and show it at position [40,20]
window = GWindow.getWindow(this, 40, 20, 480, 320, JAVA2D);
Now the window has been created and is visible it will remain in existence while the main application is running. Only disappearing when the application stops executing.
If during the runtime of our sketch the window is no longer needed then we need to be able to close it. Before we look at ways to close the window you need to know that G4P allows you to specify the action to be performed when attempting to close a window with the setActionOnClose method
/**
* This sets what happens when the users attempts to close the window.
* There are 3 possible actions depending on the value passed.
* G4P.KEEP_OPEN - ignore attempt to close window (default action)
* G4P.CLOSE_WINDOW - close this window
* G4P.EXIT_APP - exit the app, this will cause all windows to close.
* @param action the required close action
*/
public abstract void setActionOnClose(int action);
Back to closing windows.
Now there are 3 ways we can close a window
- From the operating system, click on the window close icon
- From our sketch, call window.close()
- From our sketch, call window.forceClose()
This seems straightforward except what actually happens depends on the window's action-on-close attribute.
KEEP_OPEN
|
CLOSE_WINDOW
|
EXIT_APP
|
|
---|---|---|---|
click close icon
|
O
|
C
|
E
|
close()
|
O
|
C
|
E
|
forceClose()
|
C
|
C
|
E
|
O = Window remains open
C – Window is closed
E – Application is halted and all windows are closed
Lets go back to the life cycle of our window
GWindow window;
window = GWindow.getWimdow(this, 40, 20, 480, 320, JAVA2D);
window.setActionOnClose(G4P.CLOSE_WINDOW);
So now we can use any of the 3 ways discussed to close the window, lets assume we do it programmatically.
window.close();
The window will close but the resources allocated to it by the JRE will not be released during garbage collection. This is because our sketch still has a reference to the window. We need to delete all references to the window like this
window = null;
Provided we have not previously assigned the window to other variables e.g.
anotherWindow = window
then the resources will eventually be released. If we don't clear all
references to the window then the resources will remain causing a memory leak.
So putting it all together we have
// Declaration
GWindow window;
// Create the window and set is action on close attribute
window = GWindow.getWimdow(this, 40, 20, 480, 320, JAVA2D);
window.setActionOnClose(G4P.CLOSE_WINDOW);
// Window created, visible and closeable
// Close the window and nullify object references
window.close();
window = null;
Being effective
Creating and closing windows are CPU and memory intensive tasks so we should avoid doing this as much as possible. The simplest way of avoiding these tasks is to create the window once and show / hide it as and when needed.
So consider creating your window when the application starts with
window = GWindow.getWimdow(this, 40, 20, 480, 320, JAVA2D);
window.setAlwaysOnTop(true);
window.setVisible(false);
then later use the setVisible function to show / hide the window as required. The second line makes sure that the window appears above other windows when you want to show it.
Linux window closing issues (G4P V4.0)
Linux users reported errors when closing GWindows during program execution. Although V4.0.1 has improved the situation no true fix has been found. If you follow the suggestions on this page to avoid excessive window creation/destruction by keeping the GWindows open until the sketch ends it avoids the problem altogether.