Loading...
Logo
Processing Forum
Hello,
i am completely new to processing 

can someone please help me with this code ,

I want to add a background image to it, the background image would be placed in the processing folder/data




Copy code
  1. import processing.serial.*;
  2. import processing.opengl.*;

  3. Serial serial;
  4. int serialPort = 1; 
  5.               
  6. int sen = 3; 
  7. int div = 3;
  8. Normalize n[] = new Normalize[sen];
  9. MomentumAverage cama[] = new MomentumAverage[sen];
  10. MomentumAverage axyz[] = new MomentumAverage[sen];
  11. float[] nxyz = new float[sen];
  12. int[] ixyz = new int[sen];

  13. float w = 256; // board size
  14. boolean[] flip = {
  15.   false, true, false};

  16. int player = 0;
  17. boolean moves[][][][];

  18. PFont font;

  19. void setup() {
  20.   size(800, 600, P3D);
  21.   frameRate(25);
  22.   
  23.   font = loadFont("TrebuchetMS-Italic-20.vlw");
  24.   textFont(font);
  25.   textMode(SCREEN);
  26.   
  27.   println(Serial.list());
  28.   serial = new Serial(this, Serial.list()[serialPort], 115200);
  29.   
  30.   for(int i = 0; i < sen; i++) {
  31.     n[i] = new Normalize();
  32.     cama[i] = new MomentumAverage(.01);
  33.     axyz[i] = new MomentumAverage(.15);
  34.   }
  35.   
  36.   reset();
  37. }

  38. void draw() {
  39.   updateSerial();
  40.   drawBoard();
  41. }

  42. void updateSerial() {
  43.   String cur = serial.readStringUntil('\n');
  44.   if(cur != null) {
  45.     String[] parts = split(cur, " ");
  46.     if(parts.length == sen  ) {
  47.       float[] xyz = new float[sen];
  48.       for(int i = 0; i < sen; i++)
  49.         xyz[i] = float(parts[i]);
  50.   
  51.       if(mousePressed && mouseButton == LEFT)
  52.         for(int i = 0; i < sen; i++)
  53.           n[i].note(xyz[i]);
  
     .......... the code continues 





I check online for the syntax ,even though i found them i cant seem to incorporate it into the above code.
so can someone kindly modify the above code with the right parameters for adding a background image 

any form of help would be much appreciated 

Replies(8)

Have you looked at background() this is probably the easiest way to do it especially since you are using a 3D renderer.
i tried stuffs like 
               
Copy code
  1. PImage b;
  2. void setup(){
  3. ...
  4. b = loadImage("fondo_fade.png");
  5. ...
  6. }
  7. void draw(){
  8. image(b, 0, 0);
  9. ...
  10. }
but i keep getting errors , like i said i am new to processing
could u be more specific
Change draw() to

Copy code
  1. void draw(){
  2.       background(b);
  3.       // ...
  4. }

The only restriction is that the image MUST be the same size as the skecth canvas (as speciified in the size() method call)
I tried what you suggested but i got the error " it looks like you'r e mixing active with static modes" and the "background(b);" gets highlighted at line 51 .

i did resize the image to 800x600


Copy code
  1. import processing.serial.*;
  2. import processing.opengl.*;

  3. Serial serial;
  4. int serialPort = 1;   // << Set this to be the serial port of your Arduino - ie if you have 3 ports : COM1, COM2, COM3 
  5.                       // and your Arduino is on COM2 you should set this to '1' - since the array is 0 based
  6.               
  7. int sen = 3; // sensors
  8. int div = 3; // board sub divisions

  9. Normalize n[] = new Normalize[sen];
  10. MomentumAverage cama[] = new MomentumAverage[sen];
  11. MomentumAverage axyz[] = new MomentumAverage[sen];
  12. float[] nxyz = new float[sen];
  13. int[] ixyz = new int[sen];

  14. float w = 256; // board size
  15. boolean[] flip = {
  16.   false, true, false};

  17. int player = 0;
  18. boolean moves[][][][];

  19. PFont font;
  20. PImage b;
  21. void setup() {
  22.   size(800, 600, P3D);
  23.   frameRate(25);
  24.   b = loadImage("data/1.jpg");
  25.   font = loadFont("TrebuchetMS-Italic-20.vlw");
  26.   textFont(font);
  27.   textMode(SCREEN);
  28.   
  29.   println(Serial.list());
  30.   serial = new Serial(this, Serial.list()[serialPort], 115200);
  31.   
  32.   for(int i = 0; i < sen; i++) {
  33.     n[i] = new Normalize();
  34.     cama[i] = new MomentumAverage(.01);
  35.     axyz[i] = new MomentumAverage(.15);
  36.   }
  37.   
  38.   reset();
  39. }

  40. void draw() {
  41.   updateSerial();
  42.   drawBoard();
  43. }
  44. background(b);
  45. void updateSerial() {
  46.   String cur = serial.readStringUntil('\n');
  47.   if(cur != null) {
  48.     String[] parts = split(cur, " ");
  49.     if(parts.length == sen  ) {
  50.       float[] xyz = new float[sen];
  51.       for(int i = 0; i < sen; i++)
  52.         xyz[i] = float(parts[i]);
  53.   
  54.       if(mousePressed && mouseButton == LEFT)
  55.         for(int i = 0; i < sen; i++)
  56.           n[i].note(xyz[i]);
  57.   
  58.       nxyz = new float[sen];
  59.       for(int i = 0; i < sen; i++) {
  60.         float raw = n[i].choose(xyz[i]);
  61.         nxyz[i] = flip[i] ? 1 - raw : raw;
  62.         cama[i].note(nxyz[i]);
  63.         axyz[i].note(nxyz[i]);
  64.         ixyz[i] = getPosition(axyz[i].avg);
  65.       }
  66.     }
  67.   }
  68. }
  69. }
  70. float cutoff = .2;
  71. int getPosition(float x) {
  72.   if(div == 3) {
  73.     if(x < cutoff)
  74.       return 0;
  75.     if(x < 1 - cutoff)
  76.       return 1;
  77.     else
  78.       return 2;
  79.   } 
  80.   else {
  81.     return x == 1 ? div - 1 : (int) x * div;
  82.   }
  83. }

  84. void drawBoard() {
  85.   background(255);

  86.   float h = w / 2;
  87.   camera(
  88.     h + (cama[0].avg - cama[2].avg) * h,
  89.     h + (cama[1].avg - 1) * height / 2,
  90.     w * 2,
  91.     h, h, h,
  92.     0, 1, 0);

  93.   pushMatrix();
  94.   noStroke();
  95.   fill(0, 40);
  96.   translate(w/2, w/2, w/2);
  97.   rotateY(-HALF_PI/2);
  98.   box(w);
  99.   popMatrix();

  100.   float sw = w / div;
  101.   translate(h, sw / 2, 0);
  102.   rotateY(-HALF_PI/2);

  103.   pushMatrix();
  104.   float sd = sw * (div - 1);
  105.   translate(
  106.     axyz[0].avg * sd,
  107.     axyz[1].avg * sd,
  108.     axyz[2].avg * sd);
  109.   fill(255, 160, 0);
  110.   noStroke();
  111.   sphere(18);
  112.   popMatrix();

  113.   for(int z = 0; z < div; z++) {
  114.     for(int y = 0; y < div; y++) {
  115.       for(int x = 0; x < div; x++) {
  116.         pushMatrix();
  117.         translate(x * sw, y * sw, z * sw);

  118.         noStroke();
  119.         if(moves[0][x][y][z])
  120.           fill(255, 0, 0, 200);
  121.         else if(moves[1][x][y][z])
  122.           fill(0, 0, 255, 200);
  123.         else if(
  124.         x == ixyz[0] &&
  125.           y == ixyz[1] &&
  126.           z == ixyz[2])
  127.           if(player == 0)
  128.             fill(255, 0, 0, 200);
  129.           else
  130.             fill(0, 0, 255, 200);
  131.         else
  132.           fill(0, 100);
  133.         box(sw / 3);

  134.         popMatrix();
  135.       }
  136.     }
  137.   }
  138.   
  139.   fill(0);
  140.   if(mousePressed && mouseButton == LEFT)
  141.     msg("defining boundaries");
  142. }

  143. void keyPressed() {
  144.   if(key == TAB) {
  145.     moves[player][ixyz[0]][ixyz[1]][ixyz[2]] = true;
  146.     player = player == 0 ? 1 : 0;
  147.   }
  148. }

  149. void mousePressed() {
  150.   if(mouseButton == RIGHT)
  151.     reset();
  152. }

  153. void reset() {
  154.   moves = new boolean[2][div][div][div];
  155.   for(int i = 0; i < sen; i++) {
  156.     n[i].reset();
  157.     cama[i].reset();
  158.     axyz[i].reset();
  159.   }
  160. }

  161. void msg(String msg) {
  162.   text(msg, 10, height - 10);
  163. }
could you please help me out here ? 
I am new to these, we are using it for our semester project 
Line 50 is the end of your draw() function. Your call to background() on line 51 is not inside any function!
You should replace line 93 with line 50, and then remove line 50.
It seems @ line # 51 -> background(b);
it is outside of any function scope.

AFAIK, it shoulda been the 1st thing inside function draw()!

P.S.: In these posts:
http://forum.processing.org/topic/deleted-forum-processing-org-topic-traffic-light-functions-help
http://forum.processing.org/topic/animation-erasing-and-keeping-method

There are some tricks there to turn a loaded image scaled to canvas dimensions in setup(),
then, ready to be used for background() within draw()!

That would be useful if you hadn't pre-scaled your background image outside Processing.  
In your code above line 51 is in the wrong place it should be in the draw() method so move it so it looks like this

Copy code
  1. void draw() {
  2.   background(b);
  3.   updateSerial();
  4.   drawBoard();
  5. }
Also you should remove the following line (93) from the drawBoard() method
  background(255);

since it will set the background to white, replacing your image.

Thanks guys !!!!!!! it worked