A simple mouse drag example

With this example we shall show you how to work with MouseListener and MouseMotionListener interfaces in order to handle and monitor mouse events an especially mouse drags. This is a very nice feature to use if your application has many graphical object that the user needs to move around the screen very frequently. It’s is very user friendly to let the user drag the object to perform these kinds of actions.

Basically all you have to do in order to handle mouse drags and mouse moves is:

  • Create a class that implements MouseListener and MouseMotionListener interfaces.
  • Override mouseClicked to handle mouse clicks, mouseEntered, mouseExited methods to check whether your mouse has entered or exited a certain area, mousePressed to monitor the mouse button clicks, mouseReleased to check when the user releases a mouse button.
  • Override mouseDragged method in order to handle mouse drags. Use repaint() method to repaint the object in its new position.

Let’s take a look at the code snippets that follow:

package com.javacodegeeks.snippets.desktop;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Label;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;

public class Sketch extends Component implements MouseListener, MouseMotionListener {

    int sX = -1, sY = -1;
    static Label stat;
    Image bImage;
    boolean dragging = false;
    int curX = -1, curY = -1;

    public static void main(String[] av) {

  JFrame jFrame = new JFrame("Mouse Dragger");

  Container cPane = jFrame.getContentPane();

  Image im = Toolkit.getDefaultToolkit().getImage("C:/Users/nikos7/Desktop/pic.jpg");

  Sketch sk = new Sketch(im);

  cPane.setLayout(new BorderLayout());

  cPane.add(BorderLayout.NORTH, new Label(""));

  cPane.add(BorderLayout.CENTER, sk);

  cPane.add(BorderLayout.SOUTH, stat = new Label());

  stat.setSize(jFrame.getSize().width, stat.getSize().height);





    public Sketch(Image i) {


  bImage = i;

  setSize(300, 200);



    public void showStatus(String s) {


    public void mouseClicked(MouseEvent event) {

    public void mouseEntered(MouseEvent event) {

    public void mouseExited(MouseEvent event) {

    public void mousePressed(MouseEvent event) {

  Point point = event.getPoint();

  System.out.println("mousePressed at " + point);

  sX = point.x;

  sY = point.y;

  dragging = true;

    public void mouseReleased(MouseEvent event) {

  dragging = false;

  System.out.println("Drawn rectangle area IS " + sX + "," + sY + " to "

    + curX + "," + curY);

    public void mouseDragged(MouseEvent event) {

  Point p = event.getPoint();

  // System.err.println("mouse drag to " + p);

  showStatus("mouse Dragged to " + p);

  curX = p.x;

  curY = p.y;

  if (dragging) {



    public void paint(Graphics graphic) {

  int w = curX - sX, h = curY - sY;

  Dimension dims = getSize();

  graphic.drawImage(bImage, 0, 0, dims.width, dims.height, this);

  if (sX < 0 || sY < 0) {



  System.out.println("Rect[" + sX + "," + sY

    + "] size " + w + "x" + h);


  graphic.fillRect(sX, sY, w, h);

    public void mouseMoved(MouseEvent e) {

  showStatus("Mouse to " + e.getPoint());

This was an example on how to handle mouse movements and mouse drags in Java.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button