Discussion on the realization of J2ME game double buffering

Source: Internet
Author: User
Tags final

The application of double buffering technology is very extensive, it is needed when designing the game. In midp1.0, the API does not game this package, see a lot of people on the Internet in the discussion of the design of the game will appear when the picture breaks, screen flashing and so on.

I after these days of learning to organize their own learning experience, used to throw bricks, I hope that there are experts to discuss each other. Let me also learn to study.

The principle of double buffering can be understood in such a way that the computer screen is considered a blackboard. First of all, we create a "virtual" blackboard in the memory environment, and then on this blackboard to draw complex graphics, and so on, and so on, once again, the plot of the memory of the graphic "copy" to another blackboard (screen). This method can improve the drawing speed and greatly improve the drawing effect.

For mobile phones. The specific process is through extends Canvas. Then get the bufferimage. And then you getgraphics. The final thing is to draw a picture in this graphics, and then finally to draw the bufferimage on the screen.

Say the word. It still depends on the code. The code inside refers to some open source code.

Java code

Package org.wuhua.game;

Import Javax.microedition.lcdui.Canvas;

Import Javax.microedition.lcdui.Graphics;

Import Javax.microedition.lcdui.Image;

/**

* Class Name: Gamecanvas.java

Date of preparation: 2006-11-29

Program Function Description:

* Implement double buffered game canvas. The principle of implementation is to create a bufferimage. Then draw, and finally show it out. It's as simple as that.

Demo:

Bugs:

*

*

* Program Change Date:

Change Author:

Change Description:

*

* @author Wuhua

*/

Public abstract class Gamecanvas extends Canvas {

/**

* Draw a buffered picture. When a user draws a resource, it is done by manipulating the image.

*/

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 necessary if you want to draw a specified range.

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 ();

}

/**

* The main designer is to get the picture by calling this method. And then you can draw the

* @return

*/

Protected Graphics getgraphics () {

return This.bufferImage.getGraphics ();

}

/**

* This method is mainly to deal with the Nokia platform, the user calls Setfullscreenmode (Boolean enable) in the new W & H to create a buffer picture

*/

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.