Total members 10261 | Gratitudes |It is currently Tue May 22, 2012 4:53 pm Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 2 posts ] 
Author Code Snippet
 Code subject: Drag and Drop in java
PostPosted: Tue Nov 11, 2008 8:39 pm 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2279
Location: Earth
Has thanked: 39 time
Have thanks: 61 time

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 9703 times ]
File comment: When you drag the rectangle and move, output will be
drag1.gif
drag1.gif [ 5.87 KiB | Viewed 9699 times ]

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )
TOP
 Profile Send private message  
Reply with quote  
 Code subject: Re: Drag and Drop in java
PostPosted: Wed Nov 16, 2011 5:03 pm 
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.


TOP
  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 2 posts ] 
Quick reply


  

 Similar topics
 video chat application in java
 navigating to database using java
 java mobile apps
 need help in java
 Read your gmail using Java code
 Java Programing to communicating port parallel
 Java Chat
 Steganography in java
 java code for listing folder contents from remote folder
 java

All times are UTC [ DST ]


Users browsing similar codes

Users browsing this forum: No registered users and 2 guests



Jump to:  
Previous Code Snippet | Next Code Snippet 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team