Implementation of Dual-buffer in j2e-hpc

Source: Internet
Author: User

The dual-buffer technology is widely used and is especially needed when designing games,
In midp1.0, the game package does not exist in the API. Many people on the Internet have problems such as image breakage and screen flickering when discussing the design of the game.
After several days of study, I sorted out my learning experience and used it to sell bricks. I hope that experts will discuss this with each other. Let me also learn.

The principle of double buffering can be understood as follows: the computer screen is regarded as a blackboard. First, we create a "virtual" blackboard in the memory environment, and then draw complex graphics on this blackboard. When all the graphics are finished, copy the image drawn in the memory to another blackboard (screen) at one time. This method can increase the drawing speed and greatly improve the drawing effect.
For mobile phones. The specific process is through the extends canvas. Then obtain bufferimage. And then getgraphics. The last step is to draw an image in this graphics, and finally draw the screen of the drawn bufferimage.
Let's say it all. It depends on the code. The code in references some open source code.
Java code

    /*************************************** *****************************

  • * Project name:Football project j2_client
    *
  • * Copyright 2005-2006 teesoo. all rights reserved ************************************* *******************************/
  • Package org. Wuhua. game;
  • Import javax. microedition. lcdui. Canvas; import javax. microedition. lcdui. graphics;
  • Import javax. microedition. lcdui. image;
  • /***Class Name: gamecanvas. Java 
    Written on: 2006-11-29
    Program function description:
  • * Implements a game canvas with double buffering. The implementation principle is to create a bufferimage. Then draw and display it. That's simple.
    Demo:
    BUG :*
  • ** Program change date:
    Author:
    Change description:
  • ** @ Author Wuhua
  • */
    Public
    Abstract
    Class gamecanvas extends canvas {
  • /**
  • * Draw a buffered image. This image is used when users draw resources */
  • Private image bufferimage;
  • Private
    Int height;
  • Private
    Int width;
  • Private
    Int clipx, clipy, clipwidth, clipheight;
  • Private
    Boolean setclip;
  • Protected gamecanvas (){
  • Super ();
  • Width = getwidth (); Height = getheight ();
  • This. bufferimage = image. createimage (width, height );
  • }
  • Protected
    Void paint (Graphics g ){
  • // This is required if you want to draw a specified area.
    If (this. setclip ){
  • G. cliprect (this. clipx, this. clipy, this. clipwidth, this. clipheight); this. setclip = false;
  • } G. drawimage (this. bufferimage, 0, 0, graphics. Top | graphics. Left );
  • }
  • Public
    Void flushgraphics (int x, int y, int width, int height ){
  • This. setclip = true; this. clipx = X;
  • This. clipy = y; this. clipwidth = width;
  • This. clipheight = height;
  • Repaint (); servicerepaints ();
  • }
  • Public
    Void flushgraphics () {repaint ();
  • Servicerepaints ();}
  • /**
  • * Designers call this method to obtain images. Then you can plot * @ return
  • */
    Protected graphics getgraphics (){
  • Return
    This. bufferimage. getgraphics ();}
  • /**
  • * This method is mainly used to process the Nokia platform. When the user calls setfullscreenmode (Boolean enable), the user creates a buffer Image Based on the New W & H */
  • Protected
    Final
    Void sizechanged (int w, int h) {If (h> height ){
  • This. bufferimage = image. createimage (W, H );}
  • }
  • }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.