[Solved]Overlay fonts over BufferedImage if enabled, and don't overlay otherwise

This is what it looks like when all the text have been rendered/overlaid, just before initiating the “SET” sequence.

And this is the aftermath:

I have a looping code snippet that, by my logic, overlays a given font over its BufferedImage.


BufferedStrategy bs = getBufferedStrategy();
Graphics g = bs.getDrawGraphics();
g.setColor(Color.black);
g.fillRect(0, 0, getWidth(), getHeight());
game.render(g, output);
//...
     BufferedImage old_img = output.getBufferedImage(); //Obtain BufferedImage.
     Graphics2D g2d = old_img.createGraphics(); //Get the Graphics component of the BufferedImage.
     g2d.drawImage(old_img, 0, 0, null); //Draw the original BufferedImage over it.
     renderText(g2d); //Draw text on top of the BufferedImage.
     g2d.dispose(); //Dispose it to free up memory.
     g.drawImage(old_img, 0, 0, null); //Draw what I supposed a BufferedImage with text overlaid on top of it. (INCORRECT)
//...
g.dispose();
bs.show();

The thing is, the results never show anything that was drawn using Graphics.drawString(). Does anyone know what else I should do to make the text appear using the fonts I provided?

[icode] g2d.dispose(); //Dispose it to free up memory. [/icode]

What happens when you get rid of this line? (Sorry, the amount of code you showed is on the small side, kind of hard to help without a little bit more if this doesn’t work out…)

Yes, I’m sorry about that. The relevant bits in the code snippets are the ones that I just added to work on the text rendering. Just that small logic.

Wouldn’t removing [icode]g2d.dispose();[/icode] cause memory leaks? Java specifications warned that Graphics (and its subclasses) are to be disposed of when not in use.

Hey! ;D

Fixed it. Went around on Stack Overflow looking for ways to overlay fonts, when I came across this:

That gave me ideas on working around the problem by allowing the BufferedImage in the logic code snippet to be smaller. And while I was working on these ideas, I found the bug that’s causing my font problems. I went ahead fixing it, and now everything is cleared.

Thanks Stack Overflow, you’re the best!