Mouse drag and drop to draw

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
public class Ellipse extends JFrame {
DrawingCanvas canvas;
JLabel location;
public Ellipse() {
super();
Container container = getContentPane();
canvas = new DrawingCanvas();
container.add(canvas);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1, 2));
panel.add(new JLabel("x,y: ", JLabel.RIGHT));
location = new JLabel("");
panel.add(location);
container.add(panel, BorderLayout.SOUTH);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setSize(600,300);
setVisible(true);
}
public static void main(String arg[]) {
new Ellipse();
}
class DrawingCanvas extends JPanel {
double x, y, w, h;
int x1, y1, x2, y2;
Ellipse2D ellipse;
Ellipse2D selectedShape;
Rectangle2D boundingRec;
Cursor curCursor;
public DrawingCanvas() {
x = 20;
y = 20;
w = 100;
h = 75;
setBackground(Color.white);
addMouseListener(new MyMouseListener());
addMouseMotionListener(new MyMouseMotionListener());
}
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
ellipse = new Ellipse2D.Double(x, y, w, h);
g2D.draw(ellipse);
if (boundingRec != null) {
drawHighlightSquares(g2D, boundingRec);
}
if (curCursor != null)
setCursor(curCursor);
}
public void drawHighlightSquares(Graphics2D g2D, Rectangle2D r) {
double x = r.getX();
double y = r.getY();
double w = r.getWidth();
double h = r.getHeight();
g2D.setColor(Color.black);
g2D.fill(new Rectangle.Double(x - 3.0, y - 3.0, 6.0, 6.0));
g2D
.fill(new Rectangle.Double(x + w * 0.5 - 3.0, y - 3.0, 6.0,
6.0));
g2D.fill(new Rectangle.Double(x + w - 3.0, y - 3.0, 6.0, 6.0));
g2D
.fill(new Rectangle.Double(x - 3.0, y + h * 0.5 - 3.0, 6.0,
6.0));
g2D.fill(new Rectangle.Double(x + w - 3.0, y + h * 0.5 - 3.0, 6.0,
6.0));
g2D.fill(new Rectangle.Double(x - 3.0, y + h - 3.0, 6.0, 6.0));
g2D.fill(new Rectangle.Double(x + w * 0.5 - 3.0, y + h - 3.0, 6.0,
6.0));
g2D.fill(new Rectangle.Double(x + w - 3.0, y + h - 3.0, 6.0, 6.0));
}
class MyMouseListener extends MouseAdapter {
public void mousePressed(MouseEvent e) {
if (ellipse.contains(e.getX(), e.getY())) {
selectedShape = ellipse;
if (boundingRec != null)
boundingRec = ellipse.getBounds2D();
displayParameters(selectedShape);
} else {
boundingRec = null;
location.setText("");
}
canvas.repaint();
x1 = e.getX();
y1 = e.getY();
}
public void mouseReleased(MouseEvent e) {
if (ellipse.contains(e.getX(), e.getY())) {
boundingRec = ellipse.getBounds2D();
selectedShape = ellipse;
displayParameters(selectedShape);
}
canvas.repaint();
}
public void mouseClicked(MouseEvent e) {
if (ellipse.contains(e.getX(), e.getY())) {
selectedShape = ellipse;
boundingRec = ellipse.getBounds2D();
displayParameters(selectedShape);
} else {
if (boundingRec != null)
boundingRec = null;
location.setText("");
}
canvas.repaint();
}
}
class MyMouseMotionListener extends MouseMotionAdapter {
public void mouseDragged(MouseEvent e) {
if (ellipse.contains(e.getX(), e.getY())) {
boundingRec = null;
selectedShape = ellipse;
x2 = e.getX();
y2 = e.getY();
x = x + x2 - x1;
y = y + y2 - y1;
x1 = x2;
y1 = y2;
}
if (selectedShape != null)
displayParameters(selectedShape);
canvas.repaint();
}
public void mouseMoved(MouseEvent e) {
if (ellipse != null) {
if (ellipse.contains(e.getX(), e.getY())) {
curCursor = Cursor
.getPredefinedCursor(Cursor.HAND_CURSOR);
} else {
curCursor = Cursor.getDefaultCursor();
}
}
canvas.repaint();
}
}
public void displayParameters(Shape shape) {
double x = selectedShape.getX();
double y = selectedShape.getY();
double w = selectedShape.getWidth();
double h = selectedShape.getHeight();
String locString = "(" + Double.toString(x) + ","
+ Double.toString(y) + ")";
String sizeString = "(" + Double.toString(w) + ","
+ Double.toString(h) + ")";
location.setText(locString);
}
}
}
Related examples in the same category
1. | Creating Basic Shapes | | |
2. | fillRect (int, int, int, int) method draws a solid rectangle | | |
3. | Creating a Shape Using Lines and Curves | | |
4. | Combining Shapes | | |
5. | Draw rectangles, use the drawRect() method. To fill rectangles, use the fillRect() method | | |
6. | Draw line | |  |
7. | Draw a Polygon | |  |
8. | Draw an oval outline | | |
9. | Draw a (Round)rectangle | |  |
10. | Fill a polygon | |  |
11. | Fill a solid oval | | |
12. | Fill a (Round)rectangle | |  |
13. | Change font | |  |
14. | Draw rectangle 2 | |  |
15. | Draw Arc | |  |
16. | Draw Ellipse | |  |
17. | Fill a Rectangle 2 | |  |
18. | Fill Arc 2 | |  |
19. | Draw text | |  |
20. | Draw unicode string | |  |
21. | Shape combine | |  |
22. | Effects | |  |
23. | Arc demonstration: scale, move, rotate, sheer | |  |
24. | Hypnosis Spiral | |  |
25. | GlyphVector.getNumGlyphs() | | |
26. | Resize a shape | | |
27. | Rectangle with rounded corners drawn using Java 2D Graphics API | | |
28. | Compares two ellipses and returns true if they are equal or both null. | | |
29. | Compares two lines are returns true if they are equal or both null. | | |
30. | Creates a diagonal cross shape. | | |
31. | Creates a diamond shape. | | |
32. | Creates a new Stroke-Object for the given type and with. | | |
33. | Creates a region surrounding a line segment by 'widening' the line segment. | | |
34. | Creates a triangle shape that points downwards. | | |
35. | Creates a triangle shape that points upwards. | | |
36. | Generate Polygon | | |
37. | Polygon with float coordinates. | | |
38. | Polyline 2D | | |
39. | Serialises a Shape object. | | |
40. | Tests two polygons for equality. If both are null this method returns true. | | |
41. | Union two rectangles | | |
42. | Calculate Intersection Clip | | |
43. | Draws a shape with the specified rotation about (x, y). | | |
44. | Checks, whether the given rectangle1 fully contains rectangle 2 (even if rectangle 2 has a height or width of zero!). | | |
45. | Reads a Point2D object that has been serialised by the writePoint2D(Point2D, ObjectOutputStream)} method. | | |
46. | Returns a point based on (x, y) but constrained to be within the bounds of a given rectangle. | | |
47. | RectListManager is a class to manage a list of rectangular regions. | | |
48. | Fill Rectangle2D.Double and Ellipse2D.Double | |  |
49. | This program demonstrates the various 2D shapes | |  |