How to print an ArrayList in processing and how to update the background

941 views Asked by At
ArrayList<Cell> cells;
int j=50;

void setup() {
  size(500, 500);
  cells=new ArrayList<Cell>();
  int q=int(random(1, 5)); //red fill value //fill *51
  for (int i=0; i<1; i++) {
    cells.add(new Cell(0, 0, 15, color(q*51, 0, 0), 100, 50)); //adds a new cell for each iteration
    j+=50;
    println(q+" "+i); //prints value of the red and then iteration of the for loop

  }
}

  void draw() {
    printArray(cells); //prints my array list but it is not working prints "[shades_game$Cell@6ec30aea]"

    for (int i=0; i<cells.size (); i++) {
      background(255); //supposed to remove all of the other square trail
      cells.get(i).display(); //displays each rectangle
    }
  }

  class Cell {
    private int x;
    private int y;
    private int speed;
    private color c;
    private int wwidth;
    private int hheight;
    public Cell(int xpos, int ypos, int speedy, color col, int widthh, int heightt) { // then set all of the private vars to these public ones
      x=xpos;
      y=ypos;
      speed=speedy;
      c=col;
      wwidth=widthh;
      hheight=heightt;
    }
    void display() {
      fill(c);
      rectMode(CORNERS);
      rect(x, y, wwidth, hheight);
      if (y<=height+hheight) { //if y location of the rect is less than the window then increase y pos
        y+=speed; //increase y by speed
      }
      println(y+" "+speed);
    }
  }

I want to not have the rectangle trail following the rectangle. I also need to print the cells ArrayList but String s: cells println(s) isn't working... Can someone help me? I want the rectangle to land at the bottom and sit there as it is doing but, without the long line of rectangles following it. Any help would be greatly appreciated.

Edit:easier to work

2

There are 2 answers

0
JeffThompson On

You should probably split your question into separate ones, or head over to the Processing forum for more in-depth help.

Print from ArrayList
To print a cell from the array list, you iterate the list and access the parts of the Cell class you want to print. For example:

for (Cell c : cells) {
  println("Position: " + c.x + "," + c.y);
}

Stop rectangle at bottom
Your problem is the rectMode(CORNERS) which is making the rectangle longer and longer. Remove that line and change your if statement to be if (y <= height-hheight) and it works as expected.

0
v.k. On

For the print part, you could also override the toString method in the class.

like:

ArrayList<Cell> cells;
int j=50;

void setup() {
  size(500, 500);
  cells=new ArrayList<Cell>();
  int q=int(random(1, 5)); //red fill value //fill *51
  for (int i=0; i<1; i++) {
    cells.add(new Cell(0, 0, 15, color(q*51, 0, 0), 100, 50)); //adds a new cell for each iteration
    j+=50;
    println(q+" "+i); //prints value of the red and then iteration of the for loop
  }
}

void draw() {
  printArray(cells); //prints my array list but it is not working prints "[shades_game$Cell@6ec30aea]"

  for (int i=0; i<cells.size (); i++) {
    background(255); //supposed to remove all of the other square trail
    cells.get(i).display(); //displays each rectangle
  }
}

class Cell {
  private int x;
  private int y;
  private int speed;
  private color c;
  private int wwidth;
  private int hheight;
  public Cell(int xpos, int ypos, int speedy, color col, int widthh, int heightt) { // then set all of the private vars to these public ones
    x=xpos;
    y=ypos;
    speed=speedy;
    c=col;
    wwidth=widthh;
    hheight=heightt;
  }
  void display() {
    fill(c);
    rect(x, y, wwidth, hheight);
    if (y<=height+hheight) { //if y location of the rect is less than the window then increase y pos
      y+=speed; //increase y by speed
    }
  }

  @Override
    String toString() {
    String s = "\nbuild your string...\n";
    s+= "like:\nx = " + x + "\n";
    s+= "speed = " + speed + "\n";
    s+= "and so on...\n";
    return s;
  }
}