Question

I'm making a 2 player game using a JApplet. I draw the first player's screen on the left side, and the second players screen on the right side, then draw the minimap of the entire map in the bottom center. I draw everything to a buffered Image called bimg then use bimg.getSubimage to get both sides for each player based on their locations. I use the standard drawImage method with extra parameters to draw a scaled version of the entire image of the map. The problem I'm having is that the minimap I draw flickers every few frames, it appears that sometimes it draws the left side first, and other times draws the right side first, instead of drawing both sides and then the map ontop of them. I'm wondering if there's a method anyone can suggest that would possibly help fix this problem. I hope the context I've provided is clear enough, here is some code to reference. Thanks

drawP1Side = bimg.getSubimage(p1.viewX, p1.viewY, 400, 700);
drawP2Side = bimg.getSubimage(p2.viewX, p2.viewY, 400, 700);
g.drawImage(drawP1Side, 0, 0, null);
g.drawImage(drawP2Side, 410, 0, null);
g.drawImage(bimg, 320, 500, 200, 200, null);
Was it helpful?

Solution

Based on the description...

Welcome to the wonderful world of reasons why you shouldn't override paint of top level containers.

Basically, top level containers, like JApplet are not double buffered, this causes them to flicker when they are updated.

Instead, you should create yourself a custom component, extending from something like JPanel and override it's paintComponent method and perform your custom painting there...

Take a look at Performing Custom Painting for more details

All Swing components implement ImageObsever (from JComponent), so you should be passing this as the ImageObserver parameter to drawImage, assuming you're calling it from within some JComponent...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top