×
Namespaces

Variants
Actions
Revision as of 10:45, 22 August 2009 by jimgilmour1 (Talk | contribs)

Design Pattern for Java ME Canvas based applications

From Nokia Developer Wiki
Jump to: navigation, search


Design Pattern for J2me Canvas based applications

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 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 different 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 assets.
}
 
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();
}
}
274 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.

×