| Reply | « Previous Thread | Next Thread » |
|
I ve designed a midlet which connects to server and creates an image on the screen from received data. My question is:
is it possible to write a code which shows user Canvas which displays received data count and animated image (3 frames) on the screen during the connection is made. Correct me if i am wrong bu i think thath i will need: 1. Class extending FullCanvas and implementing Runnable - it will show the animation on the screen 2. A HttpConnectionWorker 0 class (thread) which connects to server and waits for response ?? am i right has anybody got an exacmple of such code?? Mchmiel |
|
Yes, indeed, to show an animation like that, you could use a Runnable Canvas. Basic code:
Code:
public void run() {
onScreen = true;
for (;onScreen;) {
// Draw image with index index
index++;
if (index >= MAX_INDEX) index = 0;
try {
Thread.sleep(40);
} catch (Exception e) {
// Never thrown
}
}
}
|
|
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 ();
}
}
}
Last edited by melas21 : 2005-04-23 at 12:31.
|
|
I prefer using basic Threads instead of Timers, but that is just my thing. When using repaint() statements, it is wise to use a boolean to make the paint methods only accesible once at a time. This prevents flooding and poor running. So you basically skip the next paint if the paint method it already running.
Code:
public void paint(Graphics g) {
if (paintRunning) return;
paintRunning = true;
// Do your painting
paintRunning = false;
}
Code:
public void run() {
if (Thread.currentThread() == firstThread) {
// First Thread tasks
} else if (Thread.currentThread() == secondThread) {
// Second Thread tasks
}
}
|
| Reply | « Previous Thread | Next Thread » |
| Thread Tools | Search this Thread |
|---|---|