Home » Desktop Java » awt » event » A simple mouse drag example

About Byron Kiourtzoglou

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.

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);

  jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  jFrame.pack();

  jFrame.setVisible(true);

    }

    public Sketch(Image i) {

  super();

  bImage = i;

  setSize(300, 200);

  addMouseListener(this);

  addMouseMotionListener(this);
    }

    public void showStatus(String s) {

  stat.setText(s);
    }

    @Override
    public void mouseClicked(MouseEvent event) {
    }

    @Override
    public void mouseEntered(MouseEvent event) {
    }

    @Override
    public void mouseExited(MouseEvent event) {
    }

    @Override
    public void mousePressed(MouseEvent event) {

  Point point = event.getPoint();

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

  sX = point.x;

  sY = point.y;

  dragging = true;
    }

    @Override
    public void mouseReleased(MouseEvent event) {

  dragging = false;

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

    + curX + "," + curY);
    }

    @Override
    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) {

repaint();

  }
    }

    @Override
    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) {

return;

  }

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

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

  graphic.setColor(Color.red);

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

    @Override
    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.

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of