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
bogdan.galiceanu (Talk | contribs)
m (Added "Java ME" category)
TK2000 (Talk | contribs)

Revision as of 02:20, 25 August 2009


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

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
{
public CanvasScreen cs=null;
public BaseCanvas(Midlet midlet)
{
this.setFullScreenMode(true);
cs=new SplashScreen(this);
}
public void paint(Graphics g)
{
cs.paint(g);
}
public void keyPressed(int keyCode)
{
cs.keyPressed(keyCode);
}
public void keyRepeated(int keyCode)
{
cs.keyRepeated(keyCode);
}
}

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 diffenent modules of the application will be as under(e.g. SplashScreen)

import javax.microedition.lcdui.*;
public class SplashScreen implements CanvasScreen
{
BaseCanvas bc;
public SplashScreen(BaseCanvas bc)
{
this.bc=bc;
// 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.

--Submitted by Amitabh Srivastava at 15:20(IST), 22 August 2009.

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

×