How can I copy a polygon to a new location. I use e.isPopupTrigger() to select the polygon, but I dont know how to copy it. Is the function of copy similar to the function of translate?
e.g. polygon.translate(x,y)?
Thanks.
EDITED:
//this code doesnt work 🙁
if(e.getSource() == Copy){
Polygon copyPolygon = new Polygon();
copyPolygon = selectedTriangle;
copyPolygon.translate(2, 2);
repaint();
}
The problem has been solved with these code 🙂
if(e.getSource() == Copy){
Polygon copyPolygon = new Polygon(selectedTriangle.xpoints,selectedTriangle.ypoints,selectedTriangle.npoints);
copyPolygon.translate(10,10);
triangles.add(copyPolygon);
repaint();
}
You could construct a new polygon from the old one, then move that polygon to a new location
Your code doesn’t work because the line
Doesn’t make a copy of selectedTriangle, it just makes copyPolygon point to the same object. So you need to construct a new polygon that is identical to the original, which is what the first line in my suggestion does.