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. Thanks for all your past and future contributions.

Revision as of 10:29, 22 August 2009 by im2amit (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Design Pattern for Java ME Canvas based applications

From Wiki
Jump to: navigation, search

Design Pattern for J2me Canvas based applications


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.


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.

BaseScreenCanvas will be as under import javax.microedition.lcdui.*; public class BaseScreenCanvas extends Canvas { public BaseScreen bs=null; public BaseScreenCanvas(Midlet midlet) { this.setFullScreenMode(true); bs=new SplashScreen(this); } public void paint(Graphics g) { bs.paint(g); } public void keyPressed(int keyCode) { bs.keyPressed(keyCode); } public void keyRepeated(int keyCode) { bs.keyRepeated(keyCode); } }

BaseScreen Interface will look as under import javax.microedition.lcdui.*; public interface BaseScreen { public void paint(Graphics g); public void keyPressed(int keyCode); public void keyRepeated(int keyCode); }

All Your other Screens or Canvas classes needed for diffenent modules of the application would like – 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.*; public class SplashScreen implements BaseScreen { BaseScreenCanvas bsc; public SplashScreen(BaseScreenCanvas bsc) { this.bsc=bsc; WIDTH=Device.WIDTH; HEIGHT=Device.HEIGHT;

              // 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() { bsc.repaint(); } }

114 page views in the last 30 days.