wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Can't get Graphics.setFont() to work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Can Watch "Can New topic
Author

Can't get Graphics.setFont() to work

Oliver Stuttgart
Greenhorn

Joined: Jan 22, 2010
Posts: 13
Hi everybody,

for some reason I can't get Graphics.setFont() to work.

What I'm trying to do here is to write text on a transparent image and then save this image.
While this generally works, I am not able to modify the font.

The first System.out shows the font as I have defined it. This is OK.
I then set this font in Graphics.setFont(), which also seems to work.
In the second System.out I read the used font back - nothing has changed.
The created image also doesn't change.

This is the relevant code:



And the output:

Oliver@oliver-desktop:~/JavaProjects$ java SaveImage
Font defined: java.awt.Font[family=Serif,name=Serif,style=bold,size=40]
Font used: java.awt.Font[family=Dialog,name=Dialog,style=plain,size=12]


Any ideas?

Cheers,

Oliver
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2266
    
    3

Howdy, Oliver!

By just looking at your code, my guess is that when you say image.createGraphics().setFont(font);, you are setting the font in a new Graphics2D object, because you are invoking the createGraphics() method, which returns a new Graphics2D object.


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
My guess would be that each call to createGraphics throws away the previous Graphics object; try reusing it.
Oliver Stuttgart
Greenhorn

Joined: Jan 22, 2010
Posts: 13
Hi

and thanks for your incredibly quick replies!

The guess that createGraphics() threw away all the previous settings, proofed to be right.
I have modified the code, it works now flawlessly. I however wonder, if my solution is good style, as the thing with 'Graphics g = image.createGraphics();' looks somewhat strange to me!?
I thought that I could replace 'image' with 'g' in line 17, however this did not work. When I left it at 'image'", everything was OK, even the created PNG file.

This is the code:

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
I think this question would sit better on our GUIs forum. Moving.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Oliver Stuttgart wrote:
I however wonder, if my solution is good style, as the thing with 'Graphics g = image.createGraphics();' looks somewhat strange to me!?


Check the API. This is one way to create a Graphics2D object that works with the Image, image.


I thought that I could replace 'image' with 'g' in line 17, however this did not work. When I left it at 'image'", everything was OK, even the created PNG file.


Again the API will help. You won't find an overload of the write method that takes a Graphics object as it's first parameter.


This is the code:...


I've one comment here. You'll want to dispose of the Graphics when you are done with it.

Luck!
Oliver Stuttgart
Greenhorn

Joined: Jan 22, 2010
Posts: 13
Hi Pete,

thanks for the riddle.

Could you possibly give me a hint? I am a total noob and this is the first 'real' program I'm trying to write.

What I want to do, is to create an image in my computer's memory, draw some text on it and save it to the hard disc as PNG.
I never want to display it on the screen. To do this, I need to use BufferedImage, right?

What I understood from studying the API and some examples on the internet, the only way to fill the BufferedImage with content
is the method getGraphics(), which gets me a Graphics2D object.

Am I right so far?

And then, what? I am already again on the Graphics route!? All I see is that I could have used directly Graphics2D instead of Graphics,
which it extends.

I just searched the internet for "save Graphics2d" and "save BufferedImage", but only found examples in which they took pretty much
the same approach like I did.

Again - a hint would be nice.

Cheers,

Oliver
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

Images only get display on the screen if you want them to, just creating the image wont write it to the screen. You should search for writing images to Files and what that entails.

Hunter


"If the facts don't fit the theory, get new facts" --Albert Einstein
Oliver Stuttgart
Greenhorn

Joined: Jan 22, 2010
Posts: 13
Hi Pete, hi Hunter,

I am sorry, but even after hours of research I can't find a way to significantly improve may code.

Even Oracle, as well as many other websites, suggests a similar approach:
Oracle Java website

--> make a buffered image, draw on it suing Graphics or Graphics2D, store it using ImageIO.

So: forget my request for a hint - please make a specific suggestion.

