# Eclipse project that runs a Sketch

**URL:** <https://discourse.processing.org/t/eclipse-project-that-runs-a-sketch/2547>\
**Category:** Coding Questions\
**Created:** [August 11, 2018, 7:12am UTC](https://discourse.processing.org/t/eclipse-project-that-runs-a-sketch/2547 "2018-08-11T07:12:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Essay](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/essay/32/1089_2.png) [@Essay](https://discourse.processing.org/u/Essay)\
**Post date:** [August 11, 2018, 7:12am UTC](https://discourse.processing.org/t/eclipse-project-that-runs-a-sketch/2547/1 "2018-08-11T07:12:54Z")

</div>

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:

```auto
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 🙂

---

<div class="post-metadata">

**Author:** ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)\
**Post date:** [August 11, 2018, 11:29am UTC](https://discourse.processing.org/t/eclipse-project-that-runs-a-sketch/2547/2 "2018-08-11T11:29:29Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [August 11, 2018, 1:16pm UTC](https://discourse.processing.org/t/eclipse-project-that-runs-a-sketch/2547/3 "2018-08-11T13:16:45Z")

</div>

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_.

---

<div class="post-metadata">

**Author:** ![Essay](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/essay/32/1089_2.png) [@Essay](https://discourse.processing.org/u/Essay)\
**Post date:** [August 11, 2018, 4:14pm UTC](https://discourse.processing.org/t/eclipse-project-that-runs-a-sketch/2547/4 "2018-08-11T16:14:05Z")

</div>

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 😵

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 11, 2018, 6:12pm UTC](https://discourse.processing.org/t/eclipse-project-that-runs-a-sketch/2547/5 "2018-08-11T18:12:36Z")

</div>

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

```auto
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”.

---

<div class="post-metadata">

**Author:** ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)\
**Post date:** [August 11, 2018, 9:27pm UTC](https://discourse.processing.org/t/eclipse-project-that-runs-a-sketch/2547/6 "2018-08-11T21:27:00Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![colouredmirrorball](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/colouredmirrorball/32/234_2.png) [@colouredmirrorball](https://discourse.processing.org/u/colouredmirrorball)\
**Post date:** [August 11, 2018, 10:09pm UTC](https://discourse.processing.org/t/eclipse-project-that-runs-a-sketch/2547/7 "2018-08-11T22:09:43Z")

</div>

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:

```auto
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](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.

---

<div class="post-metadata">

**Author:** ![Essay](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/essay/32/1089_2.png) [@Essay](https://discourse.processing.org/u/Essay)\
**Post date:** [August 11, 2018, 10:29pm UTC](https://discourse.processing.org/t/eclipse-project-that-runs-a-sketch/2547/8 "2018-08-11T22:29:23Z")

</div>

@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
