tags:

views:

187

answers:

1

How to make a copy of a polygon using mouse click (e.g. when the mouse click, a menu pop up showing menu for copy).

I have problem to differentiate a mouse click, to draw or to copy. I created a method where when the user pressed the mouse, the method will check if the mouse pressed inside existing polygon or outside. If it outside polygon it will draw a new polygon. If it outside it will move the polygon. How am I able to copy the polygon?

.....
public void mousePressed(MouseEvent e) {        
  startDrag = new Point(e.getX(), e.getY());
  endDrag = startDrag;
  repaint();          
  for(Polygon p:triangles){            
     if(p.contains(startDrag)){ // if inside polygon triangle, mark the triangle
    selectedTriangle = p;
    break;
     }
  }
}

....

public void mouseClicked(MouseEvent e) {
   startDrag = new Point(e.getX(), e.getY());
   Polygon[] triArray = triangles.toArray(new   Polygon[triangles.size()]);
   if (e.getClickCount() ==2) {
      for (Polygon p:triArray){
     if (p.contains(startDrag)) {//Polygon has a 'contains(Point)' 
      triangles.remove (p);
          break;
     }
     }
 }
A: 

You can get the button pressed from the mouse event. Such as

 int button = e.getButton();
 if(button==MouseEvent.BUTTON1){

 }else if(button == MouseEvent.BUTTON3){

 }

See the javadoc for MouseEvent for more info.

broschb
Better way to do this in platform independent manner is to use MouseEvent.isPopupTrigger() instead.
I82Much
Thanks, I solved the problem using JPopupMenu ...thanks a lot.
Jessy