#### Howdy, Stranger!

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

# How to display element# of ArrayList as text

edited June 2017

Hello - New to processing. I have been following some YouTube tutorial videos by Daniel Shiffman @ Coding Train. I really liked his video on the Menger Sponge Fractal. I going to fold in some stuff about primes with his code from the video. Before I get there, I want to display the element# of an Array as text. Am a little confused with ArrayLists and can't get the syntax quite right. Any help would be great. To re-iterate, there is a 3d rotating series of boxes drawn, and they are all part of the ArrayList. I want to display with text, in each box the element position of that box in the box.

just showing the code on 1 of the two files (from Daniel Shiffman), as it is in the correct place (afaik)...

text('hi', 0,0,0); // this will have each 3-D box have 'hi' in the center. I don't know what to replace 'hi' with in order to display the element position of the ArrayList. The text function wants an int value in this position. Conflating this, the code is using push/pop which I am not too familiar with.

``````class Box {
PVector pos;
float r;

Box(float x, float y, float z, float r_) {
pos = new PVector(x, y, z);
r = r_;
}

ArrayList<Box> generate() {
ArrayList<Box> boxes = new ArrayList<Box>();
for (int x = -1; x < 2; x++) {
for (int y = -1; y < 2; y++) {
for (int z = -1; z < 2; z++) {
float newR = r/3;

Box b = new Box(pos.x + x*newR,
pos.y + y*newR,
pos.z + z*newR,
newR);
int sum = abs(x) + abs(y) + abs(z);
if (sum > 1) {
}
}
}
}
return boxes;
}

void show() {
pushMatrix();
translate(pos.x, pos.y, pos.z);
text(sponge.Box, 0,0,0); // this is where I need help. sponge.Box is incorrect
noStroke();
fill(255, 100);
box(r);
popMatrix();
}
}
``````
Tagged:

• edited June 2017

You want to display some text, so you need to use the `text()` function.

The `text()` function takes some parameters to know what to do.

The first parameter is the text to display, and should be an expression that evaluates to a String.

The text you want to display is the position of the Box object. The Box object is storing its position in the PVector `pos`. You can verify this immediately because the box is being drawn at the position (0,0,0) after a call to `translate()` is moving the origin to that point. Thus, the numbers you want to display are the x, y, and z components of the PVector `pos`.

You can access these components directly, as you have done so in the call to `translate()`: `pos.x`, `pos.y`, and `pos.z`.

Since you want an expression that evaluates to a string, start with a blank string: `""`, and append things to it using the string concatenation operator: `+`.

Make sure to put some spaces in between your numbers! These spaces should also be strings!

`text( "" + pos.x + " " + pos.y + " " + pos.z , 0, 0, 0);`

• Hi TfGuy44 -

Thank you for reading and attempting to help me with my problem. Unfortunately, the coordinates of the box are not what I am interested in. I am interested in what is the position of each element in the Array. Said differently, an array holds many objects, in some sort of order. I am interested in that order. One of these boxes is box #1, box #2, box #3, etc. I want these boxes labeled with 1, 2, 3, etc. I don't need the coordinates of box 1, 2, 3, etc.

Thanks, Nick

• edited June 2017
• edited June 2017

@Nicho247 -- the confusion in part is the way you are using the word "position," which you later clarify as "order." Clearer to say that you want the index of each element in the array (or ArrayList, or List, etc.). Using "index" will make it easier for others to understand and help you -- and easier to look up functions yourself. For example, notice in IntList how get, set, and remove all take an "index" as an argument.

https://processing.org/reference/IntList.html

• Make int index part of your class, fill it in the constructor with boxes.size()-1

Show index with text()

Works as long as you don't delete

Not tested

• ``````import peasy.*;
import peasy.org.apache.commons.math.*;
import peasy.org.apache.commons.math.geometry.*;

ArrayList<Box> boxes = new ArrayList();
PeasyCam cam ;

void setup() {
size(900, 900, P3D);
cam = new PeasyCam(this, 0, 0, 0, 500);
boxes=generate();
}

void draw() {
background(0);
// lights();
for (int i = 0; i<boxes.size(); i++) {
//for (int i = 0; i<1; i++) {
boxes.get(i).show();
}
}

// ------------------------------------------------------

ArrayList<Box> generate() {

float r = 67;
ArrayList<Box> boxes = new ArrayList<Box>();
PVector pos=new PVector();

for (int x = -1; x < 2; x++) {
for (int y = -1; y < 2; y++) {
for (int z = -1; z < 2; z++) {

float newR = r/3;

Box b = new Box(pos.x + x*newR,
pos.y + y*newR,
pos.z + z*newR,
newR,
boxes.size());

int sum = abs(x) + abs(y) + abs(z);

if (sum > 1) {
}
}
}
}
return boxes;
}

// ========================================================

class Box {

PVector pos;
float r;
int index;

Box(float x, float y, float z,
float r_,
int index_) {
pos = new PVector(x, y, z);
r = r_;
index = index_;     //index = boxes.size();
}

void show() {
pushMatrix();
translate(pos.x, pos.y, pos.z);

pushMatrix(); // store Matrix
fill(255, 2, 2); //red
textAlign(CENTER, CENTER); // alignment
textMode(SHAPE);           // looks much better
text(index, 0, 0, 0); // this is new
popMatrix(); // restore Matrix

// how to draw the box
if (keyPressed) {
noStroke();
fill(255, 100);
} else {
noFill();
stroke(255);
}

box(r);

popMatrix();
}
}//class
//
``````
• I think generate() should not be inside the class, since it works on the list of all boxes

I made a menger sponge now from it, so come back for more help.