Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

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

How to load many images in applet?

Hi

I am implementing an applet that can play a large number jpg images.

If i use
//***********************************************
Image[] image = new Image[5000];
for (int i = 0; i < 5000; i++) {
anim = getImage(getDocumentBase(),
"images/anim"+i+".gif");
tracker.addImage(anim, 1);
}
//***********************************************
, it means applet must download all of 5000 images before playing.

That cause so long delay and pc heavy work.

Is there any way to solve the problem.

I hope my applet start playing immediately. Then during playing thread running, more 20 images would be downloaded. then old pictures should be cleared from cache.

The following are some structure code:(of course it is not work with a large number image)

public class ViewCamApplet1 extends JApplet implements Runnable{

public void init() {
Image imgInd[] = new Image [6000];
buildUI(getContentPane());
setSize(480,300);
show();
}

public void start(){
if (thread == null)
{
thread = new Thread(this);
running = true;
thread.start();
}
}

public void run() {
int delay = 500; // 1 second
while (running) {
frame++;
System.out.println(frame);
imagePanel.repaint();
try {
Thread.sleep(delay);
} catch (Exception e) {}

}//end of while
}//end of Run

public void buildUI(Container container){
imagePanel = new ImagePanel(getImage());
container.add(imagePanel, BorderLayout.CENTER);

ActionListener startListener = new ActionListener(){
public void actionPerformed(ActionEvent e){
start();
pre.setEnabled(false);
bac.setEnabled(false);
running = true;
}
};


}//end of buildUI
public Image[] getImage()
{
URL url = null;
URLConnection con = null;
for (int i = 0; i < 3600; i++) {
imgInd = getImage(getDocumentBase(),"images4/"+i+".jpg");
tracker.addImage( imgInd, 1);
}//i is index

return imgInd;
}//end of getImage()

class ImagePanel extends JPanel{
Image[] image;
public ImagePanel(Image[] image){
this.image = image;
}
public void paintComponent(Graphics g){
try{
g.drawImage(image[frame], 0, 0,340,320, this);
}catch(ArrayIndexOutOfBoundsException e){
System.out.println(e.getMessage());
return;
}
}
}//end of inner class
}// end of applet
0
yangbin988
Asked:
yangbin988
1 Solution
 
girionisCommented:
 Well you are loading 5000 images from the server, it is supposed to take some time :-).

  It might help if you put all images in the applet's jar file.
0
 
sct75Commented:
As you might know, putting all 5000 images in the jar together with the Applet will incur long downloading time of the applet. If user does not care, this solution is enough.

Alternatively, you could consider instantiate multiple threads within the applet, each of which asynchronizely loads in certain number of images at the background. This approach is assuming that you could proceed with first certain number of images avaiable but not necessarily all of the 5000. Also, you may consider to use MediaTracker to facilitate such process.
0
 
girionisCommented:
 Both solutions require time, since 5000 images is a lot of bytes to load.

>> This approach is assuming that you could proceed with first certain number of images avaiable but not necessarily all of the 5000.

  vs

>> it means applet must download all of 5000 images before playing.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
girionisCommented:
 Sorry, just ignore my previous comment. I thought he meant he has to download all images before start palying. I think the threads suggestions is the most viable here.
0
 
yangbin988Author Commented:
Hi

Thanks for reply.

The imageSlider need not download all of images before playing. And actrually i feel it is also impossible.
I cannot put all of images in jar file cause those images is dynamically changed.

I agree to sct75's opnion. Using two thread, one thread palying another one download.

Sct75, do you have some example code about thread commmuniction? I didnot found easy understanded example from internet.


Thanks

0
 
sct75Commented:
Generally you need to have two thread classes. One is in charge of downloading. The other is in charge of painting the images.

You applet class is the manager to start them and monitor their individual status. Obviously, you could start no matter how many of the download threads as you want, but bottom of line is that the display thread cannot start until certain number of images are available in the system. For simplicity, just you could just start one at beginning.

The applet contains a central repository to host downloaded images. This repository is visible to both the download thread and display thread. You could define a stand-alone class just to manage this image repository which will handle add images to the end of queues (called from the download thread) and retrieve images from the front of the queue (called from the display thread). If no image is available, it will notify the display thread to pause. In java, this could be done to let display thread sleep certain number of milli-second.

For example, a DownLoadThread may look like following

public class DownLoadThread extends Thread
{
private MyApplet parent;
private URL downloadURL;
public DownLoadThread(MyApplet p, URL downloadURL)
{
this.parent = p;
this.downloadURL = downloadURL;
}

...
public void run()
{
for(int i=0; i<num_frames; i++) {
parent.showStatus("Loading Frame: "+i);
//block until the specified image is loaded
try{
  parent.getTracker().waitForID(i);
}
catch(InterruptedException e) {
 e.printStackTrace();
}
//check for errors loading it.
if (parent.tracker.isErrorID(i)) {
parent.showStatus("Error loading frame "+i+";.Quiting...");
return;
}
}
parent.showStatus("Loading is done.");
}

...

}

Please note, for similicity, I directly write parent.getTracker() and parent.showStatus(). The parent.getTracker() will return the MediaTracker instance defined in your applet class to keep track of the media. Please look at its javadoc for further detail of usage.

While your DisplayThread will do something like following
public DisplayThread extends Thread
{
...
private MyApplet parent;
public DisplayThread (MyApplet p,)
{
this.parent = p;
}

public void paint(Graphics g)
{
//erase the last image shown
//show your image here    
}
//Don't clear screen before calling paint() so as to avoid uncessary flushing.
public void update(Graphics g)
{
paint(g);
}

public void run()
{
while (true)
{
//your display logic, such as move around or shift images, etc.
}
update(parent.getGraphics());    
}
...
}
0
 
sct75Commented:
While, showStatus() method is defined by Java's Applet class, which is the superclass of your Applet class.
0
 
sct75Commented:
Does the thread solution work?
0
 
CleanupPingCommented:
yangbin988:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
girionisCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Points to sct75

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

girionis
EE Cleanup Volunteer
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

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