Remote image problem

My applet is trying to display images, i use the code below load the image. The code works ok with appletviewer and also offline browsing using netscape with jre1.2.2 plugged in. However, when I put it on a web server and download it to view, problems occur. Only **part of the image** can be shown(this implies no problem with browser sercurity, download problem, right?). Even if I have tried to jar all the files, the same situation occurs.

Please help! Thanks in advance.

try {
  MediaTracker m = new MediaTracker(this);
  InputStream is = getClass().getResourceAsStream("image.gif");
  BufferedInputStream bis = new BufferedInputStream(is);
  byte[] bytes = new byte[is.available()];
  is.read(bytes);
  image = Toolkit.getDefaultToolkit().createImage(bytes);
  m.addImage(image, 0);
  m.waitForAll();
}
  catch(Exception e) {
  e.printStackTrace();
}

~~~~~~
public void paintComponent(Graphics g) {
    g.drawImage(image, 0, 0, this);
}
agneskoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ravindra76Commented:


try with this code.



try {
  MediaTracker m = new MediaTracker(this);
  InputStream is = getClass().getResourceAsStream("Image.gif");
  BufferedInputStream bis = new BufferedInputStream(is);
  byte[] bytes = new byte[10000];
  int byteRead = bis.read(bytes,0,10000);
  img =   Toolkit.getDefaultToolkit().createImage(bytes,0,byteRead);
  m.addImage(img, 0);
  m.waitForAll();
  }
 catch(Exception e) {
  e.printStackTrace();
  }

I think browser will take some time to load the image.

wait for some time
0
agneskoAuthor Commented:
In fact I have tried this code before, this code is working only with the appletviewer. For netscape and IE, the problem of partial image occur again. My code is working with appletviewer, offline viewing with netscape and ie, but not online.

0
agneskoAuthor Commented:
another point:
When i debug the program with the code the same as that provided by ravindra76 using netscape, i found that the byteRead is not the same as the size of the image. When using appletviewer,  the byteRead is the same as the size of the image.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

heyhey_Commented:
use this code


ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buff = new byte[4096];
int len;
while ((len = is.read(buff)) != -1)
{
  baos.write(buff, 0, len);          
}
byte[] data = baos.toByteArray();

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wachiCommented:
Try this code. It work with IE but not on Netscape.

java.awt.Image
loadImage (String name)
{
    try {
       java.net.URL url = getClass().getResource(name);
       if (getParent() instanceof Applet)
      {
        return ((Applet)getParent()).getImage(url);
      }
      else
      return createImage((java.awt.image.ImageProducer) url.getContent());
            } catch (Exception ex) {
            return null;
      }
      }
0
agneskoAuthor Commented:
Thanks, heyhey.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.