# Drag & Drop PGraphics

**URL:** <https://discourse.processing.org/t/drag-drop-pgraphics/33396>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [November 8, 2021, 12:00pm UTC](https://discourse.processing.org/t/drag-drop-pgraphics/33396 "2021-11-08T12:00:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rism](https://avatars.discourse-cdn.com/v4/letter/r/ecccb3/32.png) [@rism](https://discourse.processing.org/u/rism)\
**Post date:** [November 8, 2021, 12:00pm UTC](https://discourse.processing.org/t/drag-drop-pgraphics/33396/1 "2021-11-08T12:00:33Z")

</div>

Hi, I am trying to move the square to another position but it forms a streak of squares instead when dragged.

Here is what I have so far:

```auto
PGraphics squares;

float bx;
float by;

boolean locked = false;

float xOffset = 0.0; 
float yOffset = 0.0; 

void setup(){
  size (600, 300);
  background(0);
         
         squares = createGraphics(displayWidth, displayHeight);
    
         squares.beginDraw();
         squares.fill(160, 82, 45, random(0, 255));
         squares.noStroke();
         squares.square(random(width), random(height), random(500));
         squares.endDraw();
}

void draw (){
  image(squares, bx, by);
}

void mousePressed() {
  locked = true; 
    
  xOffset = mouseX-bx; 
  yOffset = mouseY-by; 
}

void mouseDragged(){
  if(locked) {
      bx = mouseX-xOffset; 
      by = mouseY-yOffset; 
  }  
}

void mouseReleased() {
  locked = false;
}

```

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [November 8, 2021, 1:54pm UTC](https://discourse.processing.org/t/drag-drop-pgraphics/33396/2 "2021-11-08T13:54:05Z")

</div>

Hello,

You need to clean your screen before drawing something else on it. Otherwise, it keep adding to the previous frame:

```auto
void draw() {
  background(0);
  image(squares, bx, by);
}

```

---

<div class="post-metadata">

**Author:** ![rism](https://avatars.discourse-cdn.com/v4/letter/r/ecccb3/32.png) [@rism](https://discourse.processing.org/u/rism)\
**Post date:** [November 8, 2021, 2:48pm UTC](https://discourse.processing.org/t/drag-drop-pgraphics/33396/3 "2021-11-08T14:48:45Z")

</div>

You’re right, I overlooked that. Thank you!
