Application of double buffering technology solutions in J2ME screen flicker issues
Abstract: J2ME double buffering technology to solve the problem of screen flicker
Sometimes we may be encountered in the J2ME flashing screen, which can be applied when we double buffering technology to solve, but a growing number of mobile phones is now on its support of the double buffering. So here only to be a brief introduction.
I originally wanted to write a procedure in the NOkia 6108, and compared with no difference between the results of this support on their cell phones double buffering, there is simply no comparison out. But to understand this technology and it is necessary, therefore, I decided it written. Double buffering is originally in the development of PC applications when encountered. MIDP development in the same presence, when you write on the screen to the original painting, if very complex, the customer interface will be found in the flicker. So you can do, first of all, in another picture on the paint () in the operation, when completed later copy to put it on the screen, as is usually copy a fast time would not have been a flicker. The technology is double buffering.
Canvas provides a isDoubleBuffered () method to judge the equipment did not support this function, if returned to the true, then we would have no need to use double buffering, and if false, then we can do so:
Public DoubleCanvas (UIController uicontroller)
(
Super ();
This.uicontroller = uicontroller;
Width = this.getWidth ();
Height = this.getHeight ();
This.setCommandListener (this);
If (! IsDoubleBuffered ())
(
OffImage = Image.createImage (width, height);
)
/ / TODO Auto-generated constructor stub
)
/ * (Non-Javadoc)
* @ See javax.microedition.lcdui.Displayable # paint (javax.microedition.lcdui.Graphics)
* /
Protected void paint (Graphics arg0)
(
/ / TODO Auto-generated method stub
Arg0.drawString (isDoubleBuffered ()+"", width / 2, height / 2, Graphics.HCENTER | Graphics.TOP);
Graphics saved = arg0;
If (offImage! = Null)
(
Arg0 = offImage.getGraphics ();
)
Arg0.setColor (255,128,128);
For (int i = 2, j = 2; i
Arg0.drawRect (i, j, i-2 * width, height-2 * j);
)
If (arg0! = Saved)
(
Saved.drawImage (offImage, 0,0, Graphics.LEFT | Graphics.TOP);
)
)
Graphics are examples of offImage conducted after the paint () in the operation. This part is usually more complicated, I am here not complicated:)
Then offImage directly to the copy on the screen is the implementation of saved.drawImage (offImage, 0,0, Graphics.LEFT | Graphics.TOP);
I wrote an application to compare the use and non-use double buffering effect, but because my phone support for double buffering effect therefore do not see, if you are interested, then, to paint () to amend the part of the code and then some of the complex migrate to the mobile phone does not support that may effect Below is my procedure code:
Import javax.microedition.midlet.MIDlet;
Import javax.microedition.midlet.MIDletStateChangeException;
/ *
* Created on 2004-7-3
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
* /
/ **
* @ Author P2800
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
* /
Public class DoubleBufferMIDlet extends MIDlet
(
Private UIController uicontroller;
/ * (Non-Javadoc)
* @ See javax.microedition.midlet.MIDlet # startApp ()
* /
Protected void startApp () throws MIDletStateChangeException
(
/ / TODO Auto-generated method stub
Uicontroller = new UIController (this);
Uicontroller.init ();
)
/ * (Non-Javadoc)
* @ See javax.microedition.midlet.MIDlet # pauseApp ()
* /
Protected void pauseApp ()
(
/ / TODO Auto-generated method stub
)
/ * (Non-Javadoc)
* @ See javax.microedition.midlet.MIDlet # destroyApp (boolean)
* /
Protected void destroyApp (boolean arg0) throws MIDletStateChangeException
(
/ / TODO Auto-generated method stub
)
)
Import javax.microedition.lcdui.Command;
Import javax.microedition.lcdui.CommandListener;
Import javax.microedition.lcdui.Displayable;
Import javax.microedition.lcdui.List;
/ *
* Created on 2004-7-3
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
* /
/ **
* @ Author P2800
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
* /
Public class MainListUI extends List implements CommandListener
(
/ / Private DoubleBufferMIDlet doubleMIDlet;
Private UIController uicontroller;
Public MainListUI (UIController uicontroller)
(
Super ( "Test", List.IMPLICIT);
This.uicontroller = uicontroller;
This.append ( "Non-buffer", null);
This.append ( "Double-buffer", null);
This.setCommandListener (this);
)
/ * (Non-Javadoc)
* @ See javax.microedition.lcdui.CommandListener # commandAction (javax.microedition.lcdui.Command, javax.microedition.lcdui.Displayable)
* /
Public void commandAction (Command arg0, Displayable arg1)
(
/ / TODO Auto-generated method stub
If (arg0 == List.SELECT_COMMAND)
(
If (this.getSelectedIndex () == 0)
(
Uicontroller.handleEvent (UIController.EventID.DISPLAY_NON_BUFFER);
)
Else
(
Uicontroller.handleEvent (UIController.EventID.DISPLAY_BUFFER);
)
)
)
)
Import javax.microedition.lcdui.Command;
Import javax.microedition.lcdui.CommandListener;
Import javax.microedition.lcdui.Display;
Import javax.microedition.lcdui.Displayable;
/ *
* Created on 2004-7-3
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
* /
/ **
* @ Author P2800
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
* /
Public class UIController implements CommandListener
(
Private Display display;
Private DoubleBufferMIDlet midlet;
Private MainListUI mainList;
Private NonDoubleCanvas noDoubleCanvas;
Private DoubleCanvas doubleCanvas;
Public static final Command backCommand = new Command ( "Back" Command.BACK, 2);
Public static class EventID
(
Public static final int DISPLAY_NON_BUFFER = 0;
Public static final int DISPLAY_BUFFER = 1;
)
/ **
*
* /
Public UIController (DoubleBufferMIDlet midlet)
(
This.midlet = midlet;
/ / TODO Auto-generated constructor stub
)
Public void init ()
(
Display = Display.getDisplay (midlet);
MainList = new MainListUI (this);
NoDoubleCanvas = new NonDoubleCanvas (this);
DoubleCanvas = new DoubleCanvas (this);
AddCommand ();
Display.setCurrent (mainList);
)
Public void addCommand ()
(
NoDoubleCanvas.addCommand (backCommand);
DoubleCanvas.addCommand (backCommand);
)
Public Display getDisplay ()
(
Return display;
)
Public void setCurrent (Displayable disp)
(
Display.setCurrent (disp);
)
Public void handleEvent (int eventID)
(
Switch (eventID)
(
Case EventID.DISPLAY_BUFFER:
(
SetCurrent (doubleCanvas);
System.out.println (EventID.DISPLAY_BUFFER);
Break;
)
Case EventID.DISPLAY_NON_BUFFER:
(
SetCurrent (noDoubleCanvas);
System.out.println (EventID.DISPLAY_NON_BUFFER);
Break;
)
)
)
Public void commandAction (Command arg0, Displayable arg1)
(
/ / TODO Auto-generated method stub
If (arg0 == backCommand)
(
Display.setCurrent (mainList);
)
)
)
Import javax.microedition.lcdui.Canvas;
Import javax.microedition.lcdui.Command;
Import javax.microedition.lcdui.CommandListener;
Import javax.microedition.lcdui.Displayable;
Import javax.microedition.lcdui.Graphics;
/ *
* Created on 2004-7-3
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
* /
/ **
* @ Author P2800
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
* /
Public class NonDoubleCanvas extends Canvas implements CommandListener
(
/ / Private DoubleBufferMIDlet midlet;
Private UIController uicontroller;
Private int width;
Private int height;
/ **
*
* /
Public NonDoubleCanvas (UIController uicontroller)
(
Super ();
This.uicontroller = uicontroller;
Width = this.getWidth ();
Height = this.getHeight ();
This.setCommandListener (this);
/ / TODO Auto-generated constructor stub
)
/ * (Non-Javadoc)
* @ See javax.microedition.lcdui.Displayable # paint (javax.microedition.lcdui.Graphics)
* /
Protected void paint (Graphics arg0)
(
/ / TODO Auto-generated method stub
Arg0.setColor (100,100,100);
Arg0.drawString (isDoubleBuffered ()+"", width / 2, height / 2, Graphics.HCENTER | Graphics.TOP);
For (int i = 2, j = 2; i
Arg0.drawRect (i, j, i-2 * width, height-2 * j);
)
)
/ * (Non-Javadoc)
* @ See javax.microedition.lcdui.CommandListener # commandAction (javax.microedition.lcdui.Command, javax.microedition.lcdui.Displayable)
* /
Public void commandAction (Command arg0, Displayable arg1)
(
/ / TODO Auto-generated method stub
Uicontroller.commandAction (arg0, arg1);
)
)
Import javax.microedition.lcdui.Canvas;
Import javax.microedition.lcdui.Command;
Import javax.microedition.lcdui.CommandListener;
Import javax.microedition.lcdui.Displayable;
Import javax.microedition.lcdui.Graphics;
Import javax.microedition.lcdui.Image;
/ *
* Created on 2004-7-3
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
* /
/ **
* @ Author P2800
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
* /
Public class DoubleCanvas extends Canvas implements CommandListener
(
/ / Private DoubleBufferMIDlet midlet;
Private UIController uicontroller;
Private Image offImage;
Private int width;
Private int height;
/ **
*
* /
Public DoubleCanvas (UIController uicontroller)
(
Super ();
This.uicontroller = uicontroller;
Width = this.getWidth ();
Height = this.getHeight ();
This.setCommandListener (this);
If (! IsDoubleBuffered ())
(
OffImage = Image.createImage (width, height);
)
/ / TODO Auto-generated constructor stub
)
/ * (Non-Javadoc)
* @ See javax.microedition.lcdui.Displayable # paint (javax.microedition.lcdui.Graphics)
* /
Protected void paint (Graphics arg0)
(
/ / TODO Auto-generated method stub
Arg0.drawString (isDoubleBuffered ()+"", width / 2, height / 2, Graphics.HCENTER | Graphics.TOP);
Graphics saved = arg0;
If (offImage! = Null)
(
Arg0 = offImage.getGraphics ();
)
Arg0.setColor (255,128,128);
For (int i = 2, j = 2; i
Arg0.drawRect (i, j, i-2 * width, height-2 * j);
)
If (arg0! = Saved)
(
Saved.drawImage (offImage, 0,0, Graphics.LEFT | Graphics.TOP);
)
)
/ * (Non-Javadoc)
* @ See javax.microedition.lcdui.CommandListener # commandAction (javax.microedition.lcdui.Command, javax.microedition.lcdui.Displayable)
* /
Public void commandAction (Command arg0, Displayable arg1)
(
/ / TODO Auto-generated method stub
Uicontroller.commandAction (arg0, arg1);
)
)
↑ Back
Tags: Double






