Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (754)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (842)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [Solved]Overlay fonts over BufferedImage if enabled, and don't overlay otherwise  (Read 1167 times)
0 Members and 1 Guest are viewing this topic.
Offline tom_mai78101
« Posted 2014-05-19 11:25:09 »

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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
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?
Offline ctomni231

JGO Wizard


Medals: 99
Projects: 1
Exp: 7 years


Not a glitch. Just have a lil' pixelexia...


« Reply #1 - Posted 2014-05-20 01:07:05 »

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


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...)

Offline tom_mai78101
« Reply #2 - Posted 2014-05-20 04:35:54 »

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


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
g2d.dispose();
cause memory leaks? Java specifications warned that Graphics (and its subclasses) are to be disposed of when not in use.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline tom_mai78101
« Reply #3 - Posted 2014-05-20 09:40:25 »

Hey!  Grin

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

http://stackoverflow.com/questions/6317732/how-to-get-small-images-from-big-bufferedimage-really-fast

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!
Pages: [1]
  ignore  |  Print  
 
 

 
DesertCoockie (33 views)
2018-05-13 18:23:11

nelsongames (75 views)
2018-04-24 18:15:36

nelsongames (70 views)
2018-04-24 18:14:32

ivj94 (752 views)
2018-03-24 14:47:39

ivj94 (82 views)
2018-03-24 14:46:31

ivj94 (622 views)
2018-03-24 14:43:53

Solater (98 views)
2018-03-17 05:04:08

nelsongames (179 views)
2018-03-05 17:56:34

Gornova (405 views)
2018-03-02 22:15:33

buddyBro (1065 views)
2018-02-28 16:59:18
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!