所以,我试图从单击鼠标开始动态绘制多边形,直到我停止拖动和释放。而不是,为了这个问题,当我点击时绘制一个方形轮廓,向下拖动,然后向右,然后向上,然后向左,这就是发生的事情:http: //imgur.com/t8ZN3Pp
有什么建议么?
笔记:
model.addPolygon() 创建一个带有起点的多边形并将其添加到一个名为“polys”的 ArrayList
model.addPolygonPoint() 将点添加到存储在“polys”中的这个创建的多边形中
我的绘画功能通过多边形迭代绘画
public void mousePressed(MouseEvent e) {
oldX = e.getX();
oldY = e.getY();
model.addPolygon(oldX, oldY);
}
public void mouseDragged(MouseEvent e) {
currentX = e.getX();
currentY = e.getY();
model.addPolyPoint(currentX, currentY);
repaint();
}
. . . 然后在paintComponent中:
for (ListIterator<Polys> iter =
model.polys.listIterator(model.polys.size());
iter.hasPrevious();){
graphics2D.draw(iter.previous().poly);
全漆组件:
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (image == null) {
image = createImage(getSize().width, getSize().height);
graphics2D = (Graphics2D) image.getGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
}
g.drawImage(image, 0, 0, null);
for (ListIterator<Polys> iter =
model.polys.listIterator(model.polys.size());
iter.hasPrevious();){
graphics2D.draw(iter.previous().poly);
}
}