How to use images in an array??
in
Programming Questions
•
2 years ago
so i have an array of image and a class and cant work out how to make the images work so that when a new image is create it is the next image in the string at the moment it only create image 1 over and over.
Idea [] ideas=new Idea[15];
String[] files = {"arch.jpg","arch1.jpg","arch2.jpg","arch3.jpg"};
PImage[] images = new PImage[files.length];
int more=1;
int fade=255;
void setup() {
size(800, 800);
imageMode (CENTER);
//img = loadImage("arch.jpg");
frameRate(30);
smooth();
for(int a = 0; a<files.length; a++)
{ images[a] = loadImage(files[a]); // Load images in string
}
for (int create=0;create<15;create++)
{
ideas[create]=new Idea(random(width), random(height),100,0.8);
}
}
void mousePressed() {
// new image is created every time mouse clicked
more = (more + 1) % ideas.length;
}
// ------------------------------------------------------------------------------ Main methods
void draw() {
background(255);
fill(0, fade);
ellipse (width/2, height/2, 400, 400);
for (int display=0;display<more;display++)
{
ideas[display].drawIdea();
ideas[display].move();
}
}
class Idea
{
float ideax;
float ideay;
float change = 1;
int imageIndex=0;
float movex=random(1, 4);
float movey=random(1, 4);
Idea(float x, float y, float speed, float _diameter)
{
ideax=x;
ideay=y;
}
void drawIdea()
{
pushMatrix();
translate(ideax, ideay);//controls the position
image(images [0],20,20);
//image(img, 20, 20);
//fill (colour);
//quad(10,20, 70,50, 60,80, 20,80);
//ellipse(0, 0, 80, 80);
popMatrix();
}
void move()
{
ideax+=movex;
ideay+=movey;
if (ideax+70 > width) {//right
ideax = width-70;
movex*=-1;
}
else if (ideax-30<0) //left
{
ideax = 30;
movex *= -1;
}
if (ideay+70>height)//bottem
{
ideay=height-70;
movey*=-1;
}
else if (ideay-30<0)//top
{
ideay=30;
movey*=-1;
}
}
}
Idea [] ideas=new Idea[15];
String[] files = {"arch.jpg","arch1.jpg","arch2.jpg","arch3.jpg"};
PImage[] images = new PImage[files.length];
int more=1;
int fade=255;
void setup() {
size(800, 800);
imageMode (CENTER);
//img = loadImage("arch.jpg");
frameRate(30);
smooth();
for(int a = 0; a<files.length; a++)
{ images[a] = loadImage(files[a]); // Load images in string
}
for (int create=0;create<15;create++)
{
ideas[create]=new Idea(random(width), random(height),100,0.8);
}
}
void mousePressed() {
// new image is created every time mouse clicked
more = (more + 1) % ideas.length;
}
// ------------------------------------------------------------------------------ Main methods
void draw() {
background(255);
fill(0, fade);
ellipse (width/2, height/2, 400, 400);
for (int display=0;display<more;display++)
{
ideas[display].drawIdea();
ideas[display].move();
}
}
class Idea
{
float ideax;
float ideay;
float change = 1;
int imageIndex=0;
float movex=random(1, 4);
float movey=random(1, 4);
Idea(float x, float y, float speed, float _diameter)
{
ideax=x;
ideay=y;
}
void drawIdea()
{
pushMatrix();
translate(ideax, ideay);//controls the position
image(images [0],20,20);
//image(img, 20, 20);
//fill (colour);
//quad(10,20, 70,50, 60,80, 20,80);
//ellipse(0, 0, 80, 80);
popMatrix();
}
void move()
{
ideax+=movex;
ideay+=movey;
if (ideax+70 > width) {//right
ideax = width-70;
movex*=-1;
}
else if (ideax-30<0) //left
{
ideax = 30;
movex *= -1;
}
if (ideay+70>height)//bottem
{
ideay=height-70;
movey*=-1;
}
else if (ideay-30<0)//top
{
ideay=30;
movey*=-1;
}
}
}
1