Application Double buffer technology to solve the problem of blinking screen in J2ME

zhaozj2021-02-12  146

Sometimes we may encounter a problem with a screen in J2ME, and we can apply dual buffered technology to solve it, but because more and more mobile phone itself supports double buffering. So just make a simple introduction here.

I wrote the program I originally want to use and unused differences on Nokia 6108. As a result, this phone he supported double buffer, so there is no comparison. But understand this technology or necessary, so I decided to write it. Double buffer is originally encountered when developing a PC application. Also exists in MIDP development, when you make original photo on the screen, if you are very complicated, the user will find the interface flashing. So you can do this, first do Paint () on another picture, when you have completed it, you copy it to the screen, because you usually get the speed will not flash quickly. This technology is a double buffer.

The Canvas class provides the isDoublebuffered () method to determine whether the device supports this feature. If you return True, then we don't have to use double buffering. If false, we can do this: public doubletroller uiicontroller {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

}

After obtaining the graphics instance of OFFIMAGE, PAINT () is available. This part is usually more complicated, I am not complicated here :) Then I will execute the offimage directly to the screen is also saved.drawimage (Offimage, 0, 0, graphics.Left | graphics.top); I wrote an app Compare the effects of double buffering, but because my mobile phone supports double buffer, I can't see the effect. If you are interested, you can modify some of the Paint () part of the code and then transplanted to an unsupported mobile phone. You can see the effect, below is the code of my program: 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 import umplements 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 {uiicontroller.handleevent (uilicontroller.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 uilicontroller imports 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_buff); Break;} case eventid.display_non_buffer: {setCurrent (nodoublecanvas); system.out.println (eventid.display_non_buffer); BREAK;

}}} Public void command (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

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 uicontr oller; private Image offImage; private int width; private int height; / ** * * / public DoubleCanvas (UIController uicontroller) {super (); this.uicontroller = uicontroller; width = this.getWidth (); height = this.getHeight (); 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

转载请注明原文地址:https://www.9cbs.com/read-7690.html

New Post(0)