Image won't display in Applet


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Image won't display in Applet

  1. #1
    Join Date
    Jan 2006
    Posts
    4

    Image won't display in Applet

    Hi people,

    I'm trying to display an image (gif image) in an applet,
    however, it does not show up when the applet is run.

    Here is my code:
    ---------------------------------------------------------
    import java.applet.Applet;
    import java.awt.Graphics;
    import java.awt.Image;

    // An applet that loads an image and
    // displays it.

    public class DrawImage extends Applet
    {
    Image image; //an 'Image' object

    //initialise applet
    public void init()
    {
    //Load Image into object 'image'
    //URL url , String file
    image = getImage(getCodeBase(), "gifpic.gif");
    }

    //applet's paint function
    public void paint(Graphics g)
    {
    if(image != null)
    { //if image is found then drawImage()
    g.drawImage(image, 0, 0, this);
    }
    else
    { //if image is not found, then display message
    System.out.println("image not found");
    }
    }
    }
    -----------------------------------------------------------------------

  2. #2
    Join Date
    Jan 2006
    Posts
    53
    Be sure the image is loacted on the same server the applet is being loaded from. Be sure you have given the correct path to the file. For example, if the gifpic.gif is in /images, make the filename in your applet "/images/gifpic.gif".

    Also, the check for whether the image is null will not work. Checking on whether an image has been loaded is much more complicated than that. I'm sorry that I don't recall what is involved. I haven't done this in a very long time.

Similar Threads

  1. Replies: 3
    Last Post: 06-08-2005, 04:22 AM
  2. Replies: 0
    Last Post: 04-12-2005, 02:27 PM
  3. adding an image to an applet
    By kronic in forum Java
    Replies: 2
    Last Post: 04-05-2005, 10:53 PM
  4. Script for scrolling
    By Mark in forum Web
    Replies: 3
    Last Post: 08-30-2001, 11:45 AM
  5. Replies: 1
    Last Post: 08-01-2000, 01:51 PM

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