/* 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 NOT use double buffering. To disable double buffering, it is necessary to turn it off for the drawing surface panel AND for the "root pane" of the applet, which contains that panel. */ import java.awt.*; import java.awt.event.*; import javax.swing.*; public class NonDoubleBufferedDrag 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, and turn off double buffering. setContentPane( new Display() ); getRootPane().setDoubleBuffered(false); } 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); setDoubleBuffered(false); } 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 g.setColor(Color.white); g.fillRect(0,0,width,height); // 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