Drawing onto canvas from array

edited March 2015 in Library Questions

Hello,

I have a 2D array of values, which I've drawn using rectangles. I'd like to be able to draw them onto a canvas (so I can show/hide them by clicking a button). I've worked out how to draw onto a canvas (from the ControlP5 example), I'm not sure about syntax for adding tables/arrays. I've tried inserting the "table" code from the setup into the class setup, and declaring the array and colours in the class as well, but I just get errors. Any help is appreciated!

Here is my code:

import controlP5.*;

ControlP5 cp5;

Canvas cc;

final String CSVb = "file.csv";
final int A = 0, B = 1, TYPES[] = {
 Table.INT, Table.INT
 };

color col1 = color(127,201,127);
color col2 = color(190,174,212);
color col3 = color(253,192,134);  

 int[][] myArray;

class MyCanvas extends Canvas {
  public void draw(PApplet p) { 
   p.fill(100);
   p.rect(250,10,1040,630); 
  }
}

void setup() {
  size(1300, 650, P2D);
  cp5 = new ControlP5(this);

 Table t = loadTable(CSVb, "header");    
 int idx = 0, lines = t.getRowCount();
 t.setColumnTypes(TYPES);

 myArray = new int[linesB][2];

 for (TableRow tr : t.rows ()) {
myArray[idx][A] = (int) tBr.getInt("field1");
myArray[idx][B] = (int) tBr.getInt("field2");
 }

  cc = new MyCanvas();
  cc.pre(); // use cc.post(); to draw on top of existing controllers.
  cp5.addCanvas(cc); // add the canvas to cp5

}


void draw() {
  background(0);
 noStroke();

  for (int i = 0; i < myArray.length; i++) {
    for (int j = 0; j < myArray[i].length; j++) {

      switch( myArray[i][j] ) {
      case 0: 
        noFill();
        break;
      case 1:    
        fill(col1); 
        rect(270 + (j*31), 15 + (i*12), 30, 10);    
        break; 
      case 2: 
        fill(col2);
        rect(270 + (j*31), 15 + (i*12), 30, 10);
        break; 
      case 3:
        fill(col3);
        rect(270 + (j*31), 15 + (i*12), 30, 10);
        break; 
      default:
        println("error");
      }
    }
  }
}
Tagged:
Sign In or Register to comment.