J2ME Game development Skills

Source: Internet
Author: User

Not long ago I used J2ME to develop a MotoT720 under the Color game-gem box (Gridone). The development process has accumulated some experience, now write to share with you.

Use double buffering to avoid screen flicker

Double buffering technology is one of the key techniques to write J2ME game program. In fact, double buffering technology is a traditional technology of computer animation. The main reason for screen flicker is that the picture is displayed while the program is changing it, so the picture flashes. The solution is to open up an area in memory as a background screen, the program update it, modify, and then show it. The displayed image is always a fully-painted image, and the program will not modify the image that is being displayed. Of course there are other ways to solve the problem of screen flicker, but the use of dual buffering technology is a recommended solution. You can see the following code for the specific method:

public class BlocksCanvas extends Canvas implements Runnable
{
Graphics bg;
Image buf;
public BlocksCanvas()
{
 ......
 height = getHeight();
 width = getWidth();
 //按屏幕大小建立缓冲对象
 buf = Image.createImage(width, height);
 //将缓冲对象的Graphics附给bg
 bg = buf.getGraphics();
 ......
}  
public void run()
{......
 for(i=0;i<ROWS;i++)
 {
  for(j=0;j<COLS;j++)
  {//画方块
  drawBlock(x,y);
  }
 }
 repaint();
}
private void drawBlock(int block_x, int block_y)
{
 //取得方块的坐标
 int x = getLeft(block_x);
 int y = getTop(block_y);
 //取得方块的颜色
 int c= board[block_x][block_y];
 bg.drawImage(imgs[c], x, y, Graphics.TOP | Graphics.LEFT);
}
public void paint(Graphics g)
{
 g.drawImage(buf, 0, 0, Graphics.TOP | Graphics.LEFT);
}
}

From the above code can be seen, the double buffering thought embodied in the program is to complete the following steps in sequence:

1. Define a Graphics object BG and an Image object buf, create a Buffer object by screen size attached to BUF, and then obtain BUF graphics object attached to BG. Here, the Graphics object can be interpreted as a buffered screen, and the image object can be used as a picture on the buffered screen.

2. Drawing on the BG (buffer screen) using DrawImage () and drawstring statements is equivalent to drawing on the buffer screen.

3. Call the repaint () statement, which functions by telling the system to call paint () to complete the display of the real screen. It should be noted here that paint () is a system call statement that cannot be invoked by hand and can only be invoked through the paint () statement.

4. In the Paint (Graphics g) function, the BUF (the picture on the buffer screen) is drawn to the real screen.

Although the above steps may seem cumbersome, but the effect is very good. If you want to display something on the screen, just draw it on the BG, and then call Repaint () to show it.

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.