I am currently using Java to write an image processing program with a command-line interface. I decide to use Processing-Java as its library to deal with those images. There is a class I wrote to view those loaded images called by the main class. However, when I close the window instance, the main process will also be terminated. How can I able to close the window instance without terminating the whole program?
public static void showVolumes(List<CT_Volume> volumes) {
String[] appletArgs = { "ImageViewer" };
ImageViewer instance = new ImageViewer(volumes);
runSketch(appletArgs, instance);
}
This static method is called by the main class, which is the driver program showing the CLI interface and interacting with the user. The CT_Volume
is an object that is used to encapsulate information of a set of images that being used, it contains a list of PImage
so that the image viewer can display the images. The user can call this static method when they want to view the images loaded to the memory. I want to keep the main process called this static method after the window instance is terminated.
Closing sketches in P2D and JAVA2D renderer modes calls the PApplet instance’s exitActual()
method, which contains the line System.exit(0)
– a static method – which terminates the currently running Java virtual machine.
Overriding exitActual()
with nothing (i.e. preventing the call to System.exit(0)
) prevents sketches from closing others, fixing the issue, but introduces another problem in the case of P2D sketches: the window is not disposed properly when the cross is clicked.
Override exitActual()
to prevent the JVM (and all sketches) closing when one sketch is closed.
@Override
public void exitActual() {
}
Introduce the following code (suitable in setup()
) such that P2D sketches are disposed properly:
if (getGraphics().isGL()) {
final com.jogamp.newt.Window w = (com.jogamp.newt.Window) getSurface().getNative();
w.setDefaultCloseOperation(WindowClosingMode.DISPOSE_ON_CLOSE);
}