User feedback during applet loading

I've created an applet that takes a fair while to load in a browser. Does anyone know an easy way of writing to the screen while the applet loads so the user can be kept informed of the progress of the applet?
[224 byte] By [stephengaskell] at [2007-9-26 1:16:51]
# 1

Hi,

Here is a sample code which shows the progress as the image gets loaded.

import java.util.*;

import java.applet.*;

import java.awt.*;

public class TrackedImageLoad extends Applet implements Runnable

{

MediaTracker tracker;

int tracked;

int frame_rate;

int current_img = 0;

Thread monitor;

static final int MAXIMAGES = 10;

Image img[] = new Image[MAXIMAGES];

String name[] = new String[MAXIMAGES];

boolean stopFlag;

public void init()

{

tracker = new MediaTracker(this);

String Tokenizer st = new StringTokenizer(getParameter("img"),"+");

while(st.hasMoreTokens() && tracked <= MAXIMAGES)

{

name[tracked[ = st.nextToken();

img[tracked] = getImage(getDocumentBase(), name[tracked] + ".jpg");

tracker.addImage(img[tracked],tracked);

tracked++;

}

}

public void paint(Graphics g)

{

String loaded = " ";

int donecount = 0;

for(int i = 0; i < tracked; i++)

{

if(tracker.checkID(i,true))

{

donecount++;

loaded += name + " ";

}

}

Dimension d = getSize();

int w = d.width();

int h = d.height();

if(donecount == tracked)

{

frame_rate = 1;

Image i = img[current_img++];

int iw = i.getWidth(null);

int ih = i.getHeight(null);

g.drawImage(i, (w - iw) / 2, (h -ih)/2, null);

if(current_img >= tracked)

current_img = 0;

}

else

{

int x = w * donecount / tracked;

g.setColor(Color.black);

g.fillRect(0,h/3,x,16);

g.setColor(Color.white);

g.fillRect(x,h/3,w-x,16);

g.setColor(Color.black);

g.drawString(loaded, 10, h/2);

}

public void start()

{

motor = new Thread(this);

stopFlag = false;

motor.start();

}

public void stop()

{

stopFlag = true;

}

public void run()

{

motor.setPriority(Thread.MIN_PRIORITY);

while(true)

{

repaint();

try

{

Thread.sleep(1000/frame_rate);

}

catch(InterruptedException e)

{

};

if(stopFlag)

return;

}

}

}

I hope this will help you.

Thanks

Bakrudeen

Technical Support Engineer

Sun MicroSystems Inc, India

bakrudeen_indts at 2007-6-29 0:45:15 > top of Java-index,Archived Forums,Java Programming...