How to convert image to BufferedImage without init?

I am wondering if there is a way to convert Image to BufferedImage without code, for example

new BufferedImage(...) 

because each new init makes the application slower, moreover, if it is in the paint () method: (

Please indicate the most optimal conversion method.

thanks

+4
source share
3 answers

Is there a way to draw a BufferedImage before JLabel using the paint() method?

One convenient approach is to implement the Icon interface. In this Histogram simply drawn when the label indicates repaint() .

If the image source requires a time-consuming operation, such as scaling, pre-render the image as shown in the static factory, GradientImage .

+2
source

No. If the original Image no longer a BufferedImage . Then you can just throw:

 BufferedImage bufImg = null; if (origImage instanceof BufferedImage) { bufImg = (BufferedImage) origImage; else { bugImg = new BufferedImage(...); // proper initialization } 

If it is not a BufferedImage , it could be, for example, VolatileImage (another specific subclass in the API).

From documents on a flying image:

VolatileImage is an image that may lose its contents at any time due to circumstances beyond the control of the application (for example, situations caused by the operating system or other applications).

As you can imagine, such an image cannot provide the same interface as BufferedImage , so the only way to get BufferedImage is to create it and draw the original image on top of it.

+6
source

because every new init makes the application run slower

Load one BufferedImage , and then create a new image if the size of the required size changes. Otherwise, clear the Graphics object of the current instance and make the new drawing you want.

+6
source

All Articles