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.

Revision as of 15:09, 22 August 2009 by im2amit (Talk | contribs)

Design Pattern for Java ME Canvas based applications

From Wiki
Jump to: navigation, search


Contents

Problem

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

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 objects which can be passed over the BaseScreenCanvas 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

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 be 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 other screens or canvas classes for diffenent modules of the application will be as under(e.g. SplashScreen)

import javax.microedition.lcdui.*;
public class SplashScreen implements BaseScreen
{
BaseScreenCanvas bsc;
public SplashScreen(BaseScreenCanvas bsc)
{
this.bsc=bsc;
// 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();
}
}

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

--Submitted by im2amit 15:20, 21 Aug 2009 (IST)

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

×