Cheers,

Oliver
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18709
    
    8

Sorry, I'm lost. Originally you had a problem but then it was solved, wasn't it? So do you still have a problem? And what is it?
Oliver Stuttgart
Greenhorn

Joined: Jan 22, 2010
Posts: 13
Paul Clapham wrote:Sorry, I'm lost. Originally you had a problem but then it was solved, wasn't it? So do you still have a problem? And what is it?


Yes, first it did not work. Then I received some suggestion which helped me to find a solution. The solution, which I had posted yesterday, at 13:25:32 works great, I was however wondering if this was good style, as it looks somewhat strange to me (I am an absolute beginner).

Then there were Pete's and Hunter's posts, which I understood that way, that I should take a completely different approach, which I was however not able to find.

I am now quite confused if my code is generally OK or not and how I could improve it.

Cheers, Oliver
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

I think you should re explain what you are having trouble with in a more organized manner. It's hard for us to help when we confused as to what you want to do.


Hunter
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18709
    
    8

Oliver Stuttgart wrote:I am now quite confused if my code is generally OK or not and how I could improve it.


Are you asking about your code block which you posted at... I mean, about 10 hours ago? It looks okay to me. And if you swiped it from an Oracle tutorial, that's a good way to get started in an area which you don't know much about.

Of course there's the requirement that it should do what you meant it to do. Does it do that? In other words, does it "work"? If it does the wrong thing then that's definitely something which should be fixed.
Oliver Stuttgart
Greenhorn

Joined: Jan 22, 2010
Posts: 13
Hunter McMillen wrote:I think you should re explain what you are having trouble with in a more organized manner. [...]


All right. So, this is my solution:



I am especially wondering about what happens in line 9 and if it is OK what I am doing in 11 and 12. In 9 I say, that g shall be a Graphics objects, in 11 I set the font, in 12 I write a string on it. So far so good.

Somehow these modifications stay connected with the image object. This pretty much confuses me. I would have expected, that g is now the object which I will have to put into ImageIO.write, what however doesn't work.
I know that it is hard to understand everything 100% at the beginning, but I don't want to start programming total crap, which possibly works in a simple environment, but crashes in a more complex use case.

@Paul:
The example on the Oracle website is just a outline of the ImageIO concept. And yes, my solution seems to work flawlessly.

If you guys say it's OK, I'm happy. If you make suggestions, I'm happy, too.
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

This seems to work fine for me. In reference to lines 11 and 12, the methods you are using on those lines exist to be used in the manner you are using them.

Hunter
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Oliver Stuttgart wrote:
I am especially wondering about what happens in line 9 and if it is OK what I am doing in 11 and 12. In 9 I say, that g shall be a Graphics objects, in 11 I set the font, in 12 I write a string on it. So far so good.

Somehow these modifications stay connected with the image object. This pretty much confuses me. I would have expected, that g is now the object which I will have to put into ImageIO.write, what however doesn't work.


Again I recommend, no I urge you to read the API as all your questions are answered there. If you read the BufferedImage API section on getGraphics and createGraphics, you will see that it states that you use the BufferedImage's Graphics or Graphics2D objects to draw into the BufferedImage. So while it seems that you're drawing in the Graphics, that's not what's happening; rather you're using the Graphics to draw on or into the image. Reading and studying the API is a skill and the more you use it, the better you'll be at it.

Also, whenever you use a Graphics or Graphics2D object and it is not one that has been passed into your method via the paintComponent or paint parameter, then you should make it a habit to call dispose on the Graphics or Graphics2D object after you are done using it so as not to unnecessarily accumulate system resources.

Luck
Oliver Stuttgart
Greenhorn

Joined: Jan 22, 2010
Posts: 13
Thanks again for your replies.

Agreed, that it's a skill to work with the API - I try to improve this. However, especially some of OO concepts cause headaches to me.

Cheers, Oliver
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can't get Graphics.setFont() to work