Java paintComponent tell if called again

前端 未结 2 893
野趣味
野趣味 2021-01-29 06:52

Been Googling for a few days and I can\'t figure out how to tell if paintComponent is being called while I\'m processing paintComponent. The process is long and complicated. If

2条回答
  •  时光取名叫无心
    2021-01-29 07:26

    As usual, I think of a solution after asking for one... My solution is to launch a thread that progressively updates a BufferedImage. Then, the paintComponent method is just two lines:

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(myImage, 0, 0, null);
    }
    

    The image is drawn very quickly. Calling super is required to erase the existing view of the image before redrawing it.

    The rest of the solution is creating a thread that updates the myImage attribute of the panel. In my work, the image takes about 30 minutes to process completely, depending on the input to the program. It isn't a big deal unless I drag the window or accidentally touch the mouse wheel while the application is running. Then, paintComponent is called again and hangs the program. With this solution, the update can take days and you can resize or move the window all you want. You keep seeing the image as it has been processed so far.

提交回复
热议问题