hello,
I am using Processing (3.2.3) in Eclipse and I have managed to produce a second PApplet window using using runSketch(). I would like to use the second window to control what goes on in the first (and vice versa as well) - so use PApplet 2 to control what’s going on in PApplet 1. I am not new to Processing but I am new to Eclipse and using full Java. So I am wondering how I may access classes which are being used by the other applet - ie. how would different PApplets share objects? Below is a very simplified version of my program to deal with the relevant code.
primary PApplet:
import processing.core.PApplet;
import java.io;
//etc..
public class Test extends PApplet {
// instances of used classes:
public Display display = new Display(this);
public Class2 class2 = new Class2(this);
//etc.. I have 12 classes I refer to
public static void main(String[] args) {
PApplet.main("Test");
}
public void settings() {
fullScreen(2);
// here is the secondary PApplet being created:
ControlPanel1 cp1 = new ControlPanel1(this);
final String[] switches = { "--sketch-p;ath=" + sketchPath(), "");
runSketch(switches, cp1);
}
public void setup() {
//
//
//
}
public void draw() {
//
//
//
}
Here is the second PApplet:
import processing.core.PApplet;
//
//
public class ControlPanel1 extends PApplet {
PApplet p;
// a class used by ControlPanel1
public ControlPanel1Display cp1D = new ControlPanel1Display(this);
Test test; // declaring other PApplet (?)
// constructor
public ControlPanel1(PApplet p) {
this.p = p;
}
public void settings() {
size(400, 400);
}
// 'display' is a class instantiated in the Test PApplet - not sure how to reference it here.
public void setup() {
background(display.deepBrown);
}
public void draw() {
//
//
//
}
public void keyPressed() {
switch (key) {
case 'i':
display.refreshScreen(test);
// another reference to the Display class which is instantiated in the Test PApplet
// the test object being passed is used in the same way that PApplet p is when using
// other classes referred to by the Test PApplet
// see example below
}
// in Display class definition:
void refreshScreen(Test test) {
//
//
//
}
This is the best I can do here. I am still on the steep end of the learning curve in terms of full Java and have read through the forum material and have not had any luck so far.
thank you for any help
1 Like
I believe you mean accessing declared members of 1 PApplet from another PApplet, right?
Given those new members aren’t inherited from PApplet, you’re gonna need their exactly subclass datatype.
1 Like
Yes, that is what I mean and in the course of trying to figure this out, I found one of your old posts:
( Multi-Monitor Sketch II (v2.0)
)
and applied what I learned from this and what you have said above and may have figured it out -
here is the altered code:
import processing.core.PApplet;
import java.io;
//etc..
public class Test extends PApplet {
// instances of used classes:
public Display display = new Display(this);
public SharedClass2 sharedClass2 = new SharedClass2(this); // shared between PApplets
public SharedClass3 sharedClass3 = new SharedClass3(this);
//etc.. I have 12 classes I refer to
public static void main(String[] args) {
PApplet.main("Test");
}
public void settings() {
fullScreen(2);
// here is the secondary PApplet being created:
ControlPanel1 cp1 = new ControlPanel1(this, display, sharedClass2, sharedClass3);
final String[] switches = { "--sketch-path=" + sketchPath(), "");
runSketch(switches, cp1);
}
public void setup() {
//
//
//
}
public void draw() {
//
//
//
}
Here is the second PApplet:
import processing.core.PApplet;
//
//
public class ControlPanel1 extends PApplet {
Test test;
Display display;
SharedClass2 sharedClass2;
SharedClass3 sharedClass3;
// a class declared by ControlPanel1
public ControlPanel1Display cp1D = new ControlPanel1Display(this);
// constructor
public ControlPanel1(Test test, Display display, SharedClass2 sharedClass2, SharedClass3 sharedClass3) {
this.test = test;
this.display = display;
this.sharedClass2 = sharedClass2;
this.sharedClass3 = sharedClass3;
}
public void settings() {
size(400, 400);
}
public void setup() {
background(display.deepBrown); // this now works
}
public void draw() {
//
//
//
}
public void keyPressed() {
switch (key) {
case 'i':
display.refreshScreen(test); // this now works
//
//
//
}
it is working…
2 Likes
Ran into another problem: I can only draw directly to the first PApplet window (“test”) from the 2nd PApplet (“ControlPanel1”) from within the draw() method. If I try to draw from anywhere else (for example, from within keyPressed()), it doesn’t work.
“test” is the same:
import processing.core.PApplet;
import java.io;
//etc..
public class Test extends PApplet {
// instances of used classes:
public Display display = new Display(this);
public SharedClass2 sharedClass2 = new SharedClass2(this); // shared between PApplets
public SharedClass3 sharedClass3 = new SharedClass3(this);
//etc.. I have 12 classes I refer to
public static void main(String[] args) {
PApplet.main("Test");
}
public void settings() {
fullScreen(2);
// here is the secondary PApplet being created:
ControlPanel1 cp1 = new ControlPanel1(this, display, sharedClass2, sharedClass3);
final String[] switches = { "--sketch-path=" + sketchPath(), "");
runSketch(switches, cp1);
}
public void setup() {
//
//
//
}
public void draw() {
//
//
//
}
“controlPanel1”:
import processing.core.PApplet;
//
//
public class ControlPanel1 extends PApplet {
Test test;
Display display;
SharedClass2 sharedClass2;
SharedClass3 sharedClass3;
// a class declared by ControlPanel1
public ControlPanel1Display cp1D = new ControlPanel1Display(this);
// constructor
public ControlPanel1(Test test, Display display, SharedClass2 sharedClass2, SharedClass3 sharedClass3) {
this.test = test;
this.display = display;
this.sharedClass2 = sharedClass2;
this.sharedClass3 = sharedClass3;
}
public void settings() {
size(400, 400);
}
public void setup() {
background(display.deepBrown);
}
public void draw() {
test.stroke(255);
test.line(50,50,500,500); // this places a line on the "test" window
stroke(255);
line(50,50,500,500); // this places a line on the "controlPanel1" window
}
public void keyPressed() {
switch (key) {
case 'i':
test.stroke(255);
test.line(50,50,500,500); // this does not work - nor does it work
// anywhere else except in draw()
//
//
//
}
Any help would be enormously appreciated. I have done all the searching I can for now.
1 Like
The main canvas of a PApplet can only be mutated under that PApplet’s own “Animation” Thread.
As a workaround we can use a JAVA2D PGraphics:
2 Likes
Thank you. My deadline is rapidly approaching, and I don’t have enough experience with threads to be able to implement what you are recommending here in time, though I have learned a lot from reading it. I am not sure how to construct multiple windows that can all control what goes on in each other (especially in terms of graphics) using this technique - I am assuming something like jFrames, because from what I can tell, multiple PApplets will not work that way.
We can control what happens in a PApplet outside its “Animation” Thread, as long as it’s not about mutating its PGraphics main canvas.
We can always exchange JAVA2D PGraphics or PImage objects among PApplet windows though.
4 Likes
Think I got it now - many thanks Go ToLoop - huge help.