java Java图形重绘问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5271495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 10:21:20  来源:igfitidea点击:

Java graphics repaint problem

javaswinguser-interfacerepaintgraphics2d

提问by mix2000

Having trouble with a simple paint pad in java. Issues with getting my clear button to repaint. The array is clearing but not repainting. Can anyone spot my problem or is there any different way of generating a clear button for this code.

在 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());      
  }

}

}

回答by Ishtar

If you forget to call super.paintComponent(g);the background does not get cleared, so the old image will still be visible. And all JButton's and stuff you added, will not be drawn. To fix this, let the panel draw itself first, then you can draw your stuff on top of it.

如果忘记调用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);
     }   
  }

This works too (except that it does not draw widgets you added with DrawingPanel.add(..)). It's a dirty hack:

这也有效(除了它不绘制您添加的小部件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);
     }   
  }

In the listener this would be enough.

在侦听器中,这就足够了。

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

You shouldn't have to call revalidate();.

你不应该打电话revalidate();

回答by Samra

Try to call super.paintcomponent(g) from the paintcomponent method. And also make sure you are calling the revalidate and repaint method of JPanel.

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

回答by Erkan Haspulat

Try to call repaint(); of your JPanel.

尝试调用 repaint(); 你的JPanel。