dynamical change of PShape color settings in different Threads

edited November 2016 in How To...

I want to change the colors of PShapes dynamically. But using 2 different threads (with different colorModes) I get interferences between the coloring of one java2D and one P3D thread. (pictures flickering and seem to be influenced by the settings in the other thread.

One problem seems to be the setting of the colormode for PShape: in the processing and javadocs I found no information about how to set it. There is the method: public void colorMode(int mode, float maxX, float maxY, float maxZ, float maxA) If I understood right the variable "mode" sets the colorMode (RGB or Hue). But which value I have to set for this?

Answers

  • edited October 2016
    • Unfortunately function Processing's color() and everything which depends on it aren't thread-safe!
    • When we create a PShape, it uses the canvas' properties as its own.
    • But we can still change those initial props. Look at its reference:
      https://Processing.org/reference/PShape.html
    • And although not mentioned there, we can invoke colorMode() over it! :ar!
  • thanks a lot GoToLoop. I tried to invoke colorMode() e.g. having a PShape s in the second thread. Then invoking inside draw() s.colorMode(). But what is the argument for HSB mode? In the reference there is given an "int mode" I tried different values: 0, 1, 2 but without success. But if I write instead just colorMode(HSB, 255); then the image of the second thread is ok but on the cost of flickering and influencing the first one :-(?

  • Read the actual web reference for colorMode():
    https://Processing.org/reference/colorMode_.html

  • yes I read this some times. And I write before I set e.g. the background colorMode(RGB, 255); and then bakcground(0); but it still looks like a thunderstorm means the backgorund changes its colour in a flickering manner. At the same time in the second thread all is quite and the background is constant background(0); ? I do not change background colours in both threads.

  • My experience w/ PShape is almost nil. And discussing w/o some running example won't get us anywhere. Sorry I dunno what else I can do. :(

  • If possible please post an MCVE that briefly demonstrates what you are trying to accomplish, and the problem. Format code with CTRL-O.

  • thanks goToLoop and jeremydouglass for your answers. problem is that my code contains more than 20 classes and several thousand lines of code and there is a communication with another Audio IDE as well. So I tried to reduce the code to a small sketch but till now I could not recreate the wrong behaviour. But I will try to accomplish this.

  • If you would not recreate the wrong behavior (hooray!), maybe you should start with your base small sketch and start migrating your 20 classes over to it until the problem is re-introduced? If the problem isn't reintroduced, you've solved your problem!

  • yes I will do so but in the opposite direction: taking apart functions and classes would be easier and possibly faster to handle because of the somewhat labyrinthic dependencies.

  • @gerome Good luck with refactoring -- let the forum know if you narrow it down to a problem area and have questions....

  • edited October 2016

    I reduced the code and deleted several thousend lines:

    There are 2 threads and windows: one 2D (mainApplet) and one 3D (second Applet).

    I have a PShape sFrame wrapped with the StrFrame class which consists of several child PShapes s wrapped in the Str class. And the colours of the PShapes s are actualized in Str.setColor() which is called by StrFrame.setColor during draw in SecondApplet.displayStrFrames().

    Problem is that the setting of Colours in the PShapes seems to influence the colouring of the main Applet (2D thread): in irregular intervals the monitor of the 2D thread is flickering, sometimes you have to wait several seconds, sometimes it is more dense.

    int bands = 32768;
    float[] spectrum = new float[bands];
    final int quantaeChordae = 37;
    final SecondApplet sa = new SecondApplet();
    float [][] ssAHSB; 
    int wievieleSsA = 37; 
    int wievieleSsAZeichnen; 
    int obertoene = 1; 
    int wievieleTeiltoene = 8; 
    int wievieleSsAMitO = wievieleSsA * wievieleTeiltoene;
    float [] ssA;
    float [] ssAFreq; 
    float [][] ssAFreqMitO;
    int width = 1920;
    int height  = 1080;
    float testX;
    float testY;
    public void settings() {
      fullScreen(JAVA2D);
      //size(1920, 1080);
      runSketch(platformNames, sa);
    }
    void setup() {  
      frameRate(30);
      colorMode(RGB, 255); 
      background(0); 
      testX = 0;
      testY = 0; 
    } 
    void draw() {   
      background(0); 
    }
    float linearInterpolateMm(
      float x,  
      float xMax,  
      float yMax 
      ) {
      float b = x / xMax;
      float c = b * yMax;
      return c; 
    }   
    
    
      public class SecondApplet extends PApplet {    
      float fftFactor = 65536 / 44100;    
      boolean newAmps;
      int nOP;
      float [][] diffLastNewAmpsPerFrame;
      StrFrame [] strFrame;
      PhysM physM;  
      float [] ssA3D;
      float [][] amps;
      float maxBreite3D; 
      float maxDarzustellendesBreite3D; 
      float ySaitenDistanz3D;
    
      float shiftZ = 0;  
      float thetaX = 0; 
      float thetaY = 0; 
      float thetaZ = 0; 
    
      int tiefsteSaite;
      int hoechsteSaite; 
      int interpCount;   
      int iLS;
      int quantiTempi = 60;  
      int praesens = quantiTempi - 1;  
      int [] strFrameIndices;
      int modCount = praesens;  
      float transPraesenz = 255;
      float transSeconda = 80;  
      float transUltima = 4;  
      float transMinus = transSeconda / float(quantiTempi);
      float [] alphaVals;
      float [] zShifts;
      int nOF; 
      float nOFFactor;  
      float bands149;
    
      void settings() {
        //fullScreen(P3D, 2);
        size(800, 400, P3D);
      }
    
      void setup() {
        frameRate(30);    
        colorMode(HSB, 255);     
        newAmps = false;  
        physM = new PhysM(this);
        maxBreite3D = float(sa.width-300);    
        maxDarzustellendesBreite3D = physM.an[0]+physM.saitenLaengen[0];
        ySaitenDistanz3D = linearInterpolateMm(physM.saitenDistanz, maxDarzustellendesBreite3D, maxBreite3D);
    
        nOP = wievieleTeiltoene; //8
        nOF = quantiTempi;      //10 
        nOFFactor = 1.0 / float(nOF);     
    
        diffLastNewAmpsPerFrame = new float [quantaeChordae][nOP];  
        for (int i = 0; i < quantaeChordae; i++) {  
          for (int j = 0; j <  nOP; j++) {
            diffLastNewAmpsPerFrame[i][j] = 0;
          }
        }
    
        ssA3D = new float [quantaeChordae];
        for (int i = 0; i < quantaeChordae; i++) {
          ssA3D[i] = 0;
        }
    
        strFrameIndices = new int [quantiTempi];
        strFrame = new StrFrame [quantiTempi];
        alphaVals = new float [quantiTempi];
        logAlpha();
        alphaVals[praesens] = 255;
        for (int i = 0; i < quantiTempi; i++) {
        }
        zShifts = new float [quantiTempi];
        for (int i = 0; i < quantiTempi; i++) {      
          strFrameIndices[i] = i;    
          strFrame[i] = new StrFrame(this, i, nOP);       
          zShifts[i] = (quantiTempi - strFrameIndices[i])*1;
        }
    
        iLS = strFrameIndices[praesens-1];
    
        amps = new float [wievieleSsA][strFrame[praesens].nOP];
        for (int i =0; i < wievieleSsA; i++) {
          for (int j = 0; j < strFrame[praesens].nOP/*=wievieleTeiltoene*/; j++) {      
            amps[i][j] = 0;
          }
        }    
        bands149 = 0;
      } 
    
      void draw() {    
        displayStrFrames();
      } 
    
      void displayStrFrames() {    
        for (int i = 0; i < quantiTempi; i++) {
          pushMatrix();         
          strFrame[strFrameIndices[i]].setAplpha(alphaVals[i]);      
          if (strFrameIndices[i]==strFrameIndices[praesens]) {
          }        
          translate(0, 0, zShifts[i]);    
          strFrame[strFrameIndices[i]].display();       
          popMatrix();
        }
      }
    
      void logAlpha() {    
        float divident = quantiTempi - 2;
        float b = exp((log(transSeconda) - log(transUltima))/divident);
        for (int i = 0; i<quantiTempi-1; i++) { 
          alphaVals[i] =  transUltima * pow((b), float(i));
        }
      }
    } ///////////////////////////END OF class SecondApplet//////////////////
    
    
    public class Str { 
      final PApplet p;
      PShape s;  
      float strHue;
      float strSat;
      float strBright;
      float strAlpha;  
      color strColor;
      int strIndex;    
      int strFrameIndex;
      float length3D;   
      float xA, yA, zA;  
      float xB, yB, zB; 
      float ssA3D;   
      float [] partialAmps; 
      float [] resetPartialAmps;
      float [][] waveProfile;
      float increment = 5;   
      int anzahlZuBerechnenderPunkte;
      int anzahlZuSetzenderPunkte; 
      float angle;
      float angleVel;
      int nOP; 
      float zRange;
    
      Str(PApplet pa, int strFrameIndex_, int strIndex_, int nOP_) {     
        p = pa;      
        strFrameIndex = strFrameIndex_;    
        strIndex = strIndex_;
        nOP = nOP_;   
            length3D = linearInterpolateMm(
    sa.physM.saitenLaengen[strIndex], 
    a.maxDarzustellendesBreite3D, 
    sa.maxBreite3D);
            xA = linearInterpolateMm(sa.physM.an[strIndex], sa.maxDarzustellendesBreite3D, sa.maxBreite3D);
            yA = strIndex * sa.ySaitenDistanz3D; 
            xB = linearInterpolateMm(
    (sa.physM.an[strIndex]+sa.physM.saitenLaengen[strIndex]), sa.maxDarzustellendesBreite3D, 
    sa.maxBreite3D);
        yB = yA;
        zA = 0;
        zB = 0; 
    
        ssA3D = 0;
        anzahlZuBerechnenderPunkte = 0;
        anzahlZuSetzenderPunkte = int(round(length3D/increment))+1;    
    
        angle = 0;
        angleVel = 0;
        zRange = 15;
    
        waveProfile = new float [anzahlZuSetzenderPunkte][nOP+1];
        for (int i = 0; i <  anzahlZuSetzenderPunkte; i++) {
          for (int j = 0; j < (nOP+1); j++) {
            waveProfile[i][j] = 0; //displacements[i] = 0;
          }
        }
    
        strHue = 100;
        strSat = 100;
        strBright = 100;
        strAlpha = 100;
    
        strColor = color(strHue, strSat, strBright,strAlpha);
        s = p.createShape();    
        s.beginShape();     
        s.stroke(strColor);      
        s.fill(strColor);    
        s.vertex(xB, yB , zB);
        float x, y, z;
    
        for (int j = 0; j < anzahlZuSetzenderPunkte; j++) {
          x = xA + j * increment;    
          y = yA;
          z = waveProfile[j][nOP];
          s.vertex(x, y, z);
        }
        for (int j = 0; j < anzahlZuSetzenderPunkte; j++) {
          x = xA + j * increment;    
          y = yA;
          z = - waveProfile[j][nOP];       
          s.vertex(x, y, z);      
        }
        s.endShape();  
      }
    
      void setColor() {    
        strColor = color(strHue, strSat, strBright, strAlpha);
        s.setStroke(strColor);
        s.setFill(strColor);      
      } 
    } ///////END OF CLASS Str/////////////////////
    
    public class StrFrame {
    
      final PApplet p;
    
      int strFrameIndex;
    
      PShape sFrame;  
    
      int nOP; //numberOfPartials
    
      Str [] chordae3D;   
    
    
      StrFrame(PApplet pa, int strFrameIndex_, int nOP_) {
        p = pa;
        strFrameIndex = strFrameIndex_;
        nOP = nOP_; //8    
    
        chordae3D = new Str[quantaeChordae];     
    
        sFrame = p.createShape(GROUP);  
    
        for (int i = 0; i < quantaeChordae; i++) {
          //Saiten Objekt!
          chordae3D[i] = new Str(p, strFrameIndex, i, nOP);
          sFrame.addChild(chordae3D[i].s);
        } 
      } 
    
      StrFrame display() {    
        p.shape(sFrame);    
        return this;
      }
    
      void setAplpha(float a) {    
        for (int i = 0; i < quantaeChordae; i++) {
          chordae3D[i].strAlpha = a;
          chordae3D[i].setColor();
        }
      }
    
      void setColor(float a) { 
        colorMode(HSB, 255);
        //sFrame.colorMode(3, 255);
        for (int i = sa.tiefsteSaite; i <= sa.hoechsteSaite; i++) {
          chordae3D[i].strHue = ssAHSB[i][0];
          chordae3D[i].strSat = ssAHSB[i][1];
          chordae3D[i].strBright = ssAHSB[i][2];
          chordae3D[i].strAlpha = a;
    
          chordae3D[i].setColor();     
        }   
      }
    
    }  ///////END OF CLASS StrFrame////////////
    
     class PhysM {
          final PApplet p;  
          float [] an = new float[37];
          float [] saitenLaengen = new float[37];
          float [] saitenFrequenzen = new float[37];
          float [] laengenMalFrequenzen = new float [37];
          float saitenDistanz = 40; 
    
          PhysM(PApplet pa) {
            p = pa;  
            int n = 36; 
            float s52 = 938; 
            float s16 = 1301; 
            float sF52 = 106.2854; 
            float sF16 = 32.7032;    
            float diagonale = 913;    
            float steg = 373.5;   
            float sattel = 584;    
            float beta = acos((pow(s52, 2.0)- pow(steg, 2.0)-pow(diagonale, 2.0))/(-2.0*steg*diagonale));    
            float alpha = acos((pow(sattel, 2)-pow(s16, 2)-pow(diagonale, 2))/(-2*s16*diagonale));   
            float derWinkel = beta + alpha;
            float derWinkelInGrad = degrees((float) derWinkel);    
            float sGesamt =  tan((float)derWinkel-PI/2)*360;    
            float laengenDiff = (s16 - s52)/ (float)n; 
            float freqDiff = sF16/16; 
            for (int i = 0; i < (n+1); i++) {   
              an[n-i] = (sGesamt/ (float)n)*(float)i;      
              saitenLaengen[n-i] = s52 + (float)i * laengenDiff;      
              saitenFrequenzen[i] = freqDiff * float(i+16);       
            }
            for (int i = 0; i < (n+1); i++) {
              laengenMalFrequenzen[i] = saitenLaengen[i] * saitenFrequenzen[i];      
            }    
          } 
        }
        ////////////////////////END OF CLASS physM//////////////
    
  • to the Code given upstairs: I have a PShape sFrame wrapped with the StrFrame class which consists of several child PShapes s wrapped in the Str class. And the colours of the PShapes s are actualized in Str.setColor() which is called by StrFrame.setColor during draw in SecondApplet.displayStrFrames().

  • edited October 2016

    It's too big! It'd help a lil' bit if you hit CTRL+T inside the PDE to format the code better. 8-|

    In Java, the class some member belongs to, that is, its context, is determined where it is typed in within the code.

    You've got 2 classes derived from Processing's PApplet: the top 1, which all sketches got, and SecondApplet.

    All Processing API's accesses done outside SecondApplet, w/o using the dot . operator, belong to the top main PApplet.

    For example, when you call colorMode() @ line #296, inside class StrFrame, it's modifying the top main sketch, given it's not using the dot . operator.

    The same pattern goes to your 2 calls to method color() @ lines #224 & #248.
    Both calls are relying on the colorMode() defined for the top main sketch, not your SecondApplet's colorMode().

    If you need to specify which of those 2 PApplet instances it's referring to, you have to use the dot . operator.

    Again, if you want the color() from SecondApplet's instance, use your global variable sa instead:

    strColor = color(strHue, strSat, strBright, strAlpha); // using color() from main sketch.

    strColor = sa.color(strHue, strSat, strBright, strAlpha); // using color() from SecondApplet sketch.

    strColor = p.color(strHue, strSat, strBright, strAlpha); // using color() from the PApplet passed to Str's constructor.

  • I forgot the right formatting, please excuse my negligence!

    GoToLoop, you put the finger on the very right place: line 248, 224 resp. (if I do not call line 248 by commenting out line 142 the problem disappears) But if I put the "sa." or "p." before the color as you did in your examples I get an error message: "unexpected token: ." and the programm doesn´t start.

  • edited October 2016

    ...here is the Code given upstairs auto formatted:

    int bands = 32768;
    float[] spectrum = new float[bands];
    final int quantaeChordae = 37;
    final SecondApplet sa = new SecondApplet();
    float [][] ssAHSB; 
    int wievieleSsA = 37; 
    int wievieleSsAZeichnen; 
    int obertoene = 1; 
    int wievieleTeiltoene = 8; 
    int wievieleSsAMitO = wievieleSsA * wievieleTeiltoene;
    float [] ssA;
    float [] ssAFreq; 
    float [][] ssAFreqMitO;
    int width = 1920;
    int height  = 1080;
    float testX;
    float testY;
    public void settings() {
      fullScreen(JAVA2D);
      //size(1920, 1080);
      runSketch(platformNames, sa);
    }
    void setup() {  
      frameRate(30);
      colorMode(RGB, 255); 
      background(0); 
      testX = 0;
      testY = 0;
    } 
    void draw() {   
      background(0);
    }
    float linearInterpolateMm(
      float x, 
      float xMax, 
      float yMax 
      ) {
      float b = x / xMax;
      float c = b * yMax;
      return c;
    }   
    
    
    public class SecondApplet extends PApplet {    
      float fftFactor = 65536 / 44100;    
      boolean newAmps;
      int nOP;
      float [][] diffLastNewAmpsPerFrame;
      StrFrame [] strFrame;
      PhysM physM;  
      float [] ssA3D;
      float [][] amps;
      float maxBreite3D; 
      float maxDarzustellendesBreite3D; 
      float ySaitenDistanz3D;
    
      float shiftZ = 0;  
      float thetaX = 0; 
      float thetaY = 0; 
      float thetaZ = 0; 
    
      int tiefsteSaite;
      int hoechsteSaite; 
      int interpCount;   
      int iLS;
      int quantiTempi = 60;  
      int praesens = quantiTempi - 1;  
      int [] strFrameIndices;
      int modCount = praesens;  
      float transPraesenz = 255;
      float transSeconda = 80;  
      float transUltima = 4;  
      float transMinus = transSeconda / float(quantiTempi);
      float [] alphaVals;
      float [] zShifts;
      int nOF; 
      float nOFFactor;  
      float bands149;
    
      void settings() {
        //fullScreen(P3D, 2);
        size(800, 400, P3D);
      }
    
      void setup() {
        frameRate(30);    
        colorMode(HSB, 255);     
        newAmps = false;  
        physM = new PhysM(this);
        maxBreite3D = float(sa.width-300);    
        maxDarzustellendesBreite3D = physM.an[0]+physM.saitenLaengen[0];
        ySaitenDistanz3D = linearInterpolateMm(physM.saitenDistanz, maxDarzustellendesBreite3D, maxBreite3D);
    
        nOP = wievieleTeiltoene; //8
        nOF = quantiTempi;      //10 
        nOFFactor = 1.0 / float(nOF);     
    
        diffLastNewAmpsPerFrame = new float [quantaeChordae][nOP];  
        for (int i = 0; i < quantaeChordae; i++) {  
          for (int j = 0; j <  nOP; j++) {
            diffLastNewAmpsPerFrame[i][j] = 0;
          }
        }
    
        ssA3D = new float [quantaeChordae];
        for (int i = 0; i < quantaeChordae; i++) {
          ssA3D[i] = 0;
        }
    
        strFrameIndices = new int [quantiTempi];
        strFrame = new StrFrame [quantiTempi];
        alphaVals = new float [quantiTempi];
        logAlpha();
        alphaVals[praesens] = 255;
        for (int i = 0; i < quantiTempi; i++) {
        }
        zShifts = new float [quantiTempi];
        for (int i = 0; i < quantiTempi; i++) {      
          strFrameIndices[i] = i;    
          strFrame[i] = new StrFrame(this, i, nOP);       
          zShifts[i] = (quantiTempi - strFrameIndices[i])*1;
        }
    
        iLS = strFrameIndices[praesens-1];
    
        amps = new float [wievieleSsA][strFrame[praesens].nOP];
        for (int i =0; i < wievieleSsA; i++) {
          for (int j = 0; j < strFrame[praesens].nOP/*=wievieleTeiltoene*/; j++) {      
            amps[i][j] = 0;
          }
        }    
        bands149 = 0;
      } 
    
      void draw() {    
        displayStrFrames();
      } 
    
      void displayStrFrames() {    
        for (int i = 0; i < quantiTempi; i++) {
          pushMatrix();         
          strFrame[strFrameIndices[i]].setAplpha(alphaVals[i]);      
          if (strFrameIndices[i]==strFrameIndices[praesens]) {
          }        
          translate(0, 0, zShifts[i]);    
          strFrame[strFrameIndices[i]].display();       
          popMatrix();
        }
      }
    
      void logAlpha() {    
        float divident = quantiTempi - 2;
        float b = exp((log(transSeconda) - log(transUltima))/divident);
        for (int i = 0; i<quantiTempi-1; i++) { 
          alphaVals[i] =  transUltima * pow((b), float(i));
        }
      }
    } ///////////////////////////END OF class SecondApplet//////////////////
    
    
    public class Str { 
      final PApplet p;
      PShape s;  
      float strHue;
      float strSat;
      float strBright;
      float strAlpha;  
      color strColor;
      int strIndex;    
      int strFrameIndex;
      float length3D;   
      float xA, yA, zA;  
      float xB, yB, zB; 
      float ssA3D;   
      float [] partialAmps; 
      float [] resetPartialAmps;
      float [][] waveProfile;
      float increment = 5;   
      int anzahlZuBerechnenderPunkte;
      int anzahlZuSetzenderPunkte; 
      float angle;
      float angleVel;
      int nOP; 
      float zRange;
    
      Str(PApplet pa, int strFrameIndex_, int strIndex_, int nOP_) {     
        p = pa;      
        strFrameIndex = strFrameIndex_;    
        strIndex = strIndex_;
        nOP = nOP_;   
        length3D = linearInterpolateMm(
          sa.physM.saitenLaengen[strIndex], 
          a.maxDarzustellendesBreite3D, 
          sa.maxBreite3D);
        xA = linearInterpolateMm(sa.physM.an[strIndex], sa.maxDarzustellendesBreite3D, sa.maxBreite3D);
        yA = strIndex * sa.ySaitenDistanz3D; 
        xB = linearInterpolateMm(
          (sa.physM.an[strIndex]+sa.physM.saitenLaengen[strIndex]), sa.maxDarzustellendesBreite3D, 
          sa.maxBreite3D);
        yB = yA;
        zA = 0;
        zB = 0; 
    
        ssA3D = 0;
        anzahlZuBerechnenderPunkte = 0;
        anzahlZuSetzenderPunkte = int(round(length3D/increment))+1;    
    
        angle = 0;
        angleVel = 0;
        zRange = 15;
    
        waveProfile = new float [anzahlZuSetzenderPunkte][nOP+1];
        for (int i = 0; i <  anzahlZuSetzenderPunkte; i++) {
          for (int j = 0; j < (nOP+1); j++) {
            waveProfile[i][j] = 0; //displacements[i] = 0;
          }
        }
    
        strHue = 100;
        strSat = 100;
        strBright = 100;
        strAlpha = 100;
    
        strColor = color(strHue, strSat, strBright, strAlpha);
        s = p.createShape();    
        s.beginShape();     
        s.stroke(strColor);      
        s.fill(strColor);    
        s.vertex(xB, yB, zB);
        float x, y, z;
    
        for (int j = 0; j < anzahlZuSetzenderPunkte; j++) {
          x = xA + j * increment;    
          y = yA;
          z = waveProfile[j][nOP];
          s.vertex(x, y, z);
        }
        for (int j = 0; j < anzahlZuSetzenderPunkte; j++) {
          x = xA + j * increment;    
          y = yA;
          z = - waveProfile[j][nOP];       
          s.vertex(x, y, z);
        }
        s.endShape();
      }
    
      void setColor() {    
        strColor = color(strHue, strSat, strBright, strAlpha);
        s.setStroke(strColor);
        s.setFill(strColor);
      }
    } ///////END OF CLASS Str/////////////////////
    
    public class StrFrame {
    
      final PApplet p;
    
      int strFrameIndex;
    
      PShape sFrame;  
    
      int nOP; //numberOfPartials
    
      Str [] chordae3D;   
    
    
      StrFrame(PApplet pa, int strFrameIndex_, int nOP_) {
        p = pa;
        strFrameIndex = strFrameIndex_;
        nOP = nOP_; //8    
    
        chordae3D = new Str[quantaeChordae];     
    
        sFrame = p.createShape(GROUP);  
    
        for (int i = 0; i < quantaeChordae; i++) {
          //Saiten Objekt!
          chordae3D[i] = new Str(p, strFrameIndex, i, nOP);
          sFrame.addChild(chordae3D[i].s);
        }
      } 
    
      StrFrame display() {    
        p.shape(sFrame);    
        return this;
      }
    
      void setAplpha(float a) {    
        for (int i = 0; i < quantaeChordae; i++) {
          chordae3D[i].strAlpha = a;
          chordae3D[i].setColor();
        }
      }
    
      void setColor(float a) { 
        colorMode(HSB, 255);
        //sFrame.colorMode(3, 255);
        for (int i = sa.tiefsteSaite; i <= sa.hoechsteSaite; i++) {
          chordae3D[i].strHue = ssAHSB[i][0];
          chordae3D[i].strSat = ssAHSB[i][1];
          chordae3D[i].strBright = ssAHSB[i][2];
          chordae3D[i].strAlpha = a;
    
          chordae3D[i].setColor();
        }
      }
    }  ///////END OF CLASS StrFrame////////////
    
    class PhysM {
      final PApplet p;  
      float [] an = new float[37];
      float [] saitenLaengen = new float[37];
      float [] saitenFrequenzen = new float[37];
      float [] laengenMalFrequenzen = new float [37];
      float saitenDistanz = 40; 
    
      PhysM(PApplet pa) {
        p = pa;  
        int n = 36; 
        float s52 = 938; 
        float s16 = 1301; 
        float sF52 = 106.2854; 
        float sF16 = 32.7032;    
        float diagonale = 913;    
        float steg = 373.5;   
        float sattel = 584;    
        float beta = acos((pow(s52, 2.0)- pow(steg, 2.0)-pow(diagonale, 2.0))/(-2.0*steg*diagonale));    
        float alpha = acos((pow(sattel, 2)-pow(s16, 2)-pow(diagonale, 2))/(-2*s16*diagonale));   
        float derWinkel = beta + alpha;
        float derWinkelInGrad = degrees((float) derWinkel);    
        float sGesamt =  tan((float)derWinkel-PI/2)*360;    
        float laengenDiff = (s16 - s52)/ (float)n; 
        float freqDiff = sF16/16; 
        for (int i = 0; i < (n+1); i++) {   
          an[n-i] = (sGesamt/ (float)n)*(float)i;      
          saitenLaengen[n-i] = s52 + (float)i * laengenDiff;      
          saitenFrequenzen[i] = freqDiff * float(i+16);
        }
        for (int i = 0; i < (n+1); i++) {
          laengenMalFrequenzen[i] = saitenLaengen[i] * saitenFrequenzen[i];
        }
      }
    }
    ////////////////////////END OF CLASS physM//////////////
    
  • the following test sketch shows the problem but in short: if I say colorTetra = p.color(100,100,100, 100); I get the same error "unexpected token: ."

    So how can I make the color() belonging to SecondApplet's instance? :-??

    // excerpt from forum.Processing.org/two/discussion/17585/
    // 3d-renderer-not-recognized-in-secondapplet#Item_3
    
    // GoToLoop (2016-Jul-19) modified by Gerome 2016-Oct-29
    
    MyCircle circle;    
    
    Tetra tetra;
    Tetra tetra2;
    
    float theta = 0.0;
    
    void settings() {
      size(400, 300, JAVA2D);
      smooth(3);
      noLoop();
    
    runSketch(platformNames, new SecondApplet());
    }
    
    void setup() {
      ellipseMode(CENTER);
      strokeWeight(2.5);
      stroke(0);
    
      PVector myVec = new PVector(width>>1, height>>1, 0300);
      circle = new MyCircle(myVec, #FFFF00);
    }
    
    void draw() {
      background(255);
      background(0100);
      circle.display();
    }
    
    public class SecondApplet extends PApplet {
      public void settings() {
        size(400, 300, P3D);
      }
    
      public void setup() {
        frameRate(30);  
        //PVector myVec = new PVector(width>>1, height>>1, 0300);
        //circle = new MyCircle(this, myVec, #FFFF00);
        tetra = new Tetra(this, 10);
        tetra2 = new Tetra(this, 15);
      } 
    
      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);
    
        background(0100);
        //translate(mouseX, mouseY, 20);
        //circle.display();
        //translate(mouseX, mouseY, 0);
        tetra.setColor();
         tetra.display();
         //translate(100, 100, 20);
         tetra2.setColor();
         tetra2.display();     
        //box(100);
    
      }
    }
    
    public class MyCircle {  
      final PVector circle = new PVector();
      final color c;
    
      MyCircle(float x, float y, float diam, color cc) {    
        circle.set(x, y, diam);
        c = cc;
      }
    
      MyCircle(PVector vec, color cc) {    
        circle.set(vec);
        c = cc;
      }
    
      void display() {
        fill(c);
        ellipse(circle.x, circle.y, circle.z, circle.z);    
      }
    }
    
    
    static class Tetra {
    
      final PApplet p;
    
      // The PShape object
      final PShape gr;
      int t;  
      float speed;
    
      color colorTetra;
    
      Tetra(PApplet pa, int t_) {
        p = pa;
    
        t = t_; 
    
        colorTetra = p.color(100,100,100, 100);
        gr = p.createShape();
    
        gr.beginShape(TRIANGLES);
        gr.fill(colorTetra);
        gr.vertex(-t, -t, -t);
        gr.vertex( t, -t, -t);
        gr.vertex( 0, 0, t);
    
        gr.fill(colorTetra);
        gr.vertex( t, -t, -t);
        gr.vertex( t, t, -t);
        gr.vertex( 0, 0, t);
    
        gr.fill(colorTetra);
        gr.vertex( t, t, -t);
        gr.vertex(-t, t, -t);
        gr.vertex( 0, 0, t);
    
        gr.fill(colorTetra);
        gr.vertex(-t, t, -t);
        gr.vertex(-t, -t, -t);
        gr.vertex( 0, 0, t);
    
        gr.endShape();
      }
    
      Tetra display() {
        //p.box(100);
        p.shape(gr);
        return this;
      }
    
    
      void setColor() {   
        colorTetra = p.color(p.random(255), p.random(255),p.random(255),100);
        gr.setFill(colorTetra);
        //strColor = color(strHue, strSat, strBright, strAlpha);
        //s.setStroke(strColor);
        //s.setFill(strColor);    
      }
    }
    
  • I know that I cannot expect that this question will be answered but as I need to solve this problem next week I would appreciate a hint very much. [-O<

  • edited October 2016

    You really need to learn to make an MCVE. The reason it is hard for others to help you debug is the same reason it is hard for you to debug. Just create a simple sketch with a simple tetra, and add it. Does it compile? No? Why not?

    Tetra tetra;
    void settings() {
      size(400, 300, JAVA2D);
      noLoop();
    } 
    void setup() {
      tetra = new Tetra(this, 10);
    }
    void draw() {
      background(255);
    }
    final class Tetra {
      static PApplet p;
      int t;  
      color colorTetra;
      Tetra(PApplet pa, int t_) {
        p = pa;
        t = t_; 
        colorTetra = p.color(100,100,100, 100);
      }
    }
    

    The field p cannot be declared static in a non-static inner type, unless initialized with a constant expression

    Now go from there. I'm no PApplet expert, but working out problems with a sketc this size, you might also be able to get a bit more forum help. Clearly, when you call p.color it isn't expecting p to have any properties or methods at all. That indicates p isn't the object you think it is, and something fundamental may have gone wrong.

  • thanks jeremydouglass for your answer and sorry for my late reaction and cryptical code postings! I was working to make the sketch run with full power and dicided finally to let the "color" away. Even if I make Tetra static I cannot access the color function related to the specified PApplet.

    I will regard the MCVE standard in future.

  • @gerome -- glad that you got it working by dropping color. Good luck with the project!

Sign In or Register to comment.