File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Applets and the fly likes Image in Applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "Image in Applet" Watch "Image in Applet" New topic
Author

Image in Applet

Asher Tarnopolski
Ranch Hand

Joined: Jul 28, 2001
Posts: 260
i have an applet which has to reload an image every 10 seconds. the image always has the same name, but it changes every time it has to be loaded (actually it's a camer image capture).
i have a problem redrawing the image. it seems to me the image downloaded once and then the applet doesn't download it anymore.
i tried to solve it through the Toolkit methods but it doesn't work at all. any ideas?
here is the code:
import java.awt.*;
import java.applet.*;
import java.util.*;
import java.text.*;
public class ImgApp extends Applet implements Runnable
{
boolean error=false;
Thread t;
Image img;
public void init()
{
this.setBackground(Color.white);
}
public void paint(Graphics g)
{
Dimension d=getSize();
if(error)
{
g.setColor(Color.red);
g.fillRec(0,0,d.width,d.height);
g.setColor(Color.black);
g.drawString("Image Not Found",10,d.height/2);
}
else
{
g.drawImage(img,0,0,this);
Date date=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("Europe/Athens"));
g.setColor(Color.red);
g.drawString(sdf.format(date)+" in Athens ",150,310);
}
}
public void update(Graphics g)
{
paint(g);
}
public boolean imageUpdate(Image img,int flags,int x,int y, int w, int h)
{
if((flags & SOMEBITS) != 0 )
repaint(x,y,w,h);
else if((flags & ABORT) != 0)
{
error=true;
repaint();
}
return (flags & (ALLBITS|ABORT)) == 0;
}
public void start()
{
t=new Thread(this);
t.start();
}
public void run()
{
for(;
{
img=this.getImage(this.getDocumentBase(),"cam.jpg");
this.repaint();
try{
Thread.sleep(10000);
}catch(InterruptedException ie){}
}
}
public void stop()
{
t=null;
}

};


Asher Tarnopolski
SCJP,SCWCD
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Where is this code from?
First, there's a syntax error in it, so it won't even compile!
I'm curious how you can test it if it won't even compile?
That being said, the main problem is that getImage() only returns a data structure that later drawing routines can use to actually draw the image. When you call
img=this.getImage(this.getDocumentBase(),"cam.jpg");
The method returns immediately; it doesn't actually load the image.
Later, in your paint(), you call drawImage...
g.drawImage(img,0,0,this);
at this point, the information in the Image is used to actually load the image from wherever it is.
Back to the run() method....
Since you are loading the exact same file name each time, the getImage() method is "smart" and just returns the same exact data structure it had before...which was the original image the very first time you loaded it. That's why you are not loading any new images. If you were changing the image name each time, then it would work , loading the new image from scratch.
The easiest way to fix this is just to include this code before your getImage() method:
if( img != null) { img.flush(); }
img = this.getImage(getDocumentBase(),"cam.gif");
By flushing the previous Image data, you force getImage() to create a new internal information buffer, so next time you call drawImage(), it will actually re-fetch the image.
Two small other points:
1) you don't actually want to override update() here, because you want your applet to erase the background before you call paint() again. Otherwise, you're going to be drawing on top of the existing image, and your clock text will be getting garbled.
2) you don't need to override imageUpdate(), since the Applet already implements this for you.
To simplify your applet I'd remove both update() and imageUpdate() methods.
Good luck.
[ March 09, 2002: Message edited by: Rob Ross ]

Rob
SCJP 1.4
Asher Tarnopolski
Ranch Hand

Joined: Jul 28, 2001
Posts: 260
hey,thank's for the help, i'll try it later.
in any case the code compiled and ran, but with wrong result for me. there is no syntax error in it (did u mean ; ) where the ranch system places smiley automatically?)
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
g.fillRec(0,0,d.width,d.height);

there's no such Graphics method!
Asher Tarnopolski
Ranch Hand

Joined: Jul 28, 2001
Posts: 260
ohh,it's ok, "t" is lost by pasting
anyway, your solution didn't help. the image is still not changed... the paint method works since the clocks is running ok..
SOS!
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
I would double-check that the cam.jpg is really getting updated. Make sure it's a different image each time. I tested this applet myself and it works:

[ March 10, 2002: Message edited by: Rob Ross ]
Asher Tarnopolski
Ranch Hand

Joined: Jul 28, 2001
Posts: 260
10x once more. the image is changing and there is no problem with it. but the applet still downloads the first image only. the clock is working ok.. can't get what's the problem...
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Maybe your web server is caching the image? So when it goes to serve the image to the applet, since it recognizes that it is the same image name as before, it just returns the old image?
Have you tried just loading the applet locally? create a simple hmtl page with the applet, and have the applet and hmtl file in the same folder, then open the html file directly with a browser. Then try changing the image in that directory and see if the applet picks up the different image each time.
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Just curious...did you ever get this issue resolved,and if so, what was the solution?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Image in Applet