How to copy bufferedImage

后端 未结 4 930
迷失自我
迷失自我 2021-01-28 02:48

I have written the following example to give something runnable of a problem I am having. When you press the button the controlWhichImage switches to 2. The problem is that when

4条回答
  •  情话喂你
    2021-01-28 03:32

    The problem is that getGraphics (or better named createGraphics) is called outside the if statement, also for 2, hence both causing a resource leak (as no g2.dispose is called), and also a clean slate.

        if (controlWhichImage == 1) {
            Graphics g2 = createdImage.getGraphics();      
            g2.drawImage(img,0,0,img.getWidth(),img.getHeight(),null);
            g2.dispose();
        }
    

    Also do things like loading the image outside the paint code.

提交回复
热议问题