//program for dragging differant images PImage one, two, three, four; //image starting positions : float rx1 = 0; float rx2 = 55; float rx3 = 170; float rx4 = 350; float ry1 = 45; float ry2 = 95; float ry3 = 153; float ry4 = 203; float h; // image height //is the mouse over an image? boolean Over, oOver2, oOver3, oOver4; // Is the mouse pressed? boolean Pressed, oPressed2, oPressed3, oPressed4; // Is it pressed? //x and y offsets to make dragging accurate ; float oxOffset, oxOff2, oxOff3, oxOff4; float oyOffset, oyOff2, oyOff3, oyOff4; void setup(){ size(600,400); // images loaded one = loadImage("one.gif"); two = loadImage("two.gif"); three = loadImage("three.gif"); four = loadImage("four.gif"); h = 100; //image height //start with pressed as false for all images Pressed = false; oPressed2 = false; oPressed3 = false; oPressed4 = false; } void draw(){ //invoke movingImages() method, which contains calls to more methods for //drawing each of the images at their starting positions movingImages(0,0); } void movingImages(int posx3, int posy3){ background(120); //the rx and ry are initial coordinates theOne (rx1, ry1); //invoke the method for each image theTwo (rx2,ry2); theThree (rx3, ry3); theFour (rx4, ry4); } //methods for drawing each image void theOne(float x,float y){ image(one, x, y); } void theTwo(float x2,float y2){ image(two, x2, y2); } void theThree(float x3,float y3){ image(three, x3, y3); } void theFour(float x4,float y4){ image(four, x4, y4); } //the following mouse functions enable the dragging after testing the boolean values for Over to oOver4 void mousePressed() { if (Over == true) { Pressed = true; oxOffset = mouseX - rx1; oyOffset = mouseY - ry1; } if (oOver2 == true) { oPressed2 = true; oxOff2 = mouseX - rx2; oyOff2 = mouseY - ry2; } if (oOver3 == true) { oPressed3 = true; oxOff3 = mouseX - rx3; oyOff3 = mouseY - ry3; } if (oOver4 == true) { oPressed4 = true; oxOff4 = mouseX - rx4; oyOff4 = mouseY - ry4; } } //is mouse over an image? void mouseMoved() { if ((mouseX > rx1) && (mouseX < rx1+(one.width)) && (mouseY > ry1) && (mouseY < ry1+h)){ Over = true; } else { Over = false; } if ((mouseX > rx2) && (mouseX < rx2+(two.width)) && (mouseY > ry2) && (mouseY < ry2+h)){ oOver2 = true; } else{ oOver2 = false; } if ((mouseX > rx3) && (mouseX < rx3+(three.width)) && (mouseY > ry3) && (mouseY < ry3+h)){ oOver3 = true; } else { oOver3 = false; } if ((mouseX > rx4) && (mouseX < rx4+(four.width)) && (mouseY > ry4) && (mouseY < ry4+h)){ oOver4 = true; } else { oOver4 = false; } } //is the user trying to drag the image by pressing? void mouseDragged() { if (Pressed == true) { rx1 = mouseX - oxOffset; ry1 = mouseY - oyOffset; } if (oPressed2 == true) { rx2 = mouseX - oxOff2; ry2 = mouseY - oyOff2; } if (oPressed3 == true) { rx3 = mouseX - oxOff3; ry3 = mouseY - oyOff3; } if (oPressed4 == true) { rx4 = mouseX - oxOff4; ry4 = mouseY - oyOff4; } } void mouseReleased() { //return pressed to false after mouse released Pressed = false; oPressed2 = false; oPressed3 = false; oPressed4 = false; }