aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Flickering problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Flickering problem" Watch "Flickering problem" New topic
Author

Flickering problem

ji yan
Greenhorn

Joined: Feb 15, 2002
Posts: 7
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
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
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
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
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Flickering problem