问题
Following my previous post here , I changed the code to :
PolygonnerJframe.java
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
*
* @author X2
*
*/
public class PolygonnerJframe
{
public static void main (String[] args)
{
JFrame frame = new JFrame("Draw polygons");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new DrawingPanel());
frame.pack();
frame.setVisible(true);
}
}
Now , this code results in :

And I can't understand what causes this .
The changes that I've made are : when we're done with a polygon , its coordinates are saved in the arrayList of class Polygon
, and each time that I create a new polygon , I take
the previous polygons and draw them , while drawing a new polygon .
As you can see above , something went wrong with the drawing and I can't seem to find the problem .
I'd appreciate any help .
Thanks
EDIT:
After taking into consideration what @StanislavL said , I moved those lines to mouseClicked()
, but this time I get a new screen each time that a new polygon is created without the "old" polygons .
Just a new polygon ... without the old ones

回答1:
On each public void paintComponent(Graphics g)
call
you create a new polygon add add it to the polygons list.
Polygon poly = new Polygon(this.edges);
// add the polygon to the polygons array
this.polygons.add(poly);
Guess that should happen just one e.g. in the mouseClicked()
processing
回答2:
@Stas code works for me witout any add_ons (added basic stuff cried in IDE), did you meaning final result could be ???

.
EDIT
.
dirty hack is disable super.paintComponent(g);, but proper way should be only
to add all Objects to the array (see quite clear comment by @ trashgod)
create an BurreferImage as Backgroung Image (after Mouse Double_Click)
output by disable super.paintComponent(g);

来源:https://stackoverflow.com/questions/15657010/trying-to-draw-multiple-polygons-results-in-error