0

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);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
csoriano
  • 21
  • 3
  • Change `graphics.drawImage(image, 0, 0, newWidth, newHeight, null);` to `graphics.drawImage(image, 0, 0, newWidth, newHeight, this);` .. 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Jun 25 '15 at 11:19
  • Thanks for the comment, I don't code an applet, is a web application. I search SWT and i think that is the correct library – csoriano Jul 03 '15 at 12:48

0 Answers0