Eclipse project that runs a Sketch


#1

Hello everyone :blush:
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
}

Thank you for your help :slight_smile:


#2

How can I avoid this?

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;
  }
}

#3

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! :smile: I opened a number of bugs around this in the past and they were marked won’t fix.


#4

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 :dizzy_face:


#5
  • How many other PApplet subclasses have you got?
  • 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.
  • And pauseSketch() to put it back to “sleep”.

#6

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?


#7

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
}

This overrides the PApplet method: https://github.com/processing/processing/blob/master/core/src/processing/core/PApplet.java#L3737. The original method just calls System.exit(0) which closes the JVM, which is not what you want.


#8

@hamoid
Oh, I used Eclipse Just because I wanted to have “complete” Java, but well, I guess I could do everything on the PDE :sweat_smile: 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