Mouse drag and drop to draw : Shape « 2D Graphics GUI « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » 2D Graphics GUI » ShapeScreenshots 
Mouse drag and drop to draw
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(12));
    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 = (Graphics2Dg;
      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.06.06.0));
      g2D
          .fill(new Rectangle.Double(x + w * 0.5 3.0, y - 3.06.0,
              6.0));
      g2D.fill(new Rectangle.Double(x + w - 3.0, y - 3.06.06.0));
      g2D
          .fill(new Rectangle.Double(x - 3.0, y + h * 0.5 3.06.0,
              6.0));
      g2D.fill(new Rectangle.Double(x + w - 3.0, y + h * 0.5 3.06.0,
          6.0));
      g2D.fill(new Rectangle.Double(x - 3.0, y + h - 3.06.06.0));
      g2D.fill(new Rectangle.Double(x + w * 0.5 3.0, y + h - 3.06.0,
          6.0));
      g2D.fill(new Rectangle.Double(x + w - 3.0, y + h - 3.06.06.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. Draw lineDraw line
2. Draw a PolygonDraw a Polygon
3. Draw a rectangleDraw a rectangle
4. Fill a polygonFill a polygon
5. Fill a rectangleFill a rectangle
6. Change fontChange font
7. Draw rectangle 2Draw rectangle 2
8. Draw ArcDraw Arc
9. Draw EllipseDraw Ellipse
10. Fill a Rectangle 2Fill a Rectangle 2
11. Fill Arc 2Fill Arc 2
12. Draw textDraw text
13. Draw unicode string Draw unicode string
14. Shape combineShape combine
15. EffectsEffects
16. Arc demonstration: scale, move, rotate, sheerArc demonstration: scale, move, rotate, sheer
17. Hypnosis SpiralHypnosis Spiral
18. Shape Test Shape Test
w_w_w._j___a___v__a2__s_.co__m_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.