2011-03-11 104 views
3

在java中遇到了簡單的繪圖板問題。使我的清除按鈕重新繪製的問題。該陣列正在清除,但不會重新繪製。任何人都可以發現我的問題,或者是否有任何不同的方式爲此代碼生成清除按鈕。Java圖形重繪問題

public class DrawingPanel extends JPanel { 
    private double x1=0; 
    private double x2=0; 
    private double y1=0; 
    private double y2=0; 

    private ArrayList<Shape> myArr = new ArrayList<Shape>(); 
    //private ArrayList<Shape> clearMyArr = new ArrayList<Shape>(); 
    ButtonPanel buttonPress; 

    public void paintComponent(Graphics g) 
    { 
    for (Shape i : myArr) 
    { 
     Graphics2D g2d = (Graphics2D)g; 
     g2d.draw(i); 
    } 
     /*for (Shape i : clearMyArr) 
    { 
     Graphics2D g2d = (Graphics2D)g; 
     g2d.draw(i); 
    } */ 

    }   
    //inner class 

    class Listener1 extends MouseAdapter 
    { 
     public void mousePressed(MouseEvent e) 
    { 
     x1=e.getX(); 
     y1=e.getY(); 
     System.out.println("Mouse Pressed"); 
    } 

     public void mouseReleased(MouseEvent e) 
    { 
     x2=e.getX(); 
     y2=e.getY(); 
     Shape shape = null; 
     if (buttonPress.buttonType.equals("Rectangle")) 
     { 
     // Rectangles cannot have a zero width or height 
      if (x1 != x2 || y1 != y2) 
      { 
       double width = Math.abs(x1 -x2); 
       double height = Math.abs(y1-y2); 
       shape = new Rectangle2D.Double(Math.min(x1,x2),Math.min(y1,y2), width, height); 
      } 
     } 
     if (buttonPress.buttonType.equals("Eclipse")) 
     { 
      double width = Math.abs(x1 -x2); 
      double height = Math.abs(y1-y2); 
      shape = new Ellipse2D.Double(Math.min(x1,x2),Math.min(y1,y2), width, height);; 
     } 
     if (buttonPress.buttonType.equals("Lines")) 
     { 
      shape = new Line2D.Double(x1, y1, x2, y2); 

     } 
      if (buttonPress.buttonType.equals("Clear")) 
     { 
       for(int i = 0;i <= myArr.size(); i++) 
       { 
       System.out.println("ArrayList Size :"+myArr.size()); 

       myArr.clear(); // clear all elements from arraylist 
       //clearMyArr.addAll(myArr); 
       System.out.println("ArrayList Size :"+myArr.size()); 

       //myArr.removeAll(); 
       revalidate(); 
       repaint(); 
       } 


     } 

     if (shape != null) 
     { 
      myArr.add(shape); 

     } 
     repaint(); 
    } 


    } 
//end of inner class 

    public DrawingPanel(ButtonPanel reference) 
    { 
    buttonPress = reference; 
    setBorder(BorderFactory.createLineBorder(Color.black,4)); 
    addMouseListener(new Listener1());  
    } 

}

+1

我不明白這個問題。舊的形狀在清除後仍然可見?或者清楚的按鈕沒有被繪製? – Ishtar 2011-03-11 10:28:21

+0

爲了更快提供更好的幫助,請發佈SSCCE(http://pscode.org/sscce.html)。 – 2011-03-11 10:29:53

回答

6

如果您忘記撥打super.paintComponent(g);,背景不會被清除,因此舊圖像仍然可見。所有JButton和你添加的內容都不會被繪製。爲了解決這個問題,讓面板首先繪製自己,然後你可以在上面繪製你的東西。

@Override 
protected void paintComponent(Graphics g) { 
    super.paintComponent(g);// <-- let panel draw itself 
    Graphics2D g2d = (Graphics2D)g; 
    for (Shape i : myArr) 
    { 
     g2d.draw(i); 
    } 
    } 

這工作太(但它不會吸引你加入DrawingPanel.add(..)小部件)。這是一個骯髒的黑客:

@Override 
protected void paintComponent(Graphics g) 
    Graphics2D g2d = (Graphics2D)g; 
    g2d.setColor(Color.grey); 
    g2d.fillRect(0,0,this.getWidth(),this.getHeight()); //<-- clear the background 
    for (Shape i : myArr) 
    { 
     g2d.draw(i); 
    } 
    } 

在聽衆這就夠了。

if (buttonPress.buttonType.equals("Clear")) 
{ 
    myArr.clear(); 
    repaint(); 
} 

您不應該打電話給revalidate();

+0

先生你是個天才,非常感謝你:) – mix2000 2011-03-11 10:59:54

1

嘗試調用重繪();你的JPanel。

+0

按下時的清除按鈕清除已繪製的形狀的數組,但不會將其從jpanel上清除。我試過revalidate(),重繪()但無濟於事。 – mix2000 2011-03-11 10:47:05

1

嘗試從paintcomponent方法調用super.paintcomponent(g)。並確保您正在調用JPanel的重新驗證和重新繪製方法。