Coloring in a grid based on its cells' properties

edited December 2017 in Questions about Code

Hi all, I'm taking a digital poetry class and one of our final projects is to make something with Processing, which we were just introduced to the other week. Inspired by Game of Life, I want to make a grid filled with personality traits randomly-generated from a list - some positive, some negative - and have them change based on their neighbors.

I want a color scheme to help the user visualize the changes. I created the "state" variable to represent "positive or negative" and tried to set something up where positive traits are green and negative traits are red. The program is correctly assigning positive traits a state of 1 and negative traits a state of 0 (as you can see, I asked the program to print both the trait and its associated state in the cell, for testing purposes). However, it is failing to assign them a color that corresponds with the state. Any ideas why? I'm doing this in Processing 3.0, by the way.

int cols = 25; 
int rows = 26; 
float height = 20; 
float width = 60;  
color alive = color(0,200,0);
color dead = color(200,0,0); 

Cell[][] grid; 
String[] dwarfs = new String[]{"sneezy", "healthy", "sleepy", "energized", "grumpy", "happy", "dopey", "sharp", "bashful", "confident"};
String[] living = new String[]{"healthy", "energized", "happy", "sharp", "confident"};
String[] deceased = new String[]{"sneezy", "sleepy", "grumpy", "dopey", "bashful"};

void setup() {
  grid = new Cell[cols][rows];
  size(1500,500);
  for (int i = 0; i < cols; i++) {
    for (int j = 0; j < rows; j++) {
      // Initialize each object
      int rand = int(random(0,10)); 
      String t = dwarfs[rand]; 
      int s = 2; 
      for(int k=0; k<5; k++) {
        if (deceased[k].equals(t)){
          s = 0; 
          k = 5; 
        } //if
        else{
         s = 1; 
        } //else
      } //for deceased
      grid[i][j] = new Cell(i*width,j*height,width,height,t, s);
    } //for j
  } //for i
} //setup

// A Cell object
class Cell {
  // A cell object knows about its location in the grid 
  // as well as its size with the variables x,y,w,h
  float x,y;   // x,y location
  float w,h;   // width and height
  String t; //text
  int s; //state

  // Cell Constructor
  Cell(float tempX, float tempY, float tempW, float tempH, String tempT, int tempS) {
    x = tempX;
    y = tempY;
    w = tempW;
    h = tempH;
    t = tempT;
    s = tempS; 
  } //Constructor
} //class

void draw() {
  background(0);
  // The counter variables i and j are also the column and row numbers and 
  // are used as arguments to the constructor for each object in the grid.  
  for (int i = 0; i < cols; i++) {
    for (int j = 0; j < rows; j++) {
      rect(grid[i][j].x,grid[i][j].y,grid[i][j].w,grid[i][j].h); 
      stroke(255);
      fill(255); 
      text(grid[i][j].t+grid[i][j].s,grid[i][j].x+5,grid[i][j].y-5); 
      if (grid[i][j].s==1) {
        fill(alive);
      }  //if
      else {
        fill(dead); 
      }
    } //for
  } //for
} //draw

Answers

  • How to post in the processing forum.

    You can edit your post (click the small gear and the 'Edit').

    How to format Code:

    empty line before and after the code section
    
    select (highlight) entire code with the mouse
    
    hit ctrl-o OR click the small C in the command bar
    
  • Thanks; looks like it's much clearer to read now!

  • Do you want to change the text color or the rect color?

    you need to use fill before rect

  • Hm. The idea of a class is to move functions into the class too

    your draw could be:

      for (int i = 0; i < cols; i++) {
        for (int j = 0; j < rows; j++) {
          grid[i][j].display(); 
       }
      }
    

    when you make display() a function of the class

  • edited December 2017

    Thanks; I made display a method of the Cell class and moved the rect to the end (I want the text and the rectangle's borders to be white, and the rectangle itself filled in with either green or red. However, I'm still getting the same problem of coloring not matching s with the code below.

    int cols = 25; 
    int rows = 26; 
    float height = 20; 
    float width = 60;  
    color alive = color(0,200,0);
    color dead = color(200,0,0); 
    
    Cell[][] grid; 
    String[] dwarfs = new String[]{"sneezy", "healthy", "sleepy", "energized", "grumpy", "happy", "dopey", "sharp", "bashful", "confident"};
    String[] living = new String[]{"healthy", "energized", "happy", "sharp", "confident"};
    String[] deceased = new String[]{"sneezy", "sleepy", "grumpy", "dopey", "bashful"};
    
    void setup() {
      grid = new Cell[cols][rows];
      size(1500,500);
      for (int i = 0; i < cols; i++) {
        for (int j = 0; j < rows; j++) {
          // Initialize each object
          int rand = int(random(0,10)); 
          String t = dwarfs[rand]; 
          int s = 2; 
          for(int k=0; k<5; k++) {
            if (deceased[k].equals(t)){
              s = 0; 
              k = 5; 
            } //if
            else{
             s = 1; 
            } //else
          } //for deceased
          grid[i][j] = new Cell(i*width,j*height,width,height,t, s);
        } //for j
      } //for i
    } //setup
    
    // A Cell object
    class Cell {
      // A cell object knows about its location in the grid 
      // as well as its size with the variables x,y,w,h
      float x,y;   // x,y location
      float w,h;   // width and height
      String t; //text
      int s; //state
    
      // Cell Constructor
      Cell(float tempX, float tempY, float tempW, float tempH, String tempT, int tempS) {
        x = tempX;
        y = tempY;
        w = tempW;
        h = tempH;
        t = tempT;
        s = tempS; 
      } //Constructor
    
      //display method
    void display() {
          fill(255); 
          stroke(255); 
          text(t+s,x+5,y-5); 
          if (s==1) {
            fill(alive);
          }  //if
          else {
            fill(dead); 
          }
          rect(x,y,w,h); 
    }
    } //class
    
    void draw() {
      background(0);
      // The counter variables i and j are also the column and row numbers and 
      // are used as arguments to the constructor for each object in the grid.  
      for (int i = 0; i < cols; i++) {
        for (int j = 0; j < rows; j++) {
          grid[i][j].display(); 
        } //for
      } //for
    } //draw
    
Sign In or Register to comment.