⬆️ ⬇️

Double buffering or Back to the past. Part two

Good day!



Introduction



It is the fourth of January already in the yard, but my soul still does not calm down. So I decided to continue the topic of writing J2ME applications. Plus, several people have shown serious interest in this topic. And it was not only ordinary users of the habr, but also read-only accounts. Well, okay, closer to the subject.

Literally immediately after the publication of the topic , very sensible comments were received from habrauzer barker , namely, the remark , which is essentially a common truth, and the second comment is an amendment , no less sensible.





What are we going to talk about today?



Today we will talk about the double buffering process in javax.microedition.lcdui.Canvas and why javax.microedition.lcdui.game.GameCanvas was created.

')

What is double buffering?



Double buffering is nothing more than a technique that uses the second (off-screen) buffer to draw shapes, sprites, and so on into it, and then copy its contents to the screen. The problem is that when drawing directly, i.e. drawing directly into the screen buffer in time does not fit the screen redraw time (in Canvas, this is done by the repaint () function) and the screen simply starts blinking, i.e. the user sees in front of him the intermediate result of this very drawing. Using this very technique allows the developer to avoid these “blinks”. However, in Canvas, the use of this technique is a process of cycling, because the developers of the standard and the J2ME platform did not take care of this.



"Double Buffering" in Canvas



The process of “double buffering” in Canvas is performed using an image (the Image object of the javax.microedition.lcdui package) as an offscreen buffer. Like this:

import javax.microedition.lcdui.Canvas; import javax.microedition.lcdui.Graphics; /*      */ import javax.microedition.lcdui.Image; public class OurCanvas extends Canvas { Image img; //  Image Graphics buf; //  ,  int w; //   int h; //   public OurCanvas() { //  Lego w = getWidth(); //    h = getHeight(); //  //  ,   " "   //  ,           //   ,   Image if (!isDoubleBuffered()) { buffer = Image.createImage(w,h); } //     img buf = img.getGraphics(); } //     public void draw(Graphics g) { g.setColor(0xffffff); g.fillRect(0,0,w,h); g.setColor(0x111111); g.fillRect(25,25,125,125); g.setColor(0xababab); g.fillRect(70,60,70,60); } public void paint(Graphics g) { g.drawImage(0,0,w,h); //    draw(buf); //    } } 




That's all. The code contains more than visual comments, so parsing the code should not cause you problems. Now consider the “double buffering” in GameCanvas .



"Double buffering" in GameCanvas



Some time passed and the J2ME consortium developed the javax.microedition.lcdui.game package, which contained GameCanvas , which was all the same Canvas , but with the “double buffering” problem solved. Programmers no longer need to take care of it. The code will look like this:

 import javax.microedition.lcdui.game.GameCanvas; import javax.microedition.lcdui.Graphics; /*      */ import javax.microedition.lcdui.Image; public class OurCanvas extends GameCanvas implements Runnable { Graphics buf; Thread t; int w; //   int h; //   public OurCanvas() { //   OurCanvas w = getWidth(); //    h = getHeight(); //  //     Graphics buf = getGraphics(); //   t = new Thread(this); } // let's draw public void run { g.setColor(0xffffff); g.fillRect(0,0,w,h); g.setColor(0x111111); g.fillRect(25,25,125,125); g.setColor(0xababab); g.fillRect(70,60,70,60); flushGraphics(); //     " " } } 


Here we do not need to take care of the buffer - everything is immediately drawn into it, and then when flushGraphics is called , the entire contents of the offscreen buffer is copied to the screen.



That's all



Despite the fact that this task of drawing is solved in a few lines - this is quite an important topic in which developers can not "swim." I hope today's lesson was not less instructive than the last one . That's all, allow me to leave.



See you soon!



Drink coffee, write to Java.





Post Scriptum



The source of the tradition you can pick up on Pastebin.

Here is the first example .

And here - the second .

Source: https://habr.com/ru/post/135700/



All Articles