Images will not display


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Images will not display

  1. #1
    Join Date
    Jan 2005
    Posts
    2

    Images will not display

    I read the applet game tutorial written by that German guy. However, the code he uses to display images doesn't seem to work. No image is displayed at all. I've looked my problem up at java.com and have been utterly astonished at the complexity of their answers. So anywho, I'm on Java 2, SE v1.4.2_06. The code I got from the tutorial was:

    Image backImage;
    ...
    int(){ backImage = getImage(getCodeBase(), "land.gif");}
    ...
    paint(Graphics g){g.drawImage(backImage, 0, 0, this)};

    The whole point of the image is as a background.

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Loading images in applets (and sometimes in applications)
    requires a few more code lines. The thing is that the code:

    backImage = getImage(getCodeBase(), "land.gif");

    doesn't get you the image right away, it has to download completely
    to the client before you can display it. If you attempt to
    draw it before all its bytes have arrived you'll see nothing
    or just parts of it. So, you have to wait for it.

    Provided that the image is really located at the applets codebase
    you cold try this (I'm loading two images here just for
    demonstration)

    Code:
    boolean imagesOK=false;
    Dimension imgDimA=null;
    Dimension imgDimB=null;
    .
    .
    MediaTracker mt=new MediaTracker(this);
    backImageA = getImage(getCodeBase(), "land1.gif");
    mt.addImage(backImageA,0);
    backImageB = getImage(getCodeBase(), "land2.gif");
    mt.addImage(backImageB,1);
    try {
      mt.waitForAll();
    } catch (InterruptedException ie) {
      return; // just leave, the client probably left your page 
    }
    imgDimA=new Dimension(backImageA.getWidth(this), backImageA.getHeight(this));
    imgDimB=new Dimension(backImageB.getWidth(this), backImageB.getHeight(this));
    
    if (imgDimA.width > 0 && imgDimA.height > 0 &&
       (imgDimB.width > 0 && imgDimB.height > 0) {
      imagesOK=true;
    }
    Another nice habit:

    Code:
    public void update(Graphics g) {
      if (imagesOK) {
        g.drawImage(backImageA, 0, 0, this);
      } else {
        g.drawString("Loading images",0,0);
      }
    }
    public void paint(Graphics g) {
      update(g);
    }
    If you are using IE, and you are not doing it already I recommend that you bring up the java console (tools->sun java console) and
    check for exceptions stacktraces.
    eschew obfuscation

  3. #3
    Join Date
    Jan 2005
    Posts
    2
    Aha! Success! You're a good person sjalle. Thanks for the extra code and the helpful little tidbits.

    EDIT: ACH! It actually still refuses to work for larger images! For the little ball, the image displays correctly. However, the same technique still doesn't work on the other program with a large gif pic for the background.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center