Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

(Difference between revisions)

Design Pattern for Java ME Canvas based applications

From Wiki
Jump to: navigation, search
jimgilmour1 (Talk | contribs)
m
hamishwillee (Talk | contribs)
m (Hamishwillee - Fix categories)
 
(30 intermediate revisions by 8 users not shown)
Line 1: Line 1:
[[Category:Mobile_Design]]  
+
[[Category:LCDUI]][[Category:Mobile Design Patterns]]
 +
{{ArticleMetaData <!-- v1.3 -->
 +
|sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] -->
 +
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 +
|devices= <!-- Devices tested against - e.g. ''devices=Nokia 6131 NFC, Nokia C7-00'') -->
 +
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
 +
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 +
|signing= <!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
 +
|capabilities= <!-- Required capabilities for code (e.g. ID_CAP_LOCATION, ID_CAP_NETWORKING) -->
 +
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 +
|translated-by= <!-- [[User:XXXX]] -->
 +
|translated-from-title= <!-- Title only -->
 +
|translated-from-id= <!-- Id of translated revision -->
 +
|review-by= <!-- After re-review: [[User:username]] -->
 +
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 +
|update-by= <!-- After significant update: [[User:username]]-->
 +
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 +
|creationdate= 20090822
 +
|author= [[User:Im2amit]]
 +
}}
  
== Design Pattern for J2me Canvas based applications ==
+
== Problem ==
  
=== Problem ===
+
Everyone working in Java ME must have faced this problem of “flicker” while switching displayable objects i.e. setting a new Canvas as current from one Canvas already in View, so it is advised that displayable Canvas should not be set again and again. We should use only One Canvas set as current throughout the application, this results in a single long unmanageable Canvas class with long Key press/release, paint methods and lot of Switch case / if then else etc which effects the performance and readability of the code.
  
Everyone working in J2me must have faced this problem of “flicker” while switching displayable objects i.e. setting a new Canvas as current from one Canvas already in View, so it is advised that displayable Canvas should not be set again and again. We should use only Canvas set as current throughout the application, this results in a single long unmanageable Canvas class with long Key press/release, paint methods and lot of Switch case / if then else  etc  which effects the performance and readability of the code.
+
== Solution ==
  
=== Solution ===
+
We have to design a solution in such a way that only one Canvas (''BaseCanvas'' – see the code snippet below) is set as current displayable only once by the [[MIDlet]] and using object oriented approach of creating different Canvas Screens for different modules of the application extending  (''CanvasScreen'' – see code snippet below) a common interface class objects which can be passed over the ''BaseCanvas'' without any flicker. Code for handing Paint and events of each of the screen is separated in such different screen classes and hence become more readable and efficient.
  
We have to design a solution in such a way that only one Canvas (BaseScreenCanvas – see codelet below) is set as current displayable only once by the midlet and using object oriented approach of creating different Canvas Screens  for different modules of the application extending  (BaseScreen – see codelet below) a common interface class those can be passed over the BaseScreenCanvas without any flicker and code for handing Painting and event handling of each of the screen is separated in different screen classes and hence become more readable and efficient.
+
== Advantages ==
 +
* Easy to extend
 +
* More efficient code
 +
* More readable code
 +
* Lower memory footprint
  
'''BaseScreenCanvas will be as under'''
+
== Source Code ==
 +
=== ''BaseCanvas'' will be as under===
 +
<code java>
 
import javax.microedition.lcdui.*;
 
import javax.microedition.lcdui.*;
public class BaseScreenCanvas extends Canvas  
+
public class BaseCanvas extends Canvas  
{
+
{      
public BaseScreen bs=null;
+
        /**Singleton Instance**/
public BaseScreenCanvas(Midlet midlet)  
+
        private static BaseCanvas mCanvas;
{
+
 
this.setFullScreenMode(true);
+
        /** Singleton Accessor Method with lazy initialization**/
bs=new SplashScreen(this);
+
        public static Canvas getCanvas()
}
+
        {if(mCanvas==null)
 +
              mCanvas=new BaseCanvas();
 +
        return mCanvas;
 +
        }
 +
   
 +
        public CanvasScreen setCurrentScreen(CanvasScreen aCs)
 +
        {
 +
                CanvasScreen old=mCs;
 +
                mCs=aCs;
 +
                return old;
 +
        }
 +
       
 
public void paint(Graphics g)
 
public void paint(Graphics g)
{
+
{
bs.paint(g);
+
if(null!=mCs) mCs.paint(g);
 
}
 
}
 +
 
public void keyPressed(int keyCode)
 
public void keyPressed(int keyCode)
 
{
 
{
bs.keyPressed(keyCode);
+
if(null!=mCs) mCs.keyPressed(keyCode);
 
}
 
}
 +
 
public void keyRepeated(int keyCode)
 
public void keyRepeated(int keyCode)
 
{
 
{
bs.keyRepeated(keyCode);
+
if(null!=mCs) mCs.keyRepeated(keyCode);
 
}
 
}
}
 
  
 +
        /**Stratergy Object **/
 +
private CanvasScreen mCs=null;
 +
   
 +
private BaseCanvas()
 +
{
 +
setFullScreenMode(true);
 +
}
 +
}
 +
</code>
  
'''BaseScreen Interface will look as under'''
+
=== ''CanvasScreen''  Interface will be as under===
 +
<code java>
 
import javax.microedition.lcdui.*;
 
import javax.microedition.lcdui.*;
public interface BaseScreen
+
public interface CanvasScreen 
 
