[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 220
  • Last Modified:

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);
}
0
agnesko
Asked:
agnesko
1 Solution
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
 
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now