I'm assuming you know how to put together classes and objects. Otherwise you had best brush up on these. Shiffman has some good tutorials on the subject if you're stuck.
I've put together some object dropping code for you. What you need to do is put the dragging feature in to it yourself. It's not beginner stuff but it's a foundation for some good code which has served me well.
Code:
// A Vector is a magic array
// It's a bit like a pez dispenser, allowing you to add and subtract.
// Doing this with normal arrays is far more complicated and slower,
// so it's best to dive in and learn this very useful thing.
Vector draggables;
void setup(){
size(400, 400);
smooth();
draggables = new Vector();
}
void draw(){
background(100);
for(int i = 0; i < draggables.size(); i++){
// The Vector only knows we have Objects in there, not what they are
// So we have to "cast" them (like a mould), ie: (datatype)variable
Drag d = (Drag)draggables.get(i);
d.draw();
}
}
// This adds to the Vector of draggable objects.
// But only when you are not over an existing object
// otherwise you add to the list when dragging.
void mousePressed(){
boolean mouseOver = false;
for(int i = 0; i < draggables.size(); i++){
Drag d = (Drag)draggables.get(i);
if(d.over()){
mouseOver = true;
}
}
if(!mouseOver){
draggables.add(new Drag(mouseX, mouseY, 50));
}
}
class Drag{
float x,y,big;
boolean locked;
Drag(float x, float y, float big){
this.x = x;
this.y = y;
this.big = big;
}
void draw(){
ellipse(x, y, big, big);
// The dragging stuff goes in here.
// use if(mousePressed) to turn locked on
// and the else of that to turn it off.
}
boolean over(){
if(dist(mouseX, mouseY, x, y) < big * 0.5){
return true;
}
return false;
}
}
As for the snapping values, I've put a function up at
Seltar's Snippet Palace you can use for your dragging and dropping. That I've left for you to do as well.