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 › simple image changer problem
Page Index Toggle Pages: 1
simple image changer problem (Read 441 times)
simple image changer problem
Aug 17th, 2008, 10:59pm
 
I have only just started using processing so everythings still a bit confusing, which means that i have probably made a really obvious mistake.  But anyway.

I am trying to build an "image changer" that displays a sequence of images, the position in this sequence determined by the horizontal position of the mouse.  
The images will be shown in a chronological order, so this code should allow the user to make time run back and forth by moving the mouse. (art project basically)

Here is what i have pieced together so far:

 int horizsize = 600;
 int numImage = 2;
 int imnum = (mouseX/(horizsize/numImage));
 PImage[] pic = new PImage[numImage];
 
void setup()

{
pic[0] = loadImage("0.jpg");
pic[1]= loadImage("1.jpg");

 loop();
 size(horizsize,400);
}

void draw() {
   image(pic[imnum], 0, 0);
   noCursor();
}

for the sake of simplicity, at the moment i am only using 2 images.  
It all loads alright, but the picture doesnt change when the cursor is moved around.  

What's going wrong?

Thanks
Re: simple image changer problem
Reply #1 - Aug 18th, 2008, 12:22am
 
You don't recompute imnum in draw()...
So its value is always the same through all the life of the program.
Re: simple image changer problem
Reply #2 - Aug 18th, 2008, 1:42pm
 
I undertand what you are saying, the picture does not update.

Is the peice of code i use loop() ?

and where should i put it in order to make processing recompute imnum?


edit:

I have got it working, buy specifying imnum inside void draw()

Thanks for your help
Re: simple image changer problem
Reply #3 - Aug 26th, 2008, 12:48am
 
What PhiLho is saying is that
Code:

int imnum = (mouseX/(horizsize/numImage));


is calculated once, not continuously. You need to set that formula inside draw, not as part of the int variable initialization.

Code:

int horizsize = 600;
int numImage = 2;
int imnum = 0;
PImage[] pic = new PImage[numImage];

void setup()

{
pic[0] = loadImage("0.jpg");
pic[1]= loadImage("1.jpg");

loop();
size(horizsize,400);
}

void draw() {
imnum = (mouseX/(horizsize/numImage));
image(pic[imnum], 0, 0);
noCursor();
}


which is only illustrative as an example.
Re: simple image changer problem
Reply #4 - Aug 26th, 2008, 1:21am
 
yeah thats what i did in the end, i left imnum without a value at the beggining, then calculated it within draw.

i have it working,  (100+ images) , but i have a question.  is a redraw() necessary at the end of the draw()?

ie


void draw() {
 imnum = (mouseX/(horizsize/numImage));
   image(pic[imnum], 0, 0);
      noCursor();
       redraw();


Thanks for all your help, i'd still be scratching my head staring at a few lines of broken code otherwise :]
Re: simple image changer problem
Reply #5 - Aug 26th, 2008, 8:34am
 
You should read redraw(), your question is answered. Smiley
Re: simple image changer problem
Reply #6 - Aug 26th, 2008, 12:35pm
 
aha thanks Cheesy

so redraw() is only used if you do not have a constantly updating draw() window, like with noloop()

right.  i think i understand now

thanks it all makes so much more sense now Smiley
Page Index Toggle Pages: 1