2013-03-13 81 views
1

因此,我試圖從單擊鼠標開始動態繪製多邊形,直到我停止拖動和釋放。這個問題不是爲了這個問題,而是當我點擊,拖動,然後向右轉,然後向上,然後向左轉,畫出一個方形輪廓,這是發生了什麼事情: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); 

     } 
    } 
+0

顯示你的'paintComponent'代碼生成多個多邊形.. – 2013-03-13 21:32:49

+0

@VishalK我將修改完整的代碼,但我認爲它不重要 – Rima 2013-03-13 21:36:31

+0

您必須在mouseDragged方法中用鼠標拖動鼠標點並將它們拖拽到鼠標上之後,才能設置oldX和oldY。 – 2013-03-13 21:40:23

回答

1
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseMotionAdapter; 

public class Testing { 

    private static int lastX; 
    private static int lastY; 

    private static int modX; 
    private static int modY; 

    private static final BasicStroke STROKE = new BasicStroke(2.0F); 

    private static final Point[] SHAPE = new Point[]{ 

      new Point(10, 10), 
      new Point(10, 40), 
      new Point(60, 90), 
      new Point(50, 50) 

    }; 

    public static void main(final String[] args) { 

     final JFrame frame = new JFrame("Polygon Movement"); 
     final JPanel pane = new JPanel() { 
      @Override 
      public void paintComponent(final Graphics g1) { 
      final Graphics2D g = (Graphics2D) g1; 
      g.setColor(Color.RED); 
      g.translate(modX, modY); 
      g.setStroke(STROKE); 
      for (int i = 0; i < SHAPE.length; i++) { 
       g.drawLine(SHAPE[i].x, SHAPE[i].y, SHAPE[(i + 1) % SHAPE.length].x, SHAPE[(i + 1) % SHAPE.length].y); 
      } 
     } 
    }; 
    pane.addMouseMotionListener(new MouseMotionAdapter() { 
     @Override 
     public void mouseDragged(MouseEvent e) { 
      modX += e.getX() - lastX; 
      modY += e.getY() - lastY; 
      lastX = e.getX(); 
      lastY = e.getY(); 
      frame.repaint(); 
     } 
    }); 
    pane.addMouseListener(new MouseAdapter() { 
     @Override 
     public void mousePressed(MouseEvent e) { 
      lastX = e.getX(); 
      lastY = e.getY(); 
     } 
    }); 

     pane.setPreferredSize(new Dimension(200, 200)); 
     frame.add(pane); 
     frame.pack(); 
     frame.setVisible(true); 

    } 

} 

正如你所看到的,我就與定義的點基本形狀。這是最有效的方法,除非你想改變基本形狀(這裏是靜態的)。在這種情況下,你會發現它正在抓住並修改那個鼠標。無論哪種方式,都不需要添加或刪除點。我優先選用lastX而不是oldX

BasicStroke非常可選,與鑄造到Graphics2D對象相同。

行:

g.drawLine(SHAPE[i].x, SHAPE[i].y, SHAPE[(i + 1) % SHAPE.length].x, SHAPE[(i + 1) % SHAPE.length].y); 

應該做一些意義,如果你已經嘗試過這件事。它遍歷所有點,從當前(SHAPE[i])到下一個(SHAPE[(i + 1) & SHAPE.length)繪製一條線。

這個邏輯背後的原因是,說你有4分,就像我們在這裏做的那樣。通過他們的最後一次迭代,你將得到i = 3。由於這個以及只包含4個索引(0-3)的數組,我們必須將該值降回零。爲了簡單起見,我使用% SHAPE.length,所以不需要特殊情況。

我也選擇使用適配器,因爲只有7種可能的方法需要2種方法。

如果您有任何問題隨時問我這件事。

〜圖例

+0

謝謝,這應該幫助我做我想要的。這只是我在Java中的第三週編碼,所以我仍然試圖儘可能地學習。 – Rima 2013-03-13 23:01:59

+0

這是一個'需要知道'一點。有幾個。他們只是完成任務的基本方式。 – 2013-03-13 23:08:06

0

如果您只想繪製一個多邊形。您可以簡單地使用Shape API

這將允許您「添加」指向形狀並允許形狀自己繪製。

這裏我使用一個簡單的Path2D形狀,因爲它允許我隨着時間追加點。我保持形狀的運行列表,這讓我的要求

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Point; 
import java.awt.Shape; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.awt.geom.Line2D; 
import java.awt.geom.Path2D; 
import java.util.ArrayList; 
import java.util.List; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class DrawPolygon { 

    public static void main(String[] args) { 
     new DrawPolygon(); 
    } 

    public DrawPolygon() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException ex) { 
       } catch (InstantiationException ex) { 
       } catch (IllegalAccessException ex) { 
       } catch (UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame("Test"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new PolyPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 

     }); 
    } 

    public static class PolyPane extends JPanel { 

     private MouseHandler mouseHandler; 
     private Path2D currentShape; 
     private List<Path2D> lstPloys; 
     private Point lastPoint; 
     private Point currentPoint; 

     public PolyPane() { 
      lstPloys = new ArrayList<>(); 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(200, 200); 
     } 

     @Override 
     public void addNotify() { 
      super.addNotify(); 
      addMouseListener(getMouseHandler()); 
      addMouseMotionListener(getMouseHandler()); 
     } 

     @Override 
     public void removeNotify() { 
      removeMouseListener(getMouseHandler()); 
      removeMouseMotionListener(getMouseHandler()); 
      super.removeNotify(); 
     } 

     public MouseHandler getMouseHandler() { 
      if (mouseHandler == null) { 
       mouseHandler = new MouseHandler(); 
      } 
      return mouseHandler; 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      Graphics2D g2d = (Graphics2D) g.create(); 
      if (lastPoint != null) { 
       g2d.setColor(Color.RED); 
       g2d.fillOval(lastPoint.x - 2, lastPoint.y - 2, 4, 4); 
      } 
      if (currentShape != null) { 
       g2d.setColor(Color.RED); 
       g2d.draw(currentShape); 
       if (lastPoint != null && currentPoint != null) { 
        System.out.println(lastPoint + " - " + currentPoint); 
        g2d.setColor(new Color(255, 0, 0, 64)); 
        g2d.draw(new Line2D.Float(lastPoint, currentPoint)); 
       } 
      } 
      g2d.setColor(Color.BLACK); 
      for (Shape shape : lstPloys) { 
       g2d.draw(shape); 
      } 
      g2d.dispose(); 
     } 

     public class MouseHandler extends MouseAdapter { 

      @Override 
      public void mouseClicked(MouseEvent e) { 
       if (e.getButton() == MouseEvent.BUTTON1) { 
        if (e.getClickCount() == 1) { 
         Point p = e.getPoint(); 
         lastPoint = p; 
         if (currentShape == null) { 
          currentShape = new Path2D.Float(); 
          currentShape.moveTo(p.x, p.y); 
         } else { 
          currentShape.lineTo(p.x, p.y); 
         } 
         repaint(); 
        } else if (e.getClickCount() == 2) { 
         currentShape.closePath(); 
         lstPloys.add(currentShape); 
         currentShape = null; 
         lastPoint = null; 
         repaint(); 
        } 
       } 
      } 

      @Override 
      public void mouseMoved(MouseEvent e) { 
       if (currentShape != null) { 
        currentPoint = e.getPoint(); 
        repaint(); 
       } else { 
        currentPoint = null; 
       } 
      } 

     } 

    } 

} 

看看Working with Geometry更多細節

相關問題