Java图形重绘问题 [英] Java graphics repaint problem

查看:136
本文介绍了Java图形重绘问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在java中使用简单的画笔时出现问题。让我的清除按钮重绘的问题。阵列正在清除,但没有重新绘制。任何人都可以发现我的问题,或者是否有任何不同的方法为此代码生成一个清除按钮。

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.

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

}

推荐答案

如果忘记调用 super.paintComponent(g); 背景未被清除,因此旧图像仍然可见。而且你添加的所有JButton和东西都不会被绘制出来。要解决这个问题,让面板首先绘制自己,然后你可以在它上面绘制你的东西。

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.

@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(..))。这是一个肮脏的黑客:

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

@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() ;

这篇关于Java图形重绘问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