Hello everyone
I have a Java project in Eclipse that asks some informations to the user and then, when obtained these informations, runs a Processing sketck, that obviously opens a new window. Now, the program should iterate so itās possible to run more than one sketch, but when I close one of these windows the program completely stops. In that way, for example, at the 12th iteration i should have 12 open windows! How can I avoid this?
This is the code:
public class Finder extends PApplet{
public static void main(String[] args) {
String[] word = new String[1];
word[0] = "";
Scanner s = new Scanner(System.in);
do {
System.out.println("Type a word or type <end> to close: ");
word[0] = s.next();
if(!word[0].equals("end"))
PApplet.main("Finder", nome);
}
while(!nome[0].equals("end"));
s.close();
}
//Here overriding of settings(), draw() and so on
}
I assume you mean avoiding the closing of the whole program. Is that what you are asking?
I donāt know how to close a single window without closing the program, but I know you can stop() the applet and hide its surface. Later you can start() it again and re-show the surface.
So you could have a reusable list of PApplets (a pool). When a window is no longer needed, you hide it. When a new window is required, you show a hidden window (recycle) or create a new one if thereās no hidden ones available.
Here an example with just one window:
Popup popup = new Popup();
void setup() {
PApplet.runSketch(new String[] {"PopupWin"}, popup);
}
void draw() {
background(0);
ellipse(50, 50, 10, 10);
}
void mousePressed() {
popup.showNum(frameCount);
}
public class Popup extends PApplet {
int num = 0;
int frameCountToHide = 1;
public void settings() {
size(200, 100);
}
public void draw() {
if (frameCount == frameCountToHide) {
stop();
surface.setVisible(false);
} else {
background(255);
fill(0);
textSize(48);
text(num, 20, height - 20);
}
}
public void showNum(int num) {
this.num = num;
start();
surface.setVisible(true);
// hide in 100 frames
frameCountToHide = frameCount + 100;
}
}
Your best bet is probably looking at PApplet.exec() to run the other sketches in separate processes. Multiple windows, particularly allowing for sketches to keep running without bugs when another window is closed, is not officially supported. Therein lies a road of pain! I opened a number of bugs around this in the past and they were marked wonāt fix.
Thank you for the answers, but i canāt really understand both of you (sorry Iām new both at Processing and Java).
The point is that I have a ānormalā main without sketches in a class that extends PApplet, in which I also have setup(), draw() and so on. The ānormalā main may run one or more than one sketch. Every time a new sketch starts I donāt need the old one anymore, but I canāt close the old one because it makes stop also the ānormalā main. Do your solutions fit with this problem? Iām sorry but I really canāt understand
If itās not too many, you can simply runSketch() them all in āmainā's setup(), settings() or even main(); and place them all in an array of PApplet[] instances.
Then call pauseSketch() on each 1 right afterwards:
public static final int SKETCHES = 10;
public final PApplet[] sketches = new PApplet[SKETCHES];
public void settings() {
// Your code below to instantiate each PApplet subclass & store them in sketches[]:
// ...
// The loop below "pauses" them all:
for (final PApplet p : sketches) pauseSketch(p);
}
public static final pauseSketch(final PApplet p) {
p.noLoop();
p.getSurface().setVisible(false);
}
public static final resumeSketch(final PApplet p) {
p.loop();
p.getSurface().setVisible(true);
}
Later on, you can use resumeSketch() to āwake upā the chosen PApplet sketch.
Hi! I assumed you had more experience since people normally only use Eclipse at later stages, sorry about the complicated examples.
If you want to close the secondary window when opening a new one, why do you need to close it in the first place? Couldnāt you just call a function on the secondary window so it displays something different? What are you trying to achieve?
If you have access to the code of the sketches you want to launch, you could try to override their exit() or exitActual() method and see if that fixes the problem. Just leave the body empty:
public void exitActual()
{
//do nothing instead of quitting
}
@hamoid
Oh, I used Eclipse Just because I wanted to have ācompleteā Java, but well, I guess I could do everything on the PDE but right now while Iām typing I realized that I called noLoop() because the sketch was āstaticā, but probably I only need to loop and change the parameters and variables in the first window openedā¦ Ok now I feel a bit stupid