×
Namespaces

Variants
Actions
(Difference between revisions)

Design Pattern for Java ME Canvas based applications

From Nokia Developer Wiki
Jump to: navigation, search
vkmunjpara (Talk | contribs)
hamishwillee (Talk | contribs)
m (Hamishwillee - Fix categories)
 
(9 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{ReviewerApproved}}
+
[[Category:LCDUI]][[Category:Mobile Design Patterns]]
[[Category:Mobile_Design_Patterns]][[Category:Mobile Design]][[Category:Java ME]]
+
{{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]]
 +
}}
  
 
== 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 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  (CanvasScreen – see codelet 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 ==
Line 17: Line 35:
  
 
== Source Code ==
 
== Source Code ==
===BaseCanvas will be as under===
+
=== ''BaseCanvas'' will be as under===
 
<code java>
 
<code java>
 
import javax.microedition.lcdui.*;
 
import javax.microedition.lcdui.*;
Line 28: Line 46:
 
         public static Canvas getCanvas()
 
         public static Canvas getCanvas()
 
         {if(mCanvas==null)
 
         {if(mCanvas==null)
               mCanvas=new BaseCanvac();
+
               mCanvas=new BaseCanvas();
 
         return mCanvas;
 
         return mCanvas;
 
         }
 
         }
Line 64: Line 82:
 
</code>
 
</code>
  
===CanvasScreen  Interface will be as under===
+
=== ''CanvasScreen'' Interface will be as under===
 
<code java>
 
<code java>
 
import javax.microedition.lcdui.*;
 
import javax.microedition.lcdui.*;
Line 109: Line 127:
 
</code>
 
</code>
  
All other screen classes will implement CanvasScreen interface, do their own painting and event handling within themselves.
+
All other screen classes will implement ''CanvasScreen'' interface, do their own painting and event handling within themselves.
  
 
== Threading Safety ==  
 
== 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.
+
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.
 
+
--Submitted by [http://discussion.forum.nokia.com/forum/member.php?u=353910 Amitabh Srivastava] at 15:20(IST), 22 August 2009.
+

Latest revision as of 04: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 04:27.
300 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.

×