We are about to switch to a new forum software. Until then we have removed the registration on this forum.
the P3D renderer in the following sketch with 2 windows in PS3 is not recognized. I get the error:
vertex() with x, y, and z coordinates can only be used with a renderer that supports 3D, such as P3D... .
if I put the content of the class Tetra inside a function and call it in draw() the problem does not appear?
a hint to solve this would be great. :)
float ballX;
float ballY;
float theta = 0.0;
Tetra tetra;
public void setup() {
size(900, 900, JAVA2D);
String[] args = {"SecondApplet"};
SecondApplet sa = new SecondApplet();
PApplet.runSketch(args, sa);
}
void draw() {
ballX = mouseX;
ballY = mouseY;
background(0);
ellipse(ballX, ballY, 10, 10);
}
public class SecondApplet extends PApplet {
public void settings() {
size(900, 900, P3D);
tetra = new Tetra(10);
}
public void setup() {
frameRate(30);
}
public void draw() {
background(255);
theta += 0.01;
translate(width/2, height/2, 0);
rotateX(theta);
rotateY(theta);
// translate the scene again
translate(100, 100, 20);
shape(tetra.gr);
}
}
public class Tetra {
// The PShape object
PShape gr;
int t;
Tetra(int t_) {
t = t_;
gr = createShape();
beginShape();
fill(150, 0, 0, 127);
vertex(-t, -t, -t);
vertex( t, -t, -t);
vertex( 0, 0, t);
fill(0, 150, 0, 127);
vertex( t, -t, -t);
vertex( t, t, -t);
vertex( 0, 0, t);
fill(0, 0, 150, 127);
vertex( t, t, -t);
vertex(-t, t, -t);
vertex( 0, 0, t);
fill(150, 0, 150, 127);
vertex(-t, t, -t);
vertex(-t, -t, -t);
vertex( 0, 0, t);
endShape();
}
}
Answers
What you're asking for us to explain is an advanced & not-so-known topic about how nested classes & closures work in Java! ~O)
You can "fix" it by simply moving your class Tetra into class SecondApplet; thus turning the former a nested class of the already nested class SecondApplet: 8-}
An even easier "fix" is get rid of class Tetra and turn it a method of class SecondApplet.
After all, you just create 1 instance of it. :>
thanks a lot go2Loop and sorry if my request is unawarely demanding a lot. I extracted this simpel sketch to illustrate my problem. but the sketch I am working on contains 19 classes with altogether some 1.000 lines of code which I work on the last 3 years. At least 2 classes (more will coming for sure) I need for drawing with the P3D Applet and one of it has potentially 37 instances. But your suggested solution is a way I will try: putting all 3D relevant code inside the class SecondApplet. Thanks!
That sounds too big to fit everything in! May I suggest another approach? :-\"
fill(#FF0000);
we type inp.fill(#FF0000);
.your suggestion is very welcome GoToLoop! I try this now. Thanks!
Thanks again for this great help GoToLoop! :) :) :)
In the following example I applied your code to my problem for notice: 2 windows, one Java2D, one P3D, 2 classes for each PApplet one.
Very good! <:-P Just some extra "silly" tips: B-)
You can replace the 3 statements below:
W/ just 1:
runSketch(platformNames, new SecondApplet());
It's advisable to declare those classes w/
static
so it won't compile if we forget thep.
. *-:)Remember if you wish to make class MyCircle available for any PApplet, you're gonna need to use
p.
there too. L-)thanks for all the info! one question arises from this tip:
there is lots of code already running in my big sketch and if possible I would like to avoid changing it. So in the example I tried to let the "p." for the Java2D (main window) away to verify if it is possible and it seemes to work. I thougt that it is so because it is the "first PApplet" then using the "p." for all the code in the second PApplet (P3D) which I am writing now? Would this work?
Those were just tips. Having the
p.
in a class makes it available for any PApplet, be it the main 1 or the others.However, if it's been decided that some inner class is gonna to be used exclusively for the main PApplet, you don't need the
p.
of course. :-\"a further question concerning the communication between classes:
I have one class physModel which takes some data from an external input and computes some analysis data,
then different classes for each of both PApplets which use these data for visualization once in 2D, once in 3D.
I intend to make the analysis data of physModel available for different classes of both PApplets: If I want to access data from physModel (a class inside the first PApplet) inside class vis3D of the second PApplet can I just say: physModel.anyArray[x]? And I suppose if vis3D inherits from physModel then I write just anyArray[x]?
or to relate this question more closely to our discussion examples: how to access e.g. the value of color c of class MyCircle inside class Tetra. I put some println() inside the classes to show the data in the console. I can ask data from Tetra inside MyCircle but not from MyCircle inside a Tetra instance.
a workaround to send data from circle to tetra by a function e.g. keyPressed():
Before any further explanation an important observation about naming conventions in Java:
Variables & methods use lowerCase names, while classes & interfaces go w/ UpperCase names.
Therefore physModel should be class PhysModel.
And vis3D should be class Vis3D instead.
yes i know, I was negligent mixing class with instance names
Your workaround based on declaring "global" variables so they can be accessed everywhere is valid.
Although it breaks some "snob" principles about separation of concerns.
If field tetra is initialized in class SecondApplet, it should be declared there as well, not globally.
Well, I've fixed your last example by transferring field tetra inside SecondApplet.
In its place I've declared a "global" variable to hold the reference for SecondApplet:
final SecondApplet sa = new SecondApplet();
Now in order for any class outside SecondApplet to access its members, just prefix them w/
sa.
:sa.tetra
We can't use
p.
in those cases b/c, for example, member tetra doesn't originally exists in class PApplet.That is, tetra wasn't inherited from PApplet, but it's exclusively from SecondApplet.
There's a 2nd issue there as well: Is global field circle also supposed to be shared among classes?
Or should main PApplet got its own Circle instance and SecondApplet its own too? :-/
yes, there should be one class Circle shared with both PApplets.
No. In my sketch this class Circle get the external input and does some calculations which I would like to avoid doing twice.
Oki doki! Just 1 more detail: "global" variable circle is
null
until:circle = new MyCircle(this, myVec, #FFFF00, sa.tetra);
And tetra is
null
until:tetra = new Tetra(this, 10);
If any Thread tries to access either of them while they're still
null
the whole program is gonna crash!I believe you're aware that each PApplet got its own "Animation" Thread, right?
You may try to delay runSketch() only after circle is initialized.
Problem is that doing so, tetra is still
null
@circle = new MyCircle(this, myVec, #FFFF00, sa.tetra);
Therefore you're gonna need to plan out your program more carefully. Good luck! >-)
thank you for these insights. quite a double bind with Circle and Tetra communicating with each other >:). but this helps a lot. considering this I tend to make Circle (main PApplet) to the "operator" of the whole programm which sets and gets data from tetra. So hopefully I will work around the need to access Circle from tetra. :)