因此,我試圖從單擊鼠標開始動態繪製多邊形,直到我停止拖動和釋放。這個問題不是爲了這個問題,而是當我點擊,拖動,然後向右轉,然後向上,然後向左轉,畫出一個方形輪廓,這是發生了什麼事情:http://imgur.com/t8ZN3PpdrawPolygon不斷繪製從開始(mousePressed)位置到當前(mouseDragged)位置的線條
有什麼建議嗎?
注:
model.addPolygon()創建一個與多邊形起點,並將其添加至所謂的「多邊形」
model.addPolygonPoint(一個ArrayList)將指向存儲在此創建的多邊形'多邊形'
我通過多邊形塗料功能迭代畫
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);
完整的paintComponent:
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);
}
}
顯示你的'paintComponent'代碼生成多個多邊形.. – 2013-03-13 21:32:49
@VishalK我將修改完整的代碼,但我認爲它不重要 – Rima 2013-03-13 21:36:31
您必須在mouseDragged方法中用鼠標拖動鼠標點並將它們拖拽到鼠標上之後,才能設置oldX和oldY。 – 2013-03-13 21:40:23