how use "text()" in a array grid.

edited July 2016 in Questions about Code

i can not print by "text()" function random number on every cells grid. the numbers only appear in the 2 tops cells.

Cell cells;

void  setup() {
  size(500, 500);
  cells=new Cell();
  cells.inicializingCells();
}
void draw() {
  cells.displayCells();
  cells.textOnCell();
}


class Cell {
  int x, y;
  int w, h;
  int cols=2;
  int rows=2;
  int [][] cellBoard;
  Cell() {
    w=width/2;
    h=height/2;
    x=width/2;
    y=height/2;
    cellBoard= new int[cols][rows];
  }

  void inicializingCells() {
    for (int i=0; i<cols; i++) {
      for (int j=0; j<rows; j++) {
        cellBoard[i][j]= int (random(2));
        println(cellBoard[i][j]);     //i use println to show the numbers. 
      }
    }
  }

  void displayCells() {
    for (int i=0; i<cols; i++) {
      for (int j=0; j<rows; j++) {
        noFill();
        rect(i*x, j*y, w, h);
      }
    }
  }

  void textOnCell() {
    for (int i=0; i<cols; i++) {
      for (int j=0; j<rows; j++) {
        fill(0);
        textSize(32);
        text(cellBoard[i][j], x*i, y*j);// with this way i can not print the numbers on the bottom cells.
      }
    }
  }
}
Tagged:

Answers

  • edited July 2016

    first question .thanks

  • Answer ✓

    In this setup, the text is ending up printing above the top-left corner of each cell, and so the top row is being printed off-screen.

    text(cellBoard[i][j], x*i, (y*j)+y);

    should fix it

  • Answer ✓

    Hm...

    The class name is Cell but it should be Grid or Board

    Now you can call inicialisingCell from the constructor instead of from setup()

    displayCells and textOnCells I'd move into one function that does both

    You calculate the Position of all cells every time

    To avoid this you could make a class Cell that holds the properties of one Cell

    You would call this from class Grid and the Array cellboard could be of this type Cell

Sign In or Register to comment.