The thing isn't so simple as far as i am concerned becouse we have to create a thread for working in background and an canvas class with Timer thread to repaint it. In my software i solved this case combinining those 3 into 1 class (my project required such design). I list my code below
Code:
public class ConnectionWorker extends FullCanvas implements Runnable {
private midApp parent;
private String URL_to_call;
private Object obj_to_notify;
private String response_content;
private boolean containsErrors = false;
private boolean hasReceivedResponse = false;
private String theText = "AAAA";
private Font sF = Font.getFont (Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_MEDIUM);
private Font mF = Font.getFont (Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_LARGE);
int quant = 0;
private Timer scheduler;
private ConnectionWorker.TimerRepainter repainter;
public ConnectionWorker (midApp parent_, String url, Object monitor){
parent = parent_;
URL_to_call = url;
obj_to_notify = monitor;
this.repaint ();
this.serviceRepaints ();
scheduler = new Timer();
repainter = new ConnectionWorker.TimerRepainter(this);
scheduler.schedule(repainter,0,500);
parent.displaj.setCurrent(this);
}
public boolean getHasReceivedResponse(){
return hasReceivedResponse;
}
public void paint (Graphics g){
g.setClip (0,0,getWidth (),getHeight ());
g.drawImage (parent.chatBkg,0,0,20);
g.setColor (0xFFFFFF);
g.setFont (mF);
g.drawString ("LOADING",getWidth ()/2 - mF.stringWidth ("LOADING")/2,10,20);
g.drawString (" Connction to server",getWidth ()/2 - mF.stringWidth (" Connction to server")/2,25,20);
g.setColor (0x000000);
g.drawString ("LOADING",getWidth ()/2 - mF.stringWidth ("LOADING")/2 +1,11,20);
g.drawString (" Connection to server",getWidth ()/2 - mF.stringWidth (" Connection to server")/2 +1,26,20);
g.setColor (0x000000);
g.setFont (sF);
g.drawString (theText,getWidth ()/2 - sF.stringWidth (theText)/2,getHeight ()- 10 - sF.getHeight (),20);
g.setClip (getWidth ()/2-38/2,getHeight ()/2-50/2,38,50);
g.drawImage (parent.progressImg,getWidth ()/2-38/2,getHeight ()/2- 50/2 - 50*quant,20);
}
public void run (){
try{
//connection
/// here is my method for connecting to server
response_content = RawConnector.callServlet (URL_to_call);
hasReceivedResponse = true;
}catch (Exception e){
e.printStackTrace ();
}
//notify
try{
Thread.sleep (10000);
}catch(Exception e){
}
}
public String get_response_content (){
if (hasReceivedResponse)
return response_content;
else
return null;
}
private class TimerRepainter extends TimerTask {
private ConnectionWorker connWorker;
public TimerRepainter(ConnectionWorker conWrk){
connWorker = conWrk;
}
public void run(){
connWorker.quant = (connWorker.quant+1) % 3;
connWorker.repaint();
connWorker.serviceRepaints ();
}
}
}
hope this would help in the future