You Are Here:

Community: Developer Discussion Boards

#1 Old question - a animated canvas during HTTP connection - 2005-04-16, 19:49

Join Date: Aug 2003
Posts: 127
melas21
Offline
Regular Contributor
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
Reply With Quote

#2 Old Runnable Canvas - 2005-04-23, 11:44

Join Date: Apr 2005
Posts: 8
jvohome
Offline
Registered User
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
    }
  }
}
Just make a decent check to see whether the canvas is still visible and active.
Reply With Quote

#3 Old Not exacly - animation canvas - 2005-04-23, 12:24

Join Date: Aug 2003
Posts: 127
melas21
Offline
Regular Contributor
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
Last edited by melas21 : 2005-04-23 at 12:31.
Reply With Quote

#4 Old 2005-04-23, 13:42

Join Date: Apr 2005
Posts: 8
jvohome
Offline
Registered User
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;
}
You can ofcourse use multiple Threads running through the same class. So your run would look like this:
Code:
public void run() {
  if (Thread.currentThread() == firstThread) {
    // First Thread tasks
  } else if (Thread.currentThread() == secondThread) {
    // Second Thread tasks
  }
}
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Forum Jump

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fdiscussionE2eforumE2enokiaE2ecomE2fforumE2fshowthreadE2ephpE3ftE3d134434X qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE44iscussionQ qdcZtypeQUqfntypeZE44iscussionContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtopicQUqfnTopicZentertainmentQ qfnZtopicQUqfnTopicZj2meQ qfnZtopicQUqfnTopicZjavaQ qfnZtopicQUqfnTopicZmediaQ qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE44iscussionQ qfnZtypeQUqfntypeZE44iscussionContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE44iscussionQ qrdfZtypeQUqfntypeZE44iscussionContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