Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to draw a line with text on it??  RSS feed

 
Michelle Wang
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear friends:
How to draw a line with text on it??
Can anybody give an example??
Thanks
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Override paintComponent(Graphics g)
Inside it you can use the drawLine and drawString methods which also accept the x,y co-ordinates so you can compute and define the location of the line and text.
 
Michelle Wang
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much, it works
 
Michelle Wang
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more question, How to change the thickness of line I draw here??
Thanks again
 
Rob Spoor
Sheriff
Posts: 21087
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think you can, but of course you can just draw two or more lines next to each other to get the thickness you need.
 
Campbell Ritchie
Marshal
Posts: 55672
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course you can change the thickness of the line.

You need to use a java.awt.Graphics2D object (the Graphics object passed to paintComponent() is actually Graphics2D, so you can simply cast it), then use a java.awt.BasicStroke object for drawing.

Find the 2D part of the Java tutorial, and go forward until you get to the bit about "stroking and filling." That shows how to draw a rounded rectangle with a dashed stroke; you can use the same technique for different thicknesses.

[edit]Minor spelling error. Also I forgot to say you can find "stroke and fill" under "working with geometry" on the link I gave you.[/edit]
[ October 12, 2007: Message edited by: Campbell Ritchie ]
 
Michelle Wang
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, it works
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!