An applet showing a red square that the user can drag with the mouse, on a background of lines. The user can drag the square off the applet and drop it. It will jump back to its starting point.

This applet does uses double buffering, which is the default behavior in Swing.

/*******************************************************
*     MYCPLUS Sample Code - https://www.mycplus.com     *
*                                                     *
*   This code is made available as a service to our   *
*      visitors and is provided strictly for the      *
*               purpose of illustration.              *
*                                                     *
* Please direct all inquiries to saqib at mycplus.com *
*******************************************************/



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class DoubleBufferedDrag extends JApplet {


   public void init() {
        // The applet does nothing but set up a drawing surface
        // belonging to the nested class Display as the content
        // pane of the applet.
      setContentPane( new Display() );
   }


   class Display extends JPanel
                implements MouseListener, MouseMotionListener {
       // A nested class that is used for the drawing surface
       // of the applet.  This class also handles the drawing.

      int x1, y1;   // Coords of top-left corner of the red square.

      /* Some variables used during dragging */

      boolean dragging;      // Set to true when a drag is in progress.

      int offsetX, offsetY;  // Offset of mouse-click coordinates from
                             //   top-left corner of the square.

      Display() {
           // Constructor sets the initial position of the square
           // and sets up mouse listening.
         x1 = 10;
         y1 = 10;
         setBackground(Color.white);
         addMouseListener(this);
         addMouseMotionListener(this);
      }

      public void paintComponent(Graphics g) {
            // Draw a background of horizontal and vertical lines,
            // with the red square that the user drags on top.
            // Also draw a 2-pixel black frame around the panel.
         int width = getSize().width;   // width of this panel
         int height = getSize().height; // height of this
         super.paintComponent(g);       // clear to background color
         g.setColor(Color.gray);
         for (int i = 3; i < width; i += 2)         // vertical lines
            g.drawLine(i,0,i,height);
         for (int j = 3; j < height; j += 2)        // horizontal lines
            g.drawLine(0,j,width,j);
         g.setColor(Color.red);
         g.fillRect(x1, y1, 50, 50);                // the red square
         g.setColor(Color.black);
         g.drawRect(0, 0, width - 1, height - 1);   // frame
         g.drawRect(1, 1, width - 3, height - 3);
      }


      public void mousePressed(MouseEvent evt) {
             // Respond when the user presses the mouse on the applet.
             // If the user clicked in the red square, start dragging it.

         if (dragging)  // Exit if a drag is already in progress.
            return;

         int x = evt.getX();  // Location where user clicked.
         int y = evt.getY();

         if (x >= x1 && x < x1+50 && y >= y1 && y < y1+50) {
                // The user clicked the red square.
            dragging = true;
            offsetX = x - x1;  // Distance from corner of square to click point.
            offsetY = y - y1;
         }

      }


      public void mouseReleased(MouseEvent evt) {
             // Dragging stops when user releases the mouse button.
             // If sqaure is (almost) off the screen, move it back to
             // its starting point.
          dragging = false;
          if (x1 + 50 < 3 || x1 > getSize().width - 3
                               || y1 + 50 < 3 || y1 > getSize().height - 3) {
              x1 = 10;
              y1 = 10;
              repaint();
          }
      }


      public void mouseDragged(MouseEvent evt) {
              // Respond when the user drags the mouse.  If a square is
              // not being dragged, then exit.  Otherwise, change the position
              // of the square that is being dragged to match the position
              // of the mouse.  Note that the corner of the square is placed
              // in the same postion with respect to the mouse that it had
              // when the user started dragging it.
          if (dragging == false)
            return;
          int x = evt.getX();   // position of mouse
          int y = evt.getY();
          x1 = x - offsetX;     // move the square
          y1 = y - offsetY;
          repaint();
      }


      public void mouseMoved(MouseEvent evt) { }
      public void mouseClicked(MouseEvent evt) { }
      public void mouseEntered(MouseEvent evt) { }
      public void mouseExited(MouseEvent evt) { }

   } // end nested class Display

} // end class DragTwoSquares