×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Regular Contributor
    Join Date
    Aug 2003
    Posts
    127

    question - a animated canvas during HTTP connection

    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

  2. #2
    Registered User
    Join Date
    Apr 2005
    Posts
    8

    Runnable Canvas

    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.

  3. #3
    Regular Contributor
    Join Date
    Aug 2003
    Posts
    127

    Not exacly - animation canvas

    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 11:31.

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    8
    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
      }
    }

Posting Permissions

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