We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I've written some code in netbeans to be a JFrame that has a PApplet inside it running a simple sketch. My issue is that even when I create a public method in my PApplet class, I cannot access it from my JFrame class.
Using code from this example: http://www.sebastianoliva.com/en/en/2010/05/using-a-processing-sketch-as-a-java-component/trackback/
I have 3 classes, my main (BTW the markdown on this forum is mangling the code, I can't stop it form doing that, just note that if you move this project into netbeans using 3 different class files, import core.jar it will work if you comment out the line that's causing me problems.:
public class myMainClass {
public static void main(String[] args) {
new displayCreator().setVisible(true);
}
}
Then I have my JFrame:
public class displayCreator extends javax.swing.JFrame {
public displayCreator(){
this.setSize(600, 600); //The window Dimensions
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.JPanel panel = new javax.swing.JPanel();
panel.setBounds(20, 20, 600, 600);
processing.core.PApplet sketch = new simpleSketch();
panel.add(sketch);
this.add(panel);
sketch.testSetter(true); //<--- THIS IS CAUSING ME PROBLEMS!!! CANNOT FIND testSetter in "sketch".
sketch.init();
this.setVisible(true);
}
}
and of course my sketch:
import processing.core.*;
public class simpleSketch extends PApplet {
private boolean amIset = false;
public void testSetter(boolean setting) {
this.amIset = setting;
}
public void setup() {
size(400, 400);
background(0);
}
public void draw() {
background(0);
fill(200);
ellipseMode(CENTER);
ellipse(mouseX, mouseY, 40, 40);
}
}
When I have the line in displayCreator "sketch.testSetter(true); " I'm trying to set a value that lives inside the PApplet. The problem is that I can't. When I use Netbeans and try to autocomplete (type "sketch." then hit Ctrl+space) the method "testSetter" does not exists. If I just type it in manually, the code doesn't work even though "testSetter" is very clearly alive and well inside the simpleSketch class (I can use it without any problems inside its own class).
I really have no idea why is it preventing me from doing this.
Answers
What error are you getting?
One thing that jumps out at me is you're storing your
SimpleSketch
in aPApplet
variable. That will work (sinceSimpleSketch
is a subclass ofPApplet
, but that variable will only "know about" the methods inPApplet
.Your
testSetter()
class is not defined inPApplet
; it's defined inSimpleSketch
. So to have access to it, you have to store your instance in aSimpleSketch
variable:SimpleSketch sketch = new SimpleSketch();
More streamlined way of instantiating PApplet classes: :D
http://forum.Processing.org/two/discussion/11304/multiple-monitors-primary-dragable-secondary-fullscreen
Thanks!