Image not load(first time) when I use java.awt.Image and java.awt.RenderingHints;

145 Views Asked by At

When I use this library of java java.awt.Image and java.awt.RenderingHints. The first time that web page try to load, the image is in black. If refresh the page, the image is perfectly loaded.

How I can do it for in the first time the image is perfectly loaded?

File photo = new File(filePath);
if (photo.exists()) {

Toolkit toolkit = Toolkit.getDefaultToolkit();
RenderingHints antialias = new RenderingHints(
        RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON);
RenderingHints interpola = new RenderingHints(
        RenderingHints.KEY_INTERPOLATION,
        RenderingHints.VALUE_INTERPOLATION_BILINEAR);

Image image = toolkit.getImage(photo.getPath());

int newWidth = width;
int newHeight = height;

// Pintamos la imagen redimensionada en un buffer
BufferedImage bufferedImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = bufferedImage.createGraphics();
graphics.addRenderingHints(antialias);
graphics.addRenderingHints(interpola);
graphics.drawImage(image, 0, 0, newWidth, newHeight, null);
0

There are 0 best solutions below