The BufferedImage is just an image in memory - it doesn't have a location. It's the paintComponent() calls that render that image to the graphics context. So yes, if you have a reference to the graphics context you can draw that BufferedImage to it.

The BufferedImage is just an image in memory - it doesn't have a location. It's the paintComponent() calls that render that image to the graphics context. So yes, if you have a reference to the graphics context you can draw that BufferedImage to it.

Good to know. Will this allow me to create an image without any flickering when updating?

Thanks!

Yes, an off-screen buffer image can be used for such things. If you are actually recreating the image each display cycle it won't really help much, but if the image is only changed occasionally, or you only need to redraw parts of it, it can help quite a bit.

They can be very helpful when you have a constant "background" image (like a game board perhaps) that you draw other things on top of a lot, as the background image only needs to be created from scratch once.

As a general guideline, try to keep the code in paintComponent() as minimal as possible, since repaints occur frequently due to other events besides your own repaint() calls. If your drawing routines are math intensive, try to perform those as infrequently as possible in a separate method and let the painting code just use the results of those calculations.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.