#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# How to println() variables that live in a function in a class

edited February 2018

Hello,

I am trying to make following sketch where I read out an image, and create a grid of cells and parse each cell.

``````Cell[][] grid; // 2 dimensionale array van cellen { {0,0}, {0,1}, {1,1},...}
PImage theSource; //
String sImage = "im1.png"; // source image nog in te laden
int imageWidth = 1570; // breedte foto - manueel in te vullen
int imageHeight = 1112; // hoogte foto - manueel in te vullen
int blockSize = 11; // de dimensie van de cell
int cols = imageWidth/blockSize; // het aantal kolommen in de grid
int rows = imageHeight/blockSize; // het aantal rijen in de grid

void setup() {
size(1570,1112);  // moet exact overeenstemmen met de foto
grid = new Cell[cols][rows];
for (int i=0; i< cols; i++) {   // doorlopen van de cellen per kolom in de grid
for (int j=0; j< rows; j++) {   // doorlopen van de cellen per rij in de grid
grid[i][j] = new Cell(i,j,blockSize,imageWidth); // maak een cell object met een kolom en rij identificator
}
}
noLoop(); // dit is een sketch met statische output
}

void draw() {
for (int i=0; i< cols; i++) {   // doorlopen van de cellen per kolom in de grid
for (int j=0; j< rows; j++) {   // doorlopen van de cellen per rij in de grid
grid[i][j].getColorValuesForCell();  // haal de kleurwaarde voor elke pixel in de cell op en steek deze in een array
//grid[i][j].sampleCell(); // op basis van de bevindingen maak een nieuwe representatie van de cel
}
}
}
``````

Each cell is an object with a method to parse color values like this

``````class Cell {

int firstXPos;  // x positie van de eerste pixel in de cel
int firstYPos; // y positie van de eerste pixel in de cel
int canvasWidth;
int cellDimension;

Cell(int thisI, int thisJ, int thisCellSize,int thisImgWidth) {
firstXPos = thisI*thisCellSize; // de x positie is gelijk aan het kolomnummer maal de celbreedte
firstYPos = thisJ*thisCellSize; // de y positie is gelijk aan het rijnummer maal de celbreedte
canvasWidth = thisImgWidth;
cellDimension = thisCellSize;
//println(thisI, thisJ, (firstXPos+cellDimension)+(firstYPos+cellDimension)*canvasWidth, cellDimension);
}

circleContainer getColorValuesForCell() {

float kolom = 0;
float rij = 0;
float ns = 0;
float nc = 0;
float rs = 0;
float bs = 0;
float gs = 0;
float rc = 0;
float bc = 0;
float gc = 0;
float ars = 0;
float abs = 0;
float ags = 0;
float arc = 0;
float abc = 0;
float agc = 0;
float deltaR = 0;
float deltaG = 0;
float deltaB = 0;
float circle = 0;

for (int x = firstXPos; x < firstXPos+cellDimension; x++) {
for (int y = firstYPos; y < firstYPos+cellDimension; y++) {  // loop each pixel in this cell
int loc = x + y * canvasWidth; // loc identifies the pixel where we need the color info from
float r = red(theSource.pixels[loc]);  // red value
float g = green(theSource.pixels[loc]); // green value
float b = blue(theSource.pixels[loc]); // blue value

if ((x-firstXPos)<(cellDimension/3)) {
kolom = 1;
}

else if ((x-firstXPos)>(cellDimension*2/3)) {
kolom = 3;
}

else {
kolom=2;
}

if ((y-firstYPos)<(cellDimension/3)) {
rij = 1;
}

else if ((Y-firstYPos)>(cellDimension*2/3)) {
rij = 3;
}

else {
rij=2;
}

if (kolom == 1 & rij ==1 || (kolom == 3 & rij == 3) || (kolom == 1 & rij == 3) || (kolom == 3 & rij == 1)) {
// Side
rs = rs + r;
bs = bs +b;
gs = gs +g;
ns =ns +1;

}

else {
// Center
nc = nc +1;
rc = rc + r;
bc = bc +b;
gc = gc +g;
}
}
ars = rs/ns;
abs = bs/ns;
ags = gs/ns;
arc = rc/nc;
abc = bc/nc;
agc = gc/nc;
deltaR = abs(ars-arc);
deltaB = abs(abs-abc);
deltaG = abs(ags-agc);
if (deltaR > 0.2*(ars+arc)/2 || deltaB > 0.2*(abs+abc)/2 || deltaG > 0.2*(ags+ags)/2) {
circle = 1;
}

else {
circle = 0;
}
return new circleContainer(circle,deltaR, deltaG, deltaB);
}
}
}
``````

And the circleContainer class is just a container class like this:

``````class circleContainer{

float isitaCircle;
float redDelta;
float greenDelta;
float blueDelta;

circleContainer(float isitaCircle, float redDelta, float greenDelta, float blueDelta) {

this.isitaCircle = isitaCircle;
this.redDelta = redDelta;
this.greenDelta = greenDelta;
this.blueDelta = blueDelta;

}
}
``````

Now I get the error:

This method must return a result of type sketch_three.circleContainer which means that for one reason or the other this line of code is not working:

``````return new circleContainer(circle,deltaR, deltaG, deltaB);
``````

I guess the logic that goes beforehand doesn't work and therefore the return isn't going as planned so I want to trace the parameters I use in getColorValuesForCell, but whenever I add a

`println(circle,deltaR, deltaG, deltaB);`

Nothing gets printed. I guess because this is happening inside the function of a class. Now the question is if there is a way to print the parameters even if they only live inside the function in the class?

Thanks a lot for your help!!

Tim

Tagged:

• After line 105 put return null; or whatever

I think the error occurs because you return something only inside of for - loop which are not necessarily executed

• You can use println throughout

• yes, but apparently only now when I added the return null; the println() also works. Thanks a lot!!

• So now the println works I see that indeed line 39 is never executed. The reason was that ars = rs/ns; on line 87 should be one } lower (on line 90). Thanks a lot!!

• Hm, so now the class circleContainer should pass the 4 variables to the function //grid[i][j].sampleCell() on line 27 if I uncomment it. But does anyone know how to retrieve these values to be used in the sampleCell function?

If I add a println(isitaCircle); on line 27 (1st sketch) the message is The variable "isitaCircle" does not exist.

If I add a println(circleContainer); on line 27 (1st sketch) the message is The variable "circleContainer" does not exist.

So it seems that the values are not passed very well. If I add a println(circle, deltaR, deltaG, deltaB); on line 106 of sketch 2 it prints the variables very well, so one would say that theu are passed via the return function in line 107.

Tim

• edited February 2018 Answer ✓

variables inside the class are known only inside the class.

That's good because so they classes are isolated against each other (in fact even the variables of the different objects of one class are isolated) - otherwise we wouldn't know which asteroid or so we mean if we have 100 asteroids.

Also, your circleContainer is quite abstract, since it's only the return type of one Cell.

You wrote:

If I add a println(isitaCircle); on line 27 (1st sketch) the message is The variable "isitaCircle" does not exist.

Try

``````circleContainer cc = grid[i][j].getColorValuesForCell();  // get return type
println (cc.isitaCircle);
``````

You wrote:

If I add a println(circleContainer); on line 27 (1st sketch) the message is The variable "circleContainer" does not exist.

That won't work since circleContainer has many values.

Try:

``````circleContainer cc = grid[i][j].getColorValuesForCell();  // get return type
println (cc);  // wouldn't work really I guess
println (cc.isitaCircle);
println (cc.redDelta);
``````

Remark I

to println something (in `draw()`) from a cell use (e.g. after line 27 in first sketch):

``````println(grid[i][j].firstXPos);  // again with the dot . being the separator between object and variable
``````

Remark II

Please post your code in one go even when you have it in different classes or tabs...

• Thank you very much for this clear and simple explanation! It works now.

• Great!