Load Images into a grid?
in
Programming Questions
•
3 years ago
Hi there,
I'm trying to load a series of randomly repeating images into a type mask, but am having trouble doing it. The end result should look like this:
But so far, I can only get the pieces to slide across the screen behind the mask. Can anyone help me with this? I'm a beginner and I'm stuck... My code so far is:
http://pastie.org/1041757
I'm trying to load a series of randomly repeating images into a type mask, but am having trouble doing it. The end result should look like this:
But so far, I can only get the pieces to slide across the screen behind the mask. Can anyone help me with this? I'm a beginner and I'm stuck... My code so far is:
http://pastie.org/1041757
ArrayList pieces;
PImage maskImage;
int totalPieces = 9;
int counter = 0;
int counterThreshold = 1000;
class Piece{
PImage img;
float x, y;
float xDir, yDir;
Piece(String path)
{
img = loadImage(path);
x = random(1)*width;
y = random(1)*height;
xDir = random(1)*2+0.5; // change this to a fix number like xDir = 3; will fix the speed
if(random(1)>0.5)
yDir = random(1);
else
yDir = random(1)*(-1);
}
}
void setup(){
size(800,600);
background(255);
maskImage = loadImage("mask.png");
pieces = new ArrayList();
for(int i=0; i<totalPieces*100; i++){
String imgPath = "p"+Integer.toString( i%totalPieces+1 )+".png";
pieces.add(new Piece(imgPath));
}
}
void draw(){
background(255);
for(int i=0; i<pieces.size(); i++){
Piece piece = (Piece)pieces.get(i);
piece.x = piece.x + piece.xDir;
image(piece.img, piece.x, piece.y);
}
image(maskImage,0,0);
if(counter>counterThreshold){
for(int j=0; j<pieces.size(); j++){
Piece piece = (Piece)pieces.get(j);
piece.x = 50;
}
counter = 0;
}
counter++;
}
PImage maskImage;
int totalPieces = 9;
int counter = 0;
int counterThreshold = 1000;
class Piece{
PImage img;
float x, y;
float xDir, yDir;
Piece(String path)
{
img = loadImage(path);
x = random(1)*width;
y = random(1)*height;
xDir = random(1)*2+0.5; // change this to a fix number like xDir = 3; will fix the speed
if(random(1)>0.5)
yDir = random(1);
else
yDir = random(1)*(-1);
}
}
void setup(){
size(800,600);
background(255);
maskImage = loadImage("mask.png");
pieces = new ArrayList();
for(int i=0; i<totalPieces*100; i++){
String imgPath = "p"+Integer.toString( i%totalPieces+1 )+".png";
pieces.add(new Piece(imgPath));
}
}
void draw(){
background(255);
for(int i=0; i<pieces.size(); i++){
Piece piece = (Piece)pieces.get(i);
piece.x = piece.x + piece.xDir;
image(piece.img, piece.x, piece.y);
}
image(maskImage,0,0);
if(counter>counterThreshold){
for(int j=0; j<pieces.size(); j++){
Piece piece = (Piece)pieces.get(j);
piece.x = 50;
}
counter = 0;
}
counter++;
}
1