Total members 11800 |It is currently Tue Oct 22, 2019 2:05 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Drag and Drop in java

Code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.border.TitledBorder;

public class MouseDragAndDrop extends JFrame {
Canvas1 canvas;
  JLabel label;

  public MouseDragAndDrop() {
    super();
    Container container = getContentPane();
    canvas = new Canvas1();
    container.add(canvas);
    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(1, 2));
    label = new JLabel("");
    panel.add(label);
    container.add(panel, BorderLayout.SOUTH);
    setSize(600,300);
    setVisible(true);
  }
    public static void main(String arg[]) {
    new MouseDragAndDrop();
  }
  class Canvas1 extends JPanel {
    double p, q, width, height;
    int p1, q1, p2, q2;
    Rectangle2D rect1,rect2;
    Rectangle2D rectangle;
    Cursor cursor;
    public Canvas1() {
      p = 25;
      q = 25;
      width = 90;
      height = 70;
      addMouseListener(new EventMouseListener());
      addMouseMotionListener(new EventMouseMotionListener());
    }
    public void paint(Graphics g) {
      Graphics2D g2 = (Graphics2D) g;
      rect1 = new Rectangle2D.Double(p, q, width, height);
      g2.draw(rect1);
      if (rectangle != null) {
        drawSquares(g2, rectangle);
      }
      if (cursor != null)
        setCursor(cursor);
    }
    public void drawSquares(Graphics2D g2, Rectangle2D rect) {
      double p = rect.getX();
      double q = rect.getY();
      double width = rect.getWidth();
      double height = rect.getHeight();
      g2.setColor(Color.black);
    }
   class EventMouseListener extends MouseAdapter {
      public void mousePressed(MouseEvent event) {
          rect2 = rect1;
          rectangle = rect1.getBounds2D();
          display(rect2);
          p1 = event.getX();
          q1 = event.getY();
      }
      public void mouseReleased(MouseEvent event) {
          rectangle = rect1.getBounds2D();
          rect2 = rect1;
          display(rect2);
       }
      public void mouseClicked(MouseEvent event) {
          rect2 = rect1;
          rectangle = rect1.getBounds2D();
          display(rect2);
        }
   }
    class EventMouseMotionListener extends MouseMotionAdapter {
      public void mouseDragged(MouseEvent event) {
        if (rect1.contains(event.getX(), event.getY())) {
          rectangle = null;
          rect2 = rect1;
          p2 = event.getX();
          q2 = event.getY();
          p = p + p2 - p1;
          q = q + q2 - q1;
          p1 = p2;
          q1 = q2;
        }
        if (rect2 != null)
          display(rect2);
        canvas.repaint();
      }
    public void mouseMoved(MouseEvent event) {
         cursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
           }
        }
      public void display(Shape shape) {
      double p = rect2.getX();
      double q = rect2.getY();
      double width= rect2.getWidth();
      double height = rect2.getHeight();
      String st = "(" + Double.toString(p)+","+Double.toString(q) + ")";
      String sizeString = "(" + Double.toString(width) + ","+
        Double.toString(height) + ")";
      label.setText(st);
    }
  }
}





Attachments:
File comment: first output
drag.gif
drag.gif [ 1.4 KiB | Viewed 19579 times ]
File comment: When you drag the rectangle and move, output will be
drag1.gif
drag1.gif [ 5.87 KiB | Viewed 19575 times ]
Author:
Mastermind
User avatar Posts: 2707
Have thanks: 74 time

hi,
thank you for the code.But,how can we remove the dragged squares while we drag the object? i tried using flags but repaint gets called again .I want to show the square only when i release my mouse.Any pointer would be helpful.



Author:
Post new topic Reply to topic  [ 2 posts ] 

  Related Posts  to : Drag and Drop in java
 Drag and Drop     -  
 Drag and Drop using javascript     -  
 how to create a mouse over drop down in html ?     -  
 Validation of php drop down menu of months,days and years     -  
 2d game in java-Monster-Java 2D Game Graphics and Animation     -  
 Using FTP in java     -  
 Java course     -  
 need help in java     -  
 What is Java API?!!!     -  
 java or .net     -  



Topic Tags

Java AWT, Java Swing






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com