Results 1 to 6 of 6
Thread: Drag polygon
- 09-29-2009, 05:14 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 38
- Rep Power
- 0
-
I recommend that you use a MouseAdapter (MouseListener and MouseMotionListener combined). For more specific advice, you may wish to provide more detail and code.
- 09-30-2009, 01:17 PM #3
Member
- Join Date
- Sep 2009
- Posts
- 5
- Rep Power
- 0
example polygon with 5 corners: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; }
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.
- 09-30-2009, 03:48 PM #4
Member
- Join Date
- Mar 2009
- Posts
- 38
- Rep Power
- 0
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
- 03-10-2011, 03:15 PM #5
Member
- Join Date
- Mar 2011
- Posts
- 3
- Rep Power
- 0
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
- 03-10-2011, 06:30 PM #6
Similar Threads
-
A Polygon class which accepts floating point coordinates
By soorena in forum New To JavaReplies: 2Last Post: 04-01-2009, 08:37 AM -
colllision rectangle-polygon
By marina in forum New To JavaReplies: 4Last Post: 01-28-2009, 09:14 AM -
polygon-shaped JComponent
By zenMarko in forum New To JavaReplies: 2Last Post: 11-04-2008, 06:06 PM -
Backdoors are cleaned and Server moved
By JavaBean in forum Suggestions & FeedbackReplies: 2Last Post: 03-21-2008, 02:50 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks