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 & HelpSyntax Questions › How to display images
Page Index Toggle Pages: 1
How to display images (Read 589 times)
How to display images
Dec 18th, 2008, 7:01pm
 
My idea is to display a certain image when key pressed for few seconds. An image shows up when key pressed, but it disappears right away. Is there any way to display the images for 3~4 seconds?

PImage city,c1,c2,c3,c4;
void setup() {
 
 size(600, 400);
 city = loadImage("city.jpg");
 c1=loadImage("c1.jpg");
 c2=loadImage("c2.jpg");
 c3=loadImage("c3.jpg");
 c4=loadImage("c4.jpg");
}
void draw() {
 background(city);
}
void keyPressed() {
  int ch = int(random(4));
   
  switch(ch){
    case 1:
      background(c1);
      break;
     
    case 2:
      background(c2);
      break;
    case 3:
      background(c3);
      break;
    case 4:
      background(c4);
      break;
  }
  //background(city);
}
Re: How to display images
Reply #1 - Dec 18th, 2008, 7:51pm
 
using something like a decreasing counter might help. and by the way, you should be thinking of using arrays instead of variables like c1, c2, c3, etc.

Quote:
// your own image class
class MyImage {

 PImage img; // the image to display
 int counter; // the counter

   // constructor : setting the image being passed
 MyImage(PImage img) {
   this.img = img;
 }

}

PImage city; // background image
MyImage[] c; // more background images stored in an array

void setup() {

 size(600, 400);
 city = loadImage("city.jpg");

 // set up the array
 c = new MyImage[4];

 // load each image into the array (c0.jpg, c1.jpg, c2.jpg, c3.jpg)
 for (int i = 0; i < 4; i++) {
   c[i] = new MyImage(loadImage("c" + i + ".jpg"));
 }

}

void draw() {

 background(city);

 // for each image in the array
 for (int i = 0; i < 4; i++) {
   // if its counter is positive, display it on screen
   if (c[i].counter > 0) {
     background(c[i].img);
   }
   // decrease the counter if above zero
   if (c[i].counter > 0) c[i].counter--;
 }
}

void keyPressed() {
 // choose a random image
 int i = int(random(4));
 // set up the counter to 20 frames
 // which means the image will be displayed 20 frames
 // and will disappear
 c[i].counter = 20;
}
Page Index Toggle Pages: 1