We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › How to insert a background image
Page Index Toggle Pages: 1
How to insert a background image ? (Read 1195 times)
How to insert a background image ?
Dec 30th, 2008, 11:38pm
 
I want to inset a background image to my sketch , i have readen the  http://processing.org/reference/image_.html

and i have tried to implement it but it says it cannot find anything named "b"

i typed this after the void draw function, and the image is in the sketch folder :


 b = loadImage("fondo_fade.png");
image(b, 0, 0);


Here the whole code:
Code:
/* puff
Ira Greenberg, October 22, 2005 */
// for puff head
float headX;
float headY;
float speedX = .7;
float speedY = .9;
// for puff body
int cells = 100;
float[]px= new float[cells];
float[]py= new float[cells];
float[]radiiX = new float[cells];
float[]radiiY = new float[cells];
float[]angle = new float[cells];
float[]frequency = new float[cells];
float[]cellRadius = new float[cells];



void setup(){

size(400,400);
// begin in the center
headX = width/2;
headY = height/2;
//fill body arrays
for (int i=0; i< cells; i++){
radiiX[i] = random(-7,7);
radiiY[i] = random(-4,15);
frequency[i]= random(-9, 9);
cellRadius[i] = random(1, 100);
}
frameRate(30);
}


 
void draw(){
 
 
 b = loadImage("fondo_fade.png");
image(b, 0, 0);


//background(#FF006F);
//fill (#FF0842,20);
//rect (0,0,width,height);





noStroke();
smooth();
fill(255, 255, 255);
//follow the leader
for (int i =0; i< cells; i++){
if (i==0){
px[i] = headX+sin(radians(angle[i]))*radiiX[i];
py[i] = headY+cos(radians(angle[i]))*radiiY[i];
}
else{
px[i] = px[i-1]+cos(radians(angle[i]))*radiiX[i];
py[i] = py[i-1]+sin(radians(angle[i]))*radiiY[i];
//check collision of body
if (px[i] >= width-cellRadius[i]/2 || px[i] <= cellRadius[i]/2){
radiiX[i]*=-1;
cellRadius[i] = random(1, 40);
frequency[i]= random(-3, 13);
}
if (py[i] >= height-cellRadius[i]/2 || py[i] <= cellRadius[i]/2){
radiiY[i]*=-1;
cellRadius[i] = random(1, 40);
frequency[i]= random(-9, 9);
}
}
// draw puff
ellipse(px[i], py[i], cellRadius[i], cellRadius[i]);
// set speed of body
angle[i]+=frequency[i];
}
// set velocity of head
headX+=speedX;
headY+=speedY;
//check boundary collision of head
if (headX >= width-cellRadius[0]/2 || headX <=cellRadius[0]/2){
speedX*=-1;
}
if (headY >= height-cellRadius[0]/2 || headY <= cellRadius[0]/2){
speedY*=-1;
}
}
Re: How to insert a background image ?
Reply #1 - Dec 30th, 2008, 11:47pm
 
got it, had to put

PImage b;



at the beggining.

But now when i run the sketch it runs much more less smoother, ithink it would run smoother if the radial was self generated in processing rather than an imported image.

But i dont know how to do color gradients, does anyone know how to do this , or could drop me a code to do this kind of spehric color grading ?

Thanx!
Re: How to insert a background image ?
Reply #2 - Dec 31st, 2008, 10:56am
 
It runs slower because you load the image at every frame.
You only need to load the image once... in the setup function.

Then in the draw function you display it.

Quick recap

Code:

PImage b;
void setup(){
...
b = loadImage("fondo_fade.png");
...
}

void draw(){
image(b, 0, 0);
...
}


Cheers
Page Index Toggle Pages: 1