Advertisement
Guest User

create random two color block

a guest
Sep 14th, 2011
562
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. //create a random two color block, define block sizes, number of rows, numbers of columns, define colors
  2. //by fbz
  3.  
  4.  
  5. Cell[][] grid;
  6.  
  7. int numPixels;
  8. int blockSize = 4;
  9. int cols = 10;
  10. int rows = 4;
  11.  
  12.  
  13. void setup() {
  14. noLoop();
  15. size(cols*blockSize,rows*blockSize);
  16. grid = new Cell[cols][rows];
  17. for (int i = 0; i < cols; i++) {
  18. for (int j = 0; j < rows; j++) {
  19. grid[i][j] = new Cell(i*blockSize,j*blockSize,blockSize,blockSize,i+j);
  20. }
  21. }
  22. }
  23.  
  24. void draw() {
  25. background(0);
  26. for (int i = 0; i < cols; i++) {
  27. for (int j = 0; j < rows; j++) {
  28. // display each object
  29. grid[i][j].display();
  30. }
  31. }
  32. }
  33.  
  34. // A Cell object
  35. class Cell {
  36. // A cell object knows about its location in the grid as well as its size with the variables x,y,w,h.
  37. float x,y; // x,y location
  38. float w,h; // width and height
  39. //float c; // color
  40.  
  41. // Cell Constructor
  42. Cell(float tempX, float tempY, float tempW, float tempH, float tempColor) {
  43. x = tempX;
  44. y = tempY;
  45. w = tempW;
  46. h = tempH;
  47.  
  48. }
  49. void display() {
  50.  
  51. noStroke();
  52.  
  53. color[] palette=new color[5];
  54. palette[0]=color(40,245,243); //cyan
  55. palette[1]=color(0,0,0); //black
  56. palette[2]=color(255,255,255); //white
  57. palette[3]=color(206,0,76); //hot pink
  58. palette[4]=color(118,242,100); //green
  59.  
  60.  
  61. fill(palette[int(random(1,3))]);
  62.  
  63.  
  64.  
  65. rect(x,y,w,h);
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement