?
Solved

How to load many images in applet?

Posted on 2003-03-12
11
Medium Priority
?
336 Views
Last Modified: 2013-11-23
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
Comment
Question by:yangbin988
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 8126926
 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
 
LVL 1

Expert Comment

by:sct75
ID: 8128966
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
 
LVL 35

Expert Comment

by:girionis
ID: 8129010
 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:girionis
ID: 8129045
 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
 

Author Comment

by:yangbin988
ID: 8132534
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
 
LVL 1

Expert Comment

by:sct75
ID: 8132731
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
 
LVL 1

Expert Comment

by:sct75
ID: 8132740
While, showStatus() method is defined by Java's Applet class, which is the superclass of your Applet class.
0
 
LVL 1

Accepted Solution

by:
sct75 earned 200 total points
ID: 8160519
Does the thread solution work?
0
 

Expert Comment

by:CleanupPing
ID: 9058723
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
 
LVL 35

Expert Comment

by:girionis
ID: 9873362
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month13 days, 17 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question