试图绘制多个多边形导致错误
随着我的previous后here ,我改变了code为:
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);
}
}
现在,这个code的结果:
Now , this code results in :
和我不明白是什么原因导致这一点。
And I can't understand what causes this .
这是我所做的改变是:当我们用一个多边形做的,它的坐标每次都保存在类多边形
的ArrayList和我创建一个新的多边形,我拿
在previous多边形和吸引他们,同时绘制新的多边形。
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 .
我倒是AP preciate任何帮助。
I'd appreciate any help .
感谢
编辑:
考虑到什么@StanislavL说后,我搬到那些行的mouseClicked()
,但这一次,每次我得到一个新的屏幕,创建一个新的多边形没有老的多边形。
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
@Stas code对我的作品不用其他任何add_ons(加入基本的东西在IDE哭了),你有没有这意味着最后的结果可能是???
@Stas code works for me witout any add_ons (added basic stuff cried in IDE), did you meaning final result could be ???
修改
肮脏的黑客是禁止super.paintComponent方法(G);,但适当的方式应该是唯一的。
dirty hack is disable super.paintComponent(g);, but proper way should be only
-
所有对象添加到阵列中(见@ trashgod很清楚评论)
to add all Objects to the array (see quite clear comment by @ trashgod)
创建BurreferImage作为Backgroung图像(后鼠标DOUBLE_CLICK)
create an BurreferImage as Backgroung Image (after Mouse Double_Click)
通过禁用super.paintComponent方法(G)输出;
output by disable super.paintComponent(g);