More VolatileImage trouble - unpredictable behavior

trust me, i have been reading most of the topics with problems like this. Chances are im not understanding the concept behind VolatileImages, so any help wil be apreciated.

im studying game development with java, and came up to 2D graphics. I have a small library, consisting basicly of 3 classes: Sprite, GamePanel, GameWindow.

im using the basic getInput() - updateState() - drawScene(Graphisc g) in a infinite(for now) loop on the panel. All i want right now is show the sprite on the screen. So far no problems with input and update parts. Here is my stuation:

Sprite class(extended JComponent):

- a constructor, that loads a BufferedImage that is a collection of all the possible frames of the animation, in one single file;

- some frame sequence control methods;

- and the problem: renderSprite(Graphisc2D g)

Here, im using the code2 from "http://java.sun.com/developer/technicalArticles/GUI/java2d/java2dpart2.html#2", without any considerable change. for some reason, creating the VolatileImage using the GraphicsConfiguration on the constructor returns a null pointer exception, so i added in the beggining of the renderSprite.

in the reRender() part, when drawing the scene, i use

g2.drawImage(bimg.getSubimage(sx, 0, area.width, area.height), area.x, area.y, this);

to return a clip from the complete image, with the actual frame of the sprite, at a certain location x, y. But when i add the sprite to the game panel, that runs the main loop, sometimes nothing is drawn in the screen, and the rest of the times, a empty square with the sprite dimensions gets drawn.

i dont think the problem is with the gamePanel(extended JPanel) of with GameWindow(extended JFrame), because worked just fine before i tried using VolatileImage.

Any ideas on this? here is some code:

[1861 byte] By [Bakemono_Samaa] at [2007-10-3 5:35:32]
# 1

public class NewSprite extends JComponent

{

private BufferedImage bimg;

private VolatileImage vimg = null;

private GraphicsConfiguration gc;

private Rectangle area;

private URL url;

private int framesequence[];

private int currentframe;

private int nframes;

NewSprite(String filename, int fw, int fh, int qtdframes)

{

nframes = qtdframes;

area = new Rectangle(0, 0, fw, fh);

framesequence = new int[]{0};currentframe = 0;

url = getClass().getResource(filename);

try

{bimg = ImageIO.read(url);}

catch(IOException ioe)

{System.out.println("Cant read file: " + ioe.toString());}

}

public void nextFrame()

{

if(currentframe < framesequence.length-1)

{currentframe++;}

else

{currentframe = 0;}

}

public void renderFrame(Graphics2D gscreen)

{

gc = getGraphicsConfiguration();

vimg = gc.createCompatibleVolatileImage(area.width, area.height);

int check;

do {

check = vimg.validate(gc);

if(check == VolatileImage.IMAGE_RESTORED)

{rerender();}

else

if(check == VolatileImage.IMAGE_INCOMPATIBLE)

{

vimg = gc.createCompatibleVolatileImage(area.x, area.y);

rerender();

}

gscreen.drawImage(vimg, area.x, area.y, this);

}while(vimg.contentsLost());

}

public void rerender()

{

Graphics2D g2 = (Graphics2D)vimg.createGraphics();

int sx = framesequence[currentframe]*area.width;

g2.drawImage(bimg.getSubimage(sx, 0, area.width, area.height), area.x, area.y, this);

g2.dispose();

}

}

Bakemono_Samaa at 2007-7-14 23:43:06 > top of Java-index,Other Topics,Java Game Development...
# 2

class GamePanel extends JPanel implements MouseListener, KeyListener, Runnable

{

private long prevframetime = 0;

private long nsperframe = 20000000;

private long elapsedtime = 0;

private volatile boolean gameover = false;

private NewSprite nsp;

private NewSprite nsp2;

private Thread th;

public GamePanel()

{

nsp = new NewSprite("/randomimagesnumbered.png", 50, 50, 10);

nsp2 = new NewSprite("/randomimagesnumbered.png", 50, 50, 10);

nsp.setFrameSequence (new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9});

nsp2.setFrameSequence(new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9});

nsp.setPosition(250, 250);

nsp2.setPosition(100, 100);

setLayout(new FlowLayout());

setFocusable(true);

requestFocus();

add(nsp);

add(nsp2);

eventpool = new EventPool();

addKeyListener(this);

addMouseListener(this);

}

public void addNotify()

{

super.addNotify();

startGame();

}

public void startGame()

{

if((th == null)&&(!gameover))

{

th = new Thread(this);

th.start();

}

}

public void run()

{

gameover = false;

Graphics2D g2 = (Graphics2D)getGraphics();

while(!gameover)

{

gameInput();

gameUpdate();

gameRender(g2);

syncFrameRate();

}

}

public void gameInput()

{/*...*/}

public void gameUpdate()

{/*...*/}

public void gameRender(Graphics2D g2)

{

nsp.renderFrame(g2);

nsp2.renderFrame(g2);

nsp.nextFrame();

nsp2.nextFrame();

}

public void syncFrameRate()

{//as seen in http://java.sys-con.com/read/46983.htm

long nextframe = prevframetime+nsperframe;

long currtime = J3DTimer.getValue();

while(currtime<nextframe)

{

Thread.yield();

try

{Thread.sleep(5);}

catch(InterruptedException e)

{System.out.println("Cant Sleep");}

currtime = J3DTimer.getValue();

}

elapsedtime = currtime - prevframetime;

prevframetime = currtime;

}

}

>

Bakemono_Samaa at 2007-7-14 23:43:06 > top of Java-index,Other Topics,Java Game Development...
# 3
stuff not related to the graphics problems were removed like input processing, etc...Message was edited by: Bakemono_Sama
Bakemono_Samaa at 2007-7-14 23:43:06 > top of Java-index,Other Topics,Java Game Development...