Howto Drag and Drop a pic file

I am trying to drag a pic from a side to another and drop it in that new location, I have this code, but it doesn't work

PImage sample;

void setup(){
size(1000, 600); noStroke(); sample=loadImage("test.png"); }

void draw(){ background(255); image(sample,0,400,50,30); }

void mouseDragged() { image(sample,mouseX,mouseY,50,30); }

void mouseReleased() { image(sample,mouseX,mouseY,50,30); }

Answers

  • Answer ✓

    Please format your code. Edit your post, select code and hit ctrl+o.

    You need to use global pos variables:

    PImage sample;
    int x, y;
    
    void setup() {
      size(1000, 600); 
      noStroke(); 
      sample=loadImage("G2.jpg");
      x=0;
      y=400;
    }
    
    void draw() { 
      background(255); 
      image(sample, x, y, 50, 30);
    }
    
    void mouseDragged() { 
      x=mouseX; 
      y=mouseY;
    }
    

    Kf

  • Hello Kfrajer

    It was quite easy, but I didn't find out the answer, it worked fine for me. I will format code with Ctrl+o in future

    Thanks a lot

    Julio Dorado

Sign In or Register to comment.