I'm not a JAVA guru, but I thought I could handle some simple image creation. Well, looks like I was too overconfident :lol:

import java.applet.*;
import java.awt.Graphics;
import java.awt.Image;

public class test extends Applet  {
  private Image img1, img2;

  public test() {
  }
 
  public void start() {
   
  }
 
  public void stop() {
   
  }
 
  public void paint(Graphics g) {

    // paint the images
    g.drawImage(img1, 0, 0, 266, 200, null);
    g.drawImage(img2, 266, 200, 266, 200, null);
  }
 
  public void init() {
    Image tmp1, tmp2;
    Graphics tg1, tg2;

    // load the images
    tmp1 = getImage(getDocumentBase(), "b.png");
    tmp2 = getImage(getDocumentBase(), "c.jpg");
   
    // create new images
    img1 = createImage(533, 400);
    img2 = createImage(533, 400);
   
    // copy the loaded images onto the created ones
    tg1 = img1.getGraphics();
    tg2 = img2.getGraphics();
    tg1.drawImage(tmp1, 0, 0, null);
    tg2.drawImage(tmp2, 0, 0, null);
  }
}

The above code is supposed to display 2 images on the applet's screen space. Actually, it does, but only after you refresh your browser's window. Why is that?

I encountered this problem while making some game applet. I noticed that some images weren't displayed for the first time they were supposed to be displayed. But these same images were properly displayed at any subsequent time. This forced me to write this test app, so I could isolate the problem. To make things even more strange, simple "repaint();repaint();" doesn't fix the problem. Even a delayed repaint() doesn't, so it looks like it's not a problem with decompression time. If you have any ideas, your help will be appreciated ^^
Attachments:
Posted on 2007-02-04 20:19:27 by ti_mo_n
I found a hint here. Adding
my_image = new ImageIcon(my_image).getImage();

solves the problem. Unfortunately, I have no idea how it works and why it works. I don't know why the app doesn't work without it, either. Looks like it DOES have something to do with the decompression and/or pre-loading/pre-caching of images.
The fixed version of the above code should look like this:
import java.applet.*;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;

public class test extends Applet  {
  private Image img1, img2;

  public test() {
  }
 
  public void start() {
   
  }
 
  public void stop() {
   
  }
 
  public void paint(Graphics g) {

    // paint the images
    g.drawImage(img1, 0, 0, 266, 200, null);
    g.drawImage(img2, 266, 200, 266, 200, null);
  }
 
  public void init() {
    Image tmp1, tmp2;
    Graphics tg1, tg2;

    // load the images
    tmp1 = getImage(getDocumentBase(), "b.png");
    tmp2 = getImage(getDocumentBase(), "c.jpg");

    // ??? The follwing makes icons from the images and then
    // retieves images back from from these icons. ???
    tmp1 = new ImageIcon(tmp1).getImage(); // this line solves the problem for 'tmp1'. uses javax.swing.ImageIcon
    tmp2 = new ImageIcon(tmp2).getImage(); // this line solves the problem for 'tmp2'. uses javax.swing.ImageIcon
   
    // create new images
    img1 = createImage(533, 400);
    img2 = createImage(533, 400);
   
    // copy the loaded images onto the created ones
    tg1 = img1.getGraphics();
    tg2 = img2.getGraphics();
    tg1.drawImage(tmp1, 0, 0, this);
    tg2.drawImage(tmp2, 0, 0, this);
  }
}
Posted on 2007-02-04 20:47:34 by ti_mo_n
How does this fare:

public class test extends Applet  {
  private boolean AreImagesLoaded;
  private Image img1, img2;
  private Image tmp1, tmp2;
  private Graphics tg1, tg2;

  public test() {
  super();
 
  }
 
  public void start() {
   
  }
 
  public void stop() {
   
  }
 
  public void paint(Graphics g) {
  if(!AreImagesLoaded){
  // create new images
    img1 = createImage(533, 400);
    img2 = createImage(533, 400);
  // copy the loaded images onto the created ones
 
    tg1 = img1.getGraphics();
    tg2 = img2.getGraphics();
    tg1.drawImage(tmp1, 0, 0, null);
    tg2.drawImage(tmp2, 0, 0, null);   
    AreImagesLoaded=true;
  }

    // paint the images
    g.drawImage(img1, 0, 0, 266, 200, null);
    g.drawImage(img2, 266, 200, 266, 200, null);
    g.fillRect(10,10,10,10);
  }
 
  public void init() {
    // load the images
    tmp1 = getImage(getCodeBase(), "/b.png");
    tmp2 = getImage(getCodeBase(), "/c.jpg");
  }
}

Posted on 2007-02-04 21:24:16 by Ultrano
Hi Ultrano.
Your code doesn't seem to work at all (it does display a small rectangle, but no images), which is quite illogical for me xD Man, looks like JAVA has more bugs than I thought :) IMHO, It's kinda 'unclean' that I have to use javax.swing to do the loading :/
Attachments:
Posted on 2007-02-09 19:04:17 by ti_mo_n
Well, just replace the "getCodeBase" with "getDocumentBase" on both places.
Looks unclean? Wait till you try to implement blend-drawing on J2ME :) .
Posted on 2007-02-09 19:35:51 by Ultrano