|
by Frédéric Patin aka yov Friday 30th July 2004, 18:14pm for http://www.yov408.com |
|
| File/Method | Description of the method | When to use it |
|
Java Applet in a Frame WINDOWED/APPLET |
This method creates a new windowed Frame, then creates a new Applet, and adds the Applet to the Frame's content pane. So basically we have a Java Applet, included in a standard Java Frame (a window). Then a BufferedImage is created. For each loop, data is drawn to this BufferedImage using setRGB(), and then the BufferedImage is blit to the applets Graphics (retrieved using getGraphics()). This blit is done with the drawImage() function. | This method is useful for an application that could be ported to a Java Applet alone, to include in a web page for example. As it is provided here the Applet is included in a window but in a few minutes it can be changed to fit as a pure Java Applet. In terms of speed, this method is quite slow and heavy in code, indeed we have to deal with the Applet, the Frame, the Frame panel, the Graphics, the BufferedImage etc... Lots of mess for a simple task :/ |
|
Java Applet in a Frame w/ Menu and Buttons WINDOWED/APPLET |
The principle is the same, except that, with the applet, we add a Button and a Menu to the Frame. This only demonstrates how you can have double buffering in only a part of the window and use the rest for buttons or menus. Watchout for the Layout Manager stuff. This is an automatic way that Java provides to position the elements in the window. It is useful when the window is changed sized so that the elements always position themselves correctly. In this example I don't use any Layout Manager, and the elements are positioned in the window using absolute coordinates :). | No comments in particular, shows how to add buttons and menus along with the applet in the window. Even slower than the one before as the application has several tasks running at the same time (dealing with the buttons and menus), and more interrupts. |
|
Double Buffering in a Frame and Panel WINDOWED |
This method is completely 'applet free'! It is quite hard to find tutorials on double buffering in Java without using applets, so here it is. Okay, so the program creates a Frame, Creates a new Panel in the Frame, and draws in it. As usual we call a getGraphics(), for the Panel, to retrieve the Graphics where we will draw. Then we create a BufferedImage, and set pixels in this image. We then blit the image to the Graphics of the Panel. | Fast enough. Quite simple, not too much memory blits and screen accesses. Graphics are included in a panel which enables the programmer to add other stuff in the window and still have the animation running in a particular area. |
|
Double Buffering in a Frame WINDOWED |
The simplest I found and probably the fastest, it uses a simple BufferedImage and blits it to the Frame's Graphics. I'd say this is an exclusive mode, you can't add anything else in the window. | The fastest and simplest I could do. No special classes are used except the essentials, BufferedImage and Frame. For me the best method if you have to draw your animation's graphics pixel by pixel at runtime. |
|
Hardware Double Buffering FULLSCREEN |
This program switches to fullscreen and requests double buffering, and within the possibilities of the platform, page flipping. For each loop, we request the Graphics 'offscreen' and draw to those Graphics. Then the offscreen buffer is automatically blitted to the screen and so on. So the big problem is that we only have a Graphics class to represent the offscreen buffer, and therefore it's impossible to set only one pixel in this offscreen buffer. | This method is probably the fastest in full sceen mode as long as you don't need pixel access. You can only use the methods provided by the Graphics class, to draw to the offscreen buffer. I had tried a trick to draw pixels by drawing one pixel long lines (there is a line function in the Graphics class) but this is tragically slow. So keep this method if the Graphics functions supplied in the Graphics class are sufficient to your needs. |
|
Hardware Double Buffering w/ Image Buffering FULLSCREEN |
This program uses the same technique as before only this time pixel access is granted by the use of a BufferedImage which is copied to the offscreen buffer Graphics. This is basically triple buffering. | This solution is probably more experimental than anything else. It is quite ugly and as a matter of fact quite slow. |
|
Image Buffering FULLSCREEN |
This is the final solution I would suggest to make a full screen demo or game. It is only a full screen window (Frame) too which we blit a BufferedImage on each loop. This gives us double buffering, and pixel writing/reading. | By far the simplest solution which also provides, if you have massive pixel writing, good performances. |
|
Memory Image Source FULLSCREEN |
Oups, I hadn't tried this method before but it is indeed by far the fastest, and really by far. To set all the pixels on the screen I get about 16 times faster frame rates (1024*768*32) than the previous method. So very nice method to draw lots of pixels on the screen if you have a run time generated animation or image, this is your chance :). It's a bit tricky to handle sometimes because the pixel array is only a list without rows and columns, but it's really worth it. | No comment, simply de best perfomances, nice. |
Wednesday 14 April 2004, 21:55 GMT.
Article by Frédéric Patin aka yov (http://www.yov408.com).
Sources by Frédéric Patin, Sun Tutorials, and JavaHelp.com.