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?
Jagan Mohan Reddy
Greenhorn
Joined: May 15, 2001
Posts: 18
posted
0
Hi,
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.. Regards, Jagan Mohan Reddy..
Rob Ross
Bartender
Joined: Jan 07, 2002
Posts: 2205
posted
0
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){ g.setColor();....
instead do: offscreen.setColor();... 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: g.drawImage(image,0,0,this); 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 ]
Rob
SCJP 1.4
Prashanth menon
Ranch Hand
Joined: Feb 20, 2001
Posts: 65
posted
0
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. Prashanth
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.