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 and MouseMotionListener interfaces.
  • Override mouseClicked to handle mouse clicks, mouseEnteredmouseExited 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:

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
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.

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.
Subscribe
Notify of
guest


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

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button