Java图形重绘问题

3

我在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
为了更快获得更好的帮助,请发布一个 SSCCE(http://pscode.org/sscce.html)。 - Andrew Thompson
3个回答

7
如果您忘记调用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(); 方法。


2
尝试从paintcomponent方法中调用super.paintcomponent(g)。并确保您正在调用JPanel的revalidate和repaint方法。

1
尝试调用您的JPanel的repaint()方法。

清除按钮被按下时会清除已绘制的形状数组,但不会将它们从jpanel上清除。我尝试过revalidate()、repaint(),但都没有成功。 - mix2000

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接