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 › random image "generator"
Page Index Toggle Pages: 1
random image "generator" (Read 2519 times)
random image "generator"
May 18th, 2010, 3:30pm
 
hey,

i am very new to this,its my first programming experience..
however,i have a project in mind, i think could be done with processing.after lots of search, i decided to ask for help with the code.

so,id like to make an array of images, and when mouseClicked a random pic should be displayed in the middle of the background,untill the next mouse click.

also,im curious,if i can make more than one arrays. as to make three random "picture generators" next to each other on one screen?

thanx for any kind of help..i keep on searching for answers,myself.

anna
Re: random image "generator"
Reply #1 - May 18th, 2010, 11:04pm
 
Not too tricky.  An array of PImages can be loaded at launch -- especially easy if you've numbered the images consistently.  e.g.:

Code:
PImage[] myImageArray = new PImage[25];

for (int i=0; i<myImageArray.length; i++){
 myImageArray = loadImage( "fileName" + i + ".png");
}

void mousePressed(){
 image(myImageArray[(int)random(25)],0,0,width,height);
}


As for multiple arrays, yes, definitely.  Same method, drawing to different areas and checking the mouseX when clicking.
Re: random image "generator"
Reply #2 - May 19th, 2010, 1:17pm
 
great..
i knew its gonna be pretty simple,i just couldnt figure it out..thank you.

well,then i have another question,if i may..

is it possible to add music/mp3 to the pictures,so when displayed also a sound would follow?
or else, can an array contain also pictures and mp3 /so the random option would either display an image or play a sound/,or it has to be the same kind of "medium" in one array?
Re: random image "generator"
Reply #3 - May 19th, 2010, 2:53pm
 
a normal array has to have the same "medium" (variable type), but you can align two arrays if you make them the same size:


Code:
int numberOfObjects = 25;

PImage[] myImageArray = new PImage[numberOfObjects];
myFloatArray[] = new float[numberOfObjects];

for (int i=0; i<numberOfObjects; i++){
 myImageArray[i] = loadImage( "fileName" + i + ".png");
myFloatArray[i] = random(0, 1234);
}


I imagine sound can be played in a similar fashion, but I have no experience with the playback of prerecorded sounds in Processing. You'll be using an external library like Minim or Ess.
Re: random image "generator"
Reply #4 - May 20th, 2010, 6:45am
 
right,thanks a lot guys.
now step-by-step im compicating the whole thing.

now im trying to change it into a multi-screen project,with the MPE library.

the simple

Code:

PImage[] myImageArray = new PImage[5];

void setup() {
size(1200,600);
background(10,10,10);
smooth();
}

void draw() {
imageMode (CENTER);
for (int i=0; i < myImageArray.length; i++) {
myImageArray [0] = loadImage( "two.jpg");
myImageArray [1] = loadImage( "three.jpg");
myImageArray [2] = loadImage( "four.jpg");
myImageArray [3] = loadImage( "five.jpg");
myImageArray [4] = loadImage( "six.jpg");
}
}

void mousePressed(){
image(myImageArray[(int)random(5)],width/4,height/2);
image(myImageArray[(int)random(5)],width-width/4,height/2);
}



to the multiple with MPE /not finished/:

Code:
import mpe.client.*;
TCPClient client;

PImage[] myImageArray = new PImage[5];

void setup() {
client = new TCPClient(sketchPath("mpe.ini"), this);
size(client.getLWidth(), client.getLHeight());
background(10,10,10);
smooth();
client.start();
}
void frameEvent(TCPClient c) {
imageMode (CENTER);
for (int i=0; i < myImageArray.length; i++) {
myImageArray [0] = loadImage( "two.jpg");
myImageArray [1] = loadImage( "three.jpg");
myImageArray [2] = loadImage( "four.jpg");
myImageArray [3] = loadImage( "five.jpg");
myImageArray [4] = loadImage( "six.jpg");
}
}

void draw() {
}

void mousePressed(){
image(myImageArray[(int)random(5)],client.getMWidth()/4,client.getMHeight()/2);
image(myImageArray[(int)random(5)],client.getMWidth()-client.getMWidth()/4,client.getMHeight()/2);
}


however,im not sure how to set the code for the mousePressed to broadcast a message from one client to other clients.so when mouse pressed random pics will be displayed in all screens.
smthing with client.broadcast(); ??
can anyone help?

sorry for having so obvious questions,its the lack of experience.
thank you in advance..
Re: random image "generator"
Reply #5 - May 22nd, 2010, 12:28pm
 
I'm sorry, can't help you with that. But I guess if you'd make a new post for that in either the "Programs", or "Other Libraries" forum, you'd ensure a quicker response
Re: random image "generator"
Reply #6 - May 23rd, 2010, 5:20am
 
verry.berry wrote on May 20th, 2010, 6:45am:
Code:
 
 for (int i=0; i < myImageArray.length; i++) {
  myImageArray [0] = loadImage( "two.jpg");
  myImageArray [1] = loadImage( "three.jpg");
  myImageArray [2] = loadImage( "four.jpg");
  myImageArray [3] = loadImage( "five.jpg");
  myImageArray [4] = loadImage( "six.jpg");
 }



look again at BenHem's post. you are loading 5 images 5 times each. this is bad. it's also needs to be in setup().
Page Index Toggle Pages: 1