• Post Reply Bookmark Topic Watch Topic
  • New Topic

Draw steing over color

 
Elad Grbr
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello.
I started learning the basics of graphics in Java. I tried the code in the link: http://www.tutorialspoint.com/javaexamples/gui_piechart.htm Now, I can't understand how to draw a string above the color. I want each text to be in the exact middle of each slice. Thanks in advance!
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35716
412
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Elad,
Welcome to CodeRanch!

How far did you get? Can you figure out how to draw a String instead of the existing shape?
 
Elad Grbr
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
Umm I know it should be g.drawString(..)
But I can't determine the position of the string... I don't knkw how to :/
 
Elad Grbr
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Elad,
Welcome to CodeRanch!

How far did you get? Can you figure out how to draw a String instead of the existing shape?

Sorry for double, I don't know how to delete my comment above..

Hello
Umm I know it should be g.drawString(..)
But I can't determine the position of the string... I don't knkw how to :/
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your paintComponent() method you can use code like:



Now that you have the FontMetrics you can use methods of the class to get the widthand height of the String.

Now that you know the width and height you can determine how to center the String in your painting area.

 
Campbell Ritchie
Marshal
Posts: 52580
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

You can find out more about sizes of text in the Java™ Tutorials. I am 99% sure it will explain more about font metrics.
 
Elad Grbr
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:In your paintComponent() method you can use code like:



Now that you have the FontMetrics you can use methods of the class to get the widthand height of the String.

Now that you know the width and height you can determine how to center the String in your painting area.



Thank you for your help. Could you please explain what should I do with this variable?
Thanks alot!
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your question was:

But I can't determine the position of the string...

So you need to do some basic math.

First you get the width of the panel:



Then you need to get the width of the string your want to paint:



Then to center the text horizontally you would do:



Repeat for the y location.
 
Elad Grbr
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:Your question was:

But I can't determine the position of the string...

So you need to do some basic math.

First you get the width of the panel:



Then you need to get the width of the string your want to paint:



Then to center the text horizontally you would do:



Repeat for the y location.


Okay, thanks for the explanation; Will try tomorrow.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!