图形内容在最大化上被删除

发布于 2024-12-05 06:59:40 字数 2748 浏览 1 评论 0原文

我正在开发一个 GUI 绘画应用程序。但问题是,在最小化然后最大化时,帧内容会被擦除。我知道问题是什么,就是最大化后再次调用油漆并且框架的内容没有正确处理。但即使知道错误后我也无法解决该行为。

这是负责此行为的主要代码:

public void paint(Graphics g) {
    /*From minimized to maximized*/
    System.err.println("Inside paint now ");
   try{
    if(color==colour.red)
        g.setColor(Color.red);
    else if(color==colour.green)
        g.setColor(Color.green);
    else if(color==colour.pink)
        g.setColor(Color.PINK);
    else if(color==colour.cyan)
        g.setColor(Color.CYAN);
    else if(color==colour.magenta)
        g.setColor(Color.MAGENTA);
    else if(color==colour.orange)
        g.setColor(Color.ORANGE);
    else if(color==colour.white)
        g.setColor(Color.white);
    else if(color==colour.black)
        g.setColor(Color.BLACK);
    else if(color==colour.yellow)
    {
        g.setColor(Color.YELLOW);
        System.out.println("Colour selected ");
    }
    else if(stat==state.line)
    {
            Point p=getMousePosition();

            x=p.x;y=p.y;
            System.out.append("In here ");
            System.out.append("the point is "+x+" "+y);
            if(prevx==0 && prevy==0)
            {
                g.drawLine(x, y, x, y);
            //    System.out.append("the point is "+x+" "+y);
            }
            else
            g.drawLine(x, y, prevx, prevy);
            prevx=x;
            prevy=y;
            System.out.println("Line selected ");
    }
    else  if(stat==state.rectangle)
    {
          if(x<prevx)
          {
                x=x+prevx;
                prevx=x-prevx;
                x=x-prevx;
          }
          if(y<prevy)
          {
                y=y+prevy;
                prevy=y-prevy;
                y=y-prevy;
          }
          g.drawRect(prevx,prevy,x-prevx,y-prevy);
          System.out.println("Rectangle selected ");
     }
     else if(stat==state.circle)
     {
            if(x<prevx)
            {
                x=x+prevx;
                prevx=x-prevx;
                x=x-prevx;
            }
            if(y<prevy)
            {
                y=y+prevy;
                prevy=y-prevy;
                y=y-prevy;
            }
            g.drawOval(prevx,prevy,x-prevx,y-prevy);
            System.out.println("Circle selected ");
     }
     else  if(stat==state.ruler)
     {
            g.drawLine(x, y, prevx, prevy);
            System.out.println("Ruler selected ");
     }
     else if(stat==state.eraser)
     {
            g.setColor(getBackground());
            g.fillRect(x-10, y-10, 20, 20);
            System.out.println("Eraser selected ");
     }
     else
     {
           System.out.append("nothing done");
     }
    }
    catch(Exception e)
    {
    }
}

I am developing a GUI painting application. But the problem is that the frame contents get erased on minimizing and then maximizing. I know what the problem is, it's that paint is called again after maximizing and the frame's contents are not handled properly. But I could not solve the behavior even after knowing the error.

Here is the main code responsible for this behavior:

public void paint(Graphics g) {
    /*From minimized to maximized*/
    System.err.println("Inside paint now ");
   try{
    if(color==colour.red)
        g.setColor(Color.red);
    else if(color==colour.green)
        g.setColor(Color.green);
    else if(color==colour.pink)
        g.setColor(Color.PINK);
    else if(color==colour.cyan)
        g.setColor(Color.CYAN);
    else if(color==colour.magenta)
        g.setColor(Color.MAGENTA);
    else if(color==colour.orange)
        g.setColor(Color.ORANGE);
    else if(color==colour.white)
        g.setColor(Color.white);
    else if(color==colour.black)
        g.setColor(Color.BLACK);
    else if(color==colour.yellow)
    {
        g.setColor(Color.YELLOW);
        System.out.println("Colour selected ");
    }
    else if(stat==state.line)
    {
            Point p=getMousePosition();

            x=p.x;y=p.y;
            System.out.append("In here ");
            System.out.append("the point is "+x+" "+y);
            if(prevx==0 && prevy==0)
            {
                g.drawLine(x, y, x, y);
            //    System.out.append("the point is "+x+" "+y);
            }
            else
            g.drawLine(x, y, prevx, prevy);
            prevx=x;
            prevy=y;
            System.out.println("Line selected ");
    }
    else  if(stat==state.rectangle)
    {
          if(x<prevx)
          {
                x=x+prevx;
                prevx=x-prevx;
                x=x-prevx;
          }
          if(y<prevy)
          {
                y=y+prevy;
                prevy=y-prevy;
                y=y-prevy;
          }
          g.drawRect(prevx,prevy,x-prevx,y-prevy);
          System.out.println("Rectangle selected ");
     }
     else if(stat==state.circle)
     {
            if(x<prevx)
            {
                x=x+prevx;
                prevx=x-prevx;
                x=x-prevx;
            }
            if(y<prevy)
            {
                y=y+prevy;
                prevy=y-prevy;
                y=y-prevy;
            }
            g.drawOval(prevx,prevy,x-prevx,y-prevy);
            System.out.println("Circle selected ");
     }
     else  if(stat==state.ruler)
     {
            g.drawLine(x, y, prevx, prevy);
            System.out.println("Ruler selected ");
     }
     else if(stat==state.eraser)
     {
            g.setColor(getBackground());
            g.fillRect(x-10, y-10, 20, 20);
            System.out.println("Eraser selected ");
     }
     else
     {
           System.out.append("nothing done");
     }
    }
    catch(Exception e)
    {
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海不蓝 2024-12-12 06:59:41

我的投票是 mkrhrts 的评论,即当您应该覆盖 paintComponent 时,您却覆盖了 paint

覆盖 paint 是不好的做法,并且您似乎没有执行以下任何操作来确保它不会中断:

  • call super.paint()
  • call paintComponentpaintBorderpaintChildren

查看 paint 方法的文档

My vote is mkrhrts's comment, that you're overriding paint when you're supposed to override paintComponent.

Overriding paint is bad practice, and you don't appear to be doing any of the following to ensure it doesn't break:

  • call super.paint()
  • call paintComponent, paintBorder, or paintChildren

Check out the documentation for the paint method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文