This section illustrates you how to drag and move mouse to draw a figure.
To draw a figure using drag and drop, we have used Rectangle2D class to draw a rectangle. This rectangle has been shown on the frame. When you drag the rectangle and move, a number of figures will be drawn connected to each other. The class MouseEvent provides different methods for mouse actions and all the actions are to be performed by class MouseAdaptor and MouseMotionAdaptor.
To returns the horizontal x position of the event relative to the source component, the method getX() is used and to return the vertical y position of the event relative to the source component, getY() is used. The method getBounds2D() returns the bounding box of this Rectangle2D.
To encapsulate the bitmap representation of the mouse cursor, class Cursor is used. The method Double.toString() returns the string representation of the double argument.
Here is the code of MouseDragAndDrop.java
Output will be displayed as:
When you drag the rectangle and move, output will be displayed as:
Liked it! Share this Tutorial