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
kiran10182 (Talk | contribs)
m (Kiran10182 -)
hamishwillee (Talk | contribs)
m (Hamishwillee - Bot update - Fix ReviewerApproval and ArticleMetaData)
Line 1: Line 1:
[[Category:Mobile_Design_Patterns]][[Category:Mobile Design]][[Category:Java ME]]
+
[[Category:Mobile Design Patterns]][[Category:Mobile Design]][[Category:Java ME]]
{{ArticleMetaData
+
{{ArticleMetaData <!-- v1.1 -->
 
|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]]) -->
Line 7: Line 7:
 
|platform= <!-- Compatible platforms - e.g. Symbian^1 and later, Qt 4.6 and later -->
 
|platform= <!-- Compatible platforms - e.g. Symbian^1 and later, Qt 4.6 and later -->
 
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
 
|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= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 
|keywords= <!-- APIs, classes and methods (e.g. QSystemScreenSaver, QList, CBase -->
 
|keywords= <!-- APIs, classes and methods (e.g. QSystemScreenSaver, QList, CBase -->
 
|id= <!-- Article Id (Knowledge base articles only) -->
 
|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 -->
|review-by=<!-- After re-review: [[User:username]] -->
+
|translated-by= <!-- [[User:XXXX]] -->
|review-timestamp=<!-- After re-review: YYYYMMDD -->
+
|translated-from-title= <!-- Title only -->
|update-by=<!-- After significant update: [[User:username]]-->
+
|translated-from-id= <!-- Id of translated revision -->
|update-timestamp=<!-- After significant update: YYYYMMDD -->
+
|review-by= <!-- After re-review: [[User:username]] -->
|creationdate=20090822
+
|review-timestamp= <!-- After re-review: YYYYMMDD -->
|author=[[User:Im2amit]]
+
|update-by= <!-- After significant update: [[User:username]]-->
}}{{ReviewerApproved}}
+
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 +
|creationdate= 20090822
 +
|author= [[User:Im2amit]]
 +
}}
  
  
Line 28: Line 31:
 
== 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 ==

Revision as of 00:25, 14 February 2012

Article Metadata
Article
Created: im2amit (22 Aug 2009)
Last edited: hamishwillee (14 Feb 2012)


Contents

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.

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.

Advantages

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

Source Code

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);
}
}

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);
}

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.

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.

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

×