Processing in Eclipse with Multiple Classes using Singleton for Processing function access?
in
Integration and Hardware
•
3 years ago
I'm running through the Processing in Eclipse tutorial (
http://processing.org/learning/eclipse/) and I'm curious about the situation where one is running multiple classes. The tutorial suggests passing a reference to the PApplet when creating every other class where one would need to use a Processing function. It strikes me though that it would be easier to use a singleton style access. In this case one would just type MyApp.Instance.(whatever) when needing to call a Processing function. Is there some way that the PApplet can provide a public static reference to itself so that other classes in the code can use that to call all the Processing functions?
I tried creating it myself but my other classes wouldn't recognize the MyApp.Instance. I'm very new to Java so I really have no idea what I'm doing. I'm actually not really sure how the Processing "main" function works and whether it's sensible to have a public reference to it.
Here's the main sketch used in the eclipse tutorial:
- import processing.core.*;
- public class MyProcessingSketch extends PApplet {
- public void setup() {
- size(200,200);
- background(0);
- }
- public void draw() {
- stroke(255);
- if (mousePressed) {
- line(mouseX,mouseY,pmouseX,pmouseY);
- }
- }
- public static void main(String args[]) {
- PApplet.main(new String[] { "--present", "MyProcessingSketch" });
- }
}
What I attempted was to add a:
- private static MyProcessingSketch mInstance = null
- public MyProcessingSketch Instance()
- {
- if (null == mInstance)
- {
- mInstance = new MyProcessingSketch();
- }
- return mInstance;
- }
I don't know why this doesn't work, but I don't really know how the main function works or what processing/java is doing behind the scenes here.
Thanks,
Tavis
1