简体   繁体   中英

best practice for paintComponent()

I am rendering a rectangle using Graphics library of java, in it paintComponent() method draws the rectangle and everything is fine in drawing regards.

My question is does the paintComponent keeps on rendering or it just renders once and if a window change or any event occurs it changes the view. So I have written a simple System.out.println to check whether it keeps on printing it, and it does. So is there a way to stop that from happening or is it fine to leave rendering as it is, since it is completing my objective?

Start by taking a look at Painting in AWT and Swing and Performing Custom Painting

Swing uses a passive rendering engine, that is, it only paints when it feels that it has to. Painting can happen for any number of reasons, many of which you don't control, but you can make a request to the repaint manager via the repaint method.

So I have written a simple System.out.println to check whether it keeps on printing it, and it does

Would suggest that something is wrong, paintComponent shouldn't, under normal circumstances, be repeatedly called. This can, of course, happen if you are doing animation and need to update the graphics.

So is there a way to stop that from happening or is it fine to leave rendering as it is

Consider providing a runnable example which demonstrates your problem. This will result in less confusion and better responses.

Without some idea of what you are doing, there's no way to know what to suggest...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM