2011-03-11 83 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的重新验证和重新绘制方法。