• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Can't get Graphics.setFont() to work

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My guess would be that each call to createGraphics throws away the previous Graphics object; try reusing it.
 
Oliver Stuttgart
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Marshal
Posts: 78679
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this question would sit better on our GUIs forum. Moving.
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Oliver Stuttgart
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 28003
94
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 28003
94
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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've been selected to go to the moon! All thanks to this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic