2009-09-26 139 views
1

我有顯示所有三角形的問題。我用鼠標拖動畫一個三角形。每次我畫一個新的三角形,前一個三角形消失。我怎樣才能讓三角形留下來,所以繪圖面板上會有很多三角形?鼠標拖動和繪製

..... 
private class PaintSurface extends JComponent { 
    Point startDrag, endDrag, midPoint; 
    Polygon triangle; 
    public PaintSurface() { 
     this.addMouseListener(new MouseAdapter() { 
     public void mousePressed(MouseEvent e) { 
     startDrag = new Point(e.getX(), e.getY()); 
     endDrag = startDrag; 
     repaint(); 
     } 

     public void mouseReleased(MouseEvent e) { 
     Shape r = makeRectangle(startDrag.x, startDrag.y, e.getX(), e.getY());   
     shapes.add(r); 
     if (startDrag.x > endDrag.x) 
      midPoint = new Point((endDrag.x +(Math.abs(startDrag.x - endDrag.x)/2)),e.getY()); 
     else 
      midPoint = new Point((endDrag.x -(Math.abs(startDrag.x - endDrag.x)/2)),e.getY()); 
     int[] xs = { startDrag.x, endDrag.x, midPoint.x }; 
     int[] ys = { startDrag.y, startDrag.y, midPoint.y };   
     triangle = new Polygon(xs, ys, 3);    
     startDrag = null; 
     endDrag = null; 
     repaint(); 
     } 
    }); 

    this.addMouseMotionListener(new MouseMotionAdapter() { 
     public void mouseDragged(MouseEvent e) { 
      endDrag = new Point(e.getX(), e.getY()); 
      repaint(); 
     } 
    }); 
    } 

    public void paint(Graphics g) { 
    Graphics2D g2 = (Graphics2D) g; 
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
    paintBackground(g2); 
    Color[] colors = { Color.YELLOW, Color.MAGENTA, Color.CYAN , Color.RED, Color.BLUE, Color.PINK}; 
    int colorIndex = 0; 
    g2.setStroke(new BasicStroke(1)); 
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.50f)); 
    g2.fillPolygon(triangle);   
    } 
} 
+0

風俗畫應在的paintComponent()方法來完成,而不是paint()方法。 – camickr

回答

3

你在屏幕上看到的是你在繪畫功能中繪製的東西。目前,您只能在三角形變量中存儲一個三角形,並不斷更換和繪製它。

你需要的是存儲一個三角形列表,並且每次添加一個新的到mouseReleased的列表。這裏是要改變什麼:

private class PaintSurface extends JComponent { 
    ... 
    //Polygon triangle; 
    List<Polygon> triangles = new LinkedList<Polygon>(); 
    ... 

    public PaintSurface() { 

     public void mouseReleased(MouseEvent e) { 
     ... 
     //triangle = new Polygon(xs, ys, 3); 
     triangles.add(new Polygon(xs, ys, 3);); 
     ... 
     } 
    }); 
    ... 
    } 

    public void paint(Graphics g) { 
    ... 
    //g2.fillPolygon(triangle); 
    for (Polygon triangle : triangles) g2.fillPolygon(triangle); 
    ... 
    } 
} 
+0

謝謝Zed,這非常有幫助,解決了我的問題。 – Jessy

1
Polygon triangle; 

你到總計一個三角形對象的引用,所以每次重繪畫布時,你會得出這樣的一個多邊形。

一對夫婦的解決方案:

  • 創建Polygon對象的列表,並吸引他們所有當paint被調用。您的鼠標偵聽器會向列表中添加一個新條目。
  • 將您的多邊形繪製到BufferedImage上,然後在繪製方法中使用draw the image。如果你計劃繪製許多三角形,這可能是更好的選擇。
+0

感謝您的解答:-) – Jessy

1

不是很奇怪。您有一個三角形對象,只要您開始畫一個新的三角形,就會覆蓋它。

創建一個三角形的數組/列表,並讓繪圖例程繪製該圖形。

我的Java修改:

..... 
    private class PaintSurface extends JComponent { 
    Point startDrag, endDrag, midPoint; 
    List triangles = new ArrayList(); 
    public PaintSurface() { 
     this.addMouseListener(new MouseAdapter() { 
     public void mousePressed(MouseEvent e) { 
      startDrag = new Point(e.getX(), e.getY()); 
      endDrag = startDrag; 
      repaint(); 
    } 

    public void mouseReleased(MouseEvent e) { 
    Shape r = makeRectangle(startDrag.x, startDrag.y, e.getX(), e.getY());   
    shapes.add(r); 
    if (startDrag.x > endDrag.x) 
     midPoint = new Point((endDrag.x +(Math.abs(startDrag.x - endDrag.x)/2)),e.getY()); 
    else 
     midPoint = new Point((endDrag.x -(Math.abs(startDrag.x - endDrag.x)/2)),e.getY()); 
    int[] xs = { startDrag.x, endDrag.x, midPoint.x }; 
    int[] ys = { startDrag.y, startDrag.y, midPoint.y };   
    Polygon triangle = new Polygon(xs, ys, 3);    
    traingles.add(triangle); 
    startDrag = null; 
    endDrag = null; 
    repaint(); 
    } 
}); 

this.addMouseMotionListener(new MouseMotionAdapter() { 
    public void mouseDragged(MouseEvent e) { 
     endDrag = new Point(e.getX(), e.getY()); 
     repaint(); 
    } 
}); 
    } 

    public void paint(Graphics g) { 
     Graphics2D g2 = (Graphics2D) g; 
     g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
     paintBackground(g2); 
     Color[] colors = { Color.YELLOW, Color.MAGENTA, Color.CYAN , Color.RED, Color.BLUE, Color.PINK}; 
     int colorIndex = 0; 
     g2.setStroke(new BasicStroke(1)); 
     g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.50f)); 
     Iterator iter = triangles.iterator(); 
     while(iter.hasNext()) { 
      g2.fillPolygon((Polygon)iter.next());   
     } 
    } 
    } 
+0

感謝您的代碼:-) – Jessy