Hi, I'm trying to create a code which will display several images and then allow the user to click on each particular iamge and then drag it across.
I have an array example for displaying several images.
Code:
Sprite[] sprites;
void setup()
{
size(800, 600); // Set the size of the drawing window
smooth(); // Which on smoothing (slower, but nicer graphics)
sprites = new Sprite[5];
for(int i = 0; i < sprites.length; i++)
sprites[i] = new Sprite(loadImage(i + ".png"), (int)random(width), (int)random(height));
}
void draw()
{
background(255, 255, 255);
for(int i = 0; i < sprites.length; i++)
image(sprites[i].img, sprites[i].x, sprites[i].y);
if(mousePressed)
{
ellipse(mouseX, mouseY, 20, 20);
}
}
void keyPressed()
{
if(keyCode == UP)
{
sprites[0].y -= 5;
}
else if(keyCode == DOWN)
{
sprites[0].y += 5;
}
else if(keyCode == RIGHT)
{
sprites[0].x += 5;
}
else if(keyCode == LEFT)
{
sprites[0].x -= 5;
}
else
{
for(int i = 0; i < sprites.length; i++)
{
sprites[i].x = (int)random(width);
sprites[i].y = (int)random(height);
}
}
}
class Sprite
{
int x;
int y;
PImage img;
Sprite(PImage img, int x, int y)
{
this.x = x;
this.y = y;
this.img = img;
}
}
And another example for drag and droping images
Code:
float bx;
float by;
int bs = 40;
int bz = 30;
boolean bover = false;
boolean locked = false;
float bdifx = 0.0;
float bdify = 0.0;
PImage a;
void setup()
{
size(600, 400);
bx = width/2.0;
by = height/2.0;
rectMode(RADIUS);
a = loadImage("Button.jpg");
}
void draw()
{
background(0);
// Test if the cursor is over the box
if (mouseX > bx-bs && mouseX < bx+bs &&
mouseY > by-bs && mouseY < by+bs)
{
bover = true;
if(!locked)
{
stroke(255);
fill(153);
}
}
else
{
stroke(153);
fill(153);
bover = false;
}
// Draw the box
//rect(bx, by, bs, bs);
image(a, bx, by, bs, bz);
}
void mousePressed() {
if(bover) {
locked = true;
fill(255, 255, 255);
} else {
locked = false;
}
bdifx = mouseX-bx;
bdify = mouseY-by;
}
void mouseDragged() {
if(locked) {
bx = mouseX-bdifx;
by = mouseY-bdify;
}
}
void mouseReleased() {
locked = false;
}
How can I integrate these two files together, so that the user can click on one individual image and drag it around while the rest remain the same. Then click on another image and drag it around?
Thanks in advance.