event
Drag event 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 objects 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
andMouseMotionListener
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. Userepaint()
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.applet.Applet; import java.awt.BasicStroke; import java.awt.BorderLayout; import java.awt.Canvas; import java.awt.Color; import java.awt.Dimension; import java.awt.Frame; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Label; import java.awt.Rectangle; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JApplet; //Double buffering to make it repaint faster. public class DragRect extends JApplet { static protected Label lab = new Label( "Drag the rectangle with your mouse"); @Override public void init() { getContentPane().setLayout(new BorderLayout()); getContentPane().add(new MyCanvas()); getContentPane().add("South", lab); } public static void main(String s[]) { Frame frame = new Frame("ShapeMover"); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); Applet app = new DragRect(); frame.add("Center", app); app.init(); frame.pack(); frame.setSize(new Dimension(550, 250)); frame.setVisible(true); } } class MyCanvas extends Canvas implements MouseListener, MouseMotionListener { Rectangle rectangle = new Rectangle(0, 0, 70, 60); Graphics2D graphic2D; int pX, pY; boolean ftime = true; Rectangle shape; boolean pOut = false; public MyCanvas() { setBackground(Color.BLUE); addMouseMotionListener(this); addMouseListener(this); } @Override public void mousePressed(MouseEvent event) { pX = rectangle.x - event.getX(); pY = rectangle.y - event.getY(); if (rectangle.contains(event.getX(), event.getY())) { updateLocation(event); } else { DragRect.lab.setText("Drag the shape with your mouse."); pOut = true; } } @Override public void mouseDragged(MouseEvent event) { if (!pOut) { updateLocation(event); } else { DragRect.lab.setText("Drag the shape with your mouse."); } } @Override public void mouseReleased(MouseEvent event) { if (rectangle.contains(event.getX(), event.getY())) { updateLocation(event); } else { DragRect.lab.setText("Drag the shape with your mouse."); pOut = false; } } @Override public void mouseMoved(MouseEvent e) { } @Override public void mouseClicked(MouseEvent e) { } @Override public void mouseExited(MouseEvent e) { } @Override public void mouseEntered(MouseEvent e) { } public void updateLocation(MouseEvent e) { rectangle.setLocation(pX + e.getX(), pY + e.getY()); if (checkRect()) { DragRect.lab.setText(rectangle.getX() + ", " + rectangle.getY()); } else { DragRect.lab.setText("Drag the shape with your mouse."); } repaint(); } @Override public void paint(Graphics g) { update(g); } @Override public void update(Graphics g) { Graphics2D g2 = (Graphics2D) g; Dimension dim = getSize(); int w = (int) dim.getWidth(); int h = (int) dim.getHeight(); g2.setStroke(new BasicStroke(8.0f)); if (ftime) { shape = new Rectangle(dim); rectangle.setLocation(w / 2 - 50, h / 2 - 25); ftime = false; } g2.setPaint(Color.white); g2.fillRect(0, 0, w, h); g2.setColor(Color.red); g2.draw(rectangle); g2.setColor(Color.black); g2.fill(rectangle); } boolean checkRect() { if (shape == null) { return false; } if (shape.contains(rectangle.x, rectangle.y, 100, 50)) { return true; } int new_x = rectangle.x; int new_y = rectangle.y; if ((rectangle.x + 100) > shape.getWidth()) { new_x = (int) shape.getWidth() - 99; } if (rectangle.x < 0) { new_x = -1; } if ((rectangle.y + 50) > shape.getHeight()) { new_y = (int) shape.getHeight() - 49; } if (rectangle.y < 0) { new_y = -1; } rectangle.setLocation(new_x, new_y); return false; } }
This was an example on how to handle object dragging in Java.