Results 1 to 6 of 6

Thread: Drag polygon

  1. #1
    cassysumandak is offline Member
    Join Date
    Mar 2009
    Posts
    38
    Rep Power
    0

    Default Drag polygon

    I draw many polygons, and later wanted to move some of the polygon to new location on screen using mouse drag. How can I drag the polygon using mouse and see it moving with the mouse drag?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I recommend that you use a MouseAdapter (MouseListener and MouseMotionListener combined). For more specific advice, you may wish to provide more detail and code.

  3. #3
    oxano is offline Member
    Join Date
    Sep 2009
    Posts
    5
    Rep Power
    0

    Default

    Java Code:
    int startX, startY, curX, curY;
    boolean dragging = false;
    
    
    //from the moment you press down the mousebutton it checks and stores the
    //original mouse position and sets dragging boolean to true
    public void mousePressed(mousEvent e){
       startX = e.getX();
       startY = e.getY();
       dragging = true; 
    }
    
    //Every time you move the mouse this method is called, you get the current
    //mouse position and compare it with the original one(startX,startY) and you
    //know to where it's draggging. Because it's called every time the mouse is
    //moved, even with no pressing of the mouse, the boolean is important
    public void mouseMoved(mouseEvent e){
       if(dragging){
          curX = e.getX();
          curY = e.getY();
          repaint() / Update() / redraw()  //whatever method you have
       
    }
    
    //when you release the mousebutton the boolean must be set back to false
    public void mouseReleased(mouseEvent e){
       dragging = false;
    }
    example polygon with 5 corners:
    1,2
    4,9
    6,7
    3,5
    7,8

    then dragging begins: mousepressed -->mousemoved for a while -->mousereleased
    the mousepressed sets startX and Y to 1,2
    then after some mousemoved methods it reads for the curX and Y : 4,7
    then you know you must redraw your polygon at the same X coordinates +(4-1) and Y +(7-2).

    thus far the easy part.

    hard part: how to know on which polygon you clicked and if you even clicked on 1 after all.
    Do you want to detect it with code or do you have some sort of list in your GUI where all the drawn polygons are listed and you can select em that way?
    Last edited by oxano; 09-30-2009 at 01:20 PM.

  4. #4
    cassysumandak is offline Member
    Join Date
    Mar 2009
    Posts
    38
    Rep Power
    0

    Default

    Thanks Oxano, with your explaination, I have changed my code and manage to drag the triangles successfully :-).

    Java Code:
    public class DrawingBoardWithMatrix extends JFrame implements MouseListener, ActionListener {
    
    java.util.List<Polygon> triangles = new LinkedList<Polygon>();
    Point startDrag,endDrag, midPoint;
    Polygon selectedTriangle=null;
    ....
    
    public PaintSurface() {  
            this.addMouseListener(new MouseAdapter() { 
            public void mousePressed (MouseEvent e) {
                  startDrag = new Point(e.getX(), e.getY()); //First point
                  endDrag = startDrag;
                  repaint();     			
                  //check the point is inside the triangle
                  for (Polygon p:triangles){
                       if (p.contains(startDrag)) {    // mouse pressed inside triangle
                          selectedTriangle = p;
         	              break;
                       }
                  }
            } //end mousePressed 
          			     		
            public void mouseReleased(MouseEvent e) {  
      	     if (selectedTriangle ==null) { //mouse pressed not within area of triangles, create new triangle
                 .....
    	          int[] xs = { startDrag.x, endDrag.x, midPoint.x };
                      int[] ys = { startDrag.y, startDrag.y, midPoint.y }; 
                      triangles.add(new Polygon(xs, ys,3)); //save to list
    	     }
    	     repaint();
    	     endDrag   = null;
    	     startDrag = null;
    	     selectedTriangle=null;
            } //end mouseReleased
    
          });//end addMouseListener
        
          this.addMouseMotionListener(new MouseMotionAdapter() {
          public void mouseDragged(MouseEvent e) {
               endDrag = new Point(e.getX(), e.getY());
        
               if (selectedTriangle !=null) {	//mouse pressed inside polygon triangle
    	         int xtrans = endDrag.x - startDrag.x;
    	         int ytrans = endDrag.y - startDrag.y; 
    	         selectedTriangle.translate(xtrans,ytrans);   //move existing triangle
    	         repaint();
    	         startDrag = endDrag;
               }
           } //end mouseDragged
          }); //end this.addMouseMotionListener      
    }//end paintSurface       
            
    public void paint(Graphics g) {
    .....
    }//end paint

  5. #5
    hawx is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default

    excuse will be possible to let me see your code I need to make a tangram and the drag of the polygons have not been able to
    thanks

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default

    Start your own thread and ask a specific question and you may get help.

    Keep on saying gimmedacodez and you'll get nowhere. A forum is not a code factory.

    db

Similar Threads

  1. Replies: 2
    Last Post: 04-01-2009, 08:37 AM
  2. colllision rectangle-polygon
    By marina in forum New To Java
    Replies: 4
    Last Post: 01-28-2009, 09:14 AM
  3. polygon-shaped JComponent
    By zenMarko in forum New To Java
    Replies: 2
    Last Post: 11-04-2008, 06:06 PM
  4. Backdoors are cleaned and Server moved
    By JavaBean in forum Suggestions & Feedback
    Replies: 2
    Last Post: 03-21-2008, 02:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •