• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Flickering problem

Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using java to write a little game in which I have a background that keeps moving while other elements of the game such as plane, monsters keep moving on the background,too. Now, everytime the background re-renders itself, it will clear the whole background that includes my monsters, planes...etc, and then the whole world will be redrawn again. This gives away a severe problem of constant flicking. Now, if everytime when the background redraws itself,it is able to only redraw the part that is not overlapped by other elements, flickering will be largely reduced. Can anyone tell me how I can do that?
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If we use the bufferedImage class the flickering might be largely reduced..Though i have not tried on it,i think this class might be helpful for u..
Jagan Mohan Reddy..
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try double-buffering your paint operations.
In your applet, create these member variables:
Graphics offscreen;
Image image;

in your init() method, add this code:
image = createImage(bounds().width,bounds().height); // make offscreen buffer
offscreen = image.getGraphics();
Now, when you paint, instead of calling operations on the Graphics passed into paint, use the offscreen you created above.
for example, in your paint:
public void paint(Graphics g){

instead do:
Use the offscreen graphics to draw what you normally draw in your paint(). Then, at the very end of paint(), you copy the offscreen image to your actual graphics:
This will cause all drawing to be done offscreen, then copied in one operation to the screen. This will reduce your flicker.
Good luck!
[ March 13, 2002: Message edited by: Rob Ross ]
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you call the paint method it usually goes for flickering.
try public void update(Graphics g)
I would also suggest you to use Double Buffering.
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And to maximize efficiency (of the computer's processor time, not the programmer's thinking and coding time), you'll want to define a shape as your clipping path where you want the graphics to be updated. A basic tutorial on this subject can be found in Clipping The Drawing Region from Sun's Java Tutorial. More information is available in The Java API Specification. Quoting from the Graphics class documentation:

All rendering operations modify only pixels which lie within the area bounded by the current clip, which is specified by a Shape in user space and is controlled by the program using the Graphics object. This user clip is transformed into device space and combined with the device clip, which is defined by the visibility of windows and device extents. The combination of the user clip and device clip defines the composite clip, which determines the final clipping region. The user clip cannot be modified by the rendering system to reflect the resulting composite clip. The user clip can only be changed through the setClip or clipRect methods.

Good Luck.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic