开发者

Graphics contents get erased on maximized

开发者 https://www.devze.com 2023-04-06 16:43 出处:网络
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 ma

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 sel开发者_开发问答ected ");
    }
    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)
    {
    }
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号