{
 
{
 
public void paint(Graphics g);
 
public void paint(Graphics g);
Line 44: Line 91:
 
public void keyRepeated(int keyCode);
 
public void keyRepeated(int keyCode);
 
}
 
}
 +
</code>
  
 
+
===All other screens or canvas classes for different modules of the application will be as under<br>(e.g. SplashScreen)===
'''All Your other Screens or Canvas classes needed for different modules of the application would like –
+
<code java>
For example:  SplashScreen class will look like as under .and will handle the flow within themselves and all will all implement BaseScreen interface.'''
+
 
import javax.microedition.lcdui.*;
 
import javax.microedition.lcdui.*;
public class SplashScreen implements BaseScreen
+
public class SplashScreen implements CanvasScreen
 
{
 
{
BaseScreenCanvas bsc;
+
BaseCanvas bc;
public SplashScreen(BaseScreenCanvas bsc)  
+
public SplashScreen()  
 
{
 
{
this.bsc=bsc;
+
this.bc=BaseCanvas.getCanvas();
WIDTH=Device.WIDTH;
+
                bc.setCurrentScreen(this);
HEIGHT=Device.HEIGHT;
+
                // do what you want to paint on splash screen and load your assests.
              // do what you want to paint on splash screen and load your assets.
+
 
}
 
}
 
 
Line 71: Line 117:
 
public void keyPressed(int keyCode)
 
public void keyPressed(int keyCode)
 
{
 
{
            // do what you need
+
                // do what you need
 
}
 
}
  
 
private void repaint()
 
private void repaint()
 
{
 
{
bsc.repaint();
+
bc.repaint();
 
}
 
}
 
}
 
}
 +
</code>
 +
 +
All other screen classes will implement ''CanvasScreen'' interface, do their own painting and event handling within themselves.
 +
 +
== Threading Safety ==
 +
 +
Access to BaseCanvas class above is not thread safe. Simultaneous access from multiple threads can give unpredictable results. So if an application access this class from multiple threads then access to it should be synchronized.

Latest revision as of 01:27, 16 August 2013

Article Metadata
Article
Created: im2amit (22 Aug 2009)
Last edited: hamishwillee (16 Aug 2013)

Contents

[edit] Problem

Everyone working in Java ME must have faced this problem of “flicker” while switching displayable objects i.e. setting a new Canvas as current from one Canvas already in View, so it is advised that displayable Canvas should not be set again and again. We should use only One Canvas set as current throughout the application, this results in a single long unmanageable Canvas class with long Key press/release, paint methods and lot of Switch case / if then else etc which effects the performance and readability of the code.

[edit] Solution

We have to design a solution in such a way that only one Canvas (BaseCanvas – see the code snippet below) is set as current displayable only once by the MIDlet and using object oriented approach of creating different Canvas Screens for different modules of the application extending (CanvasScreen – see code snippet below) a common interface class objects which can be passed over the BaseCanvas without any flicker. Code for handing Paint and events of each of the screen is separated in such different screen classes and hence become more readable and efficient.

[edit] Advantages

  • Easy to extend
  • More efficient code
  • More readable code
  • Lower memory footprint

[edit] Source Code

[edit] BaseCanvas will be as under

import javax.microedition.lcdui.*;
public class BaseCanvas extends Canvas
{
/**Singleton Instance**/
private static BaseCanvas mCanvas;
 
/** Singleton Accessor Method with lazy initialization**/
public static Canvas getCanvas()
{if(mCanvas==null)
mCanvas=new BaseCanvas();
return mCanvas;
}
 
public CanvasScreen setCurrentScreen(CanvasScreen aCs)
{
CanvasScreen old=mCs;
mCs=aCs;
return old;
}
 
public void paint(Graphics g)
{
if(null!=mCs) mCs.paint(g);
}
 
public void keyPressed(int keyCode)
{
if(null!=mCs) mCs.keyPressed(keyCode);
}
 
public void keyRepeated(int keyCode)
{
if(null!=mCs) mCs.keyRepeated(keyCode);
}
 
/**Stratergy Object **/
private CanvasScreen mCs=null;
 
private BaseCanvas()
{
setFullScreenMode(true);
}
}

[edit] CanvasScreen Interface will be as under

import javax.microedition.lcdui.*;
public interface CanvasScreen
{
public void paint(Graphics g);
public void keyPressed(int keyCode);
public void keyRepeated(int keyCode);
}

[edit] All other screens or canvas classes for different modules of the application will be as under
(e.g. SplashScreen)

import javax.microedition.lcdui.*;
public class SplashScreen implements CanvasScreen
{
BaseCanvas bc;
public SplashScreen()
{
this.bc=BaseCanvas.getCanvas();
bc.setCurrentScreen(this);
// do what you want to paint on splash screen and load your assests.
}
 
public void keyRepeated(int keyCode){ }
 
public void paint(Graphics g)
{
// paint your Splash Screen
// paint Loader(g);
}
 
 
public void keyPressed(int keyCode)
{
// do what you need
}
 
private void repaint()
{
bc.repaint();
}
}

All other screen classes will implement CanvasScreen interface, do their own painting and event handling within themselves.

[edit] Threading Safety

Access to BaseCanvas class above is not thread safe. Simultaneous access from multiple threads can give unpredictable results. So if an application access this class from multiple threads then access to it should be synchronized.

This page was last modified on 16 August 2013, at 01:27.
204 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×