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
hamishwillee (Talk | contribs)
m (Hamishwillee - Bot update - Fix ReviewerApproval and ArticleMetaData)
hamishwillee (Talk | contribs)
m (Hamishwillee - Fix categories)
 
(3 intermediate revisions by one user not shown)
Line 1: Line 1:
[[Category:Mobile Design Patterns]][[Category:Mobile Design]][[Category:Java ME]]
+
[[Category:LCDUI]][[Category:Mobile Design Patterns]]
{{ArticleMetaData <!-- v1.1 -->
+
{{ArticleMetaData <!-- v1.3 -->
 
|sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] -->
 
|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]]) -->
 
|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'') -->
 
|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]) -->
 
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
|platform= <!-- Compatible platforms - e.g. Symbian^1 and later, Qt 4.6 and later -->
 
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
 
 
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 
|signing= <!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
 
|signing= <!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
+
|capabilities= <!-- Required capabilities for code (e.g. ID_CAP_LOCATION, ID_CAP_NETWORKING) -->
|keywords= <!-- APIs, classes and methods (e.g. QSystemScreenSaver, QList, CBase -->
+
|id= <!-- Article Id (Knowledge base articles only) -->
+
 
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 
|translated-by= <!-- [[User:XXXX]] -->
 
|translated-by= <!-- [[User:XXXX]] -->
Line 23: Line 19:
 
|author= [[User:Im2amit]]
 
|author= [[User:Im2amit]]
 
}}
 
}}
 
  
 
== 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 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.
  
 
== 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 (''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.
  
 
== Advantages ==
 
== Advantages ==

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.
200 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.

×