Meaningless Drivel is fun!*
The moose likes Swing / AWT / SWT and the fly likes Drawing text along a line Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Drawing text along a line" Watch "Drawing text along a line" New topic
Author

Drawing text along a line

Veronique Kus
Ranch Hand

Joined: Jun 13, 2010
Posts: 41
Hi,
I am trying to implement drawing text along lines, or rather cubic curves. I found this page very useful ("Text along a path" section). However, this code draws text at the beginning of the line. What I need is to draw text at the end of the line. I tried modifying the code given on the website (TextStroke.java) and I came up with the below class (my code changes are enclosed in //MY CHANGES (...) //END OF MY CHANGES and most of the comments is my trying to understand this code):



I'm also posting two further classes which, together which the class above form an application which demonstrates what I am trying to achieve and my problem.




This sort of works but there are a couple of problems:
- one or more letters get missing when I reposition the curve
- sometimes the string disappears completely
- when I swap line ends (I move the end of the line to the left of its start), the string is not drawn correctly

I find all this very confusing and it took me ages to even figure out what I have done so far. It is the first time I'm working with the PathIterator and GlyphVector classes and so I am really struggling to understand the content of class which does the work of drawing the string. I would REALLY appreaciate some help with getting this to work correctly... anyone, please?

Also, I wanted to ask why in line 88 of the first class the nextAdvance variable, which seems to me to represent the horizontal advance along the baseline caused by the next charcter in the string, is calculated as:
nextAdvance = glyphVector.getGlyphMetrics( currentChar ).getAdvance() * 0.5f;
Specifically, if glyphVector.getGlyphMetrics( currentChar ).getAdvance() gives us the advance, why are we multiplying it by 0.5f??
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Veronique Kus wrote:Specifically, if glyphVector.getGlyphMetrics( currentChar ).getAdvance() gives us the advance, why are we multiplying it by 0.5f??

I'm no expert on the main question, but if I wanted to discover why in someone else's code a factor of 0.5F is applied, I would change that factor to different values (probably in a range of about 0.1 to 10) and see how it affected the visual representation.


luck, db
There are no new questions, but there may be new answers.
Veronique Kus
Ranch Hand

Joined: Jun 13, 2010
Posts: 41
I did that and I noticed no difference to be honest... really strange!
EDIT: Now you said that I looked again and turned out my program was using one copy of the class and I was changing a different copy... Let's see what happens when I fix this!
EDIT2: Same thing again... have no idea why this time.
Veronique Kus
Ranch Hand

Joined: Jun 13, 2010
Posts: 41
Anyone, pleeeease? This is driving me crazy :-/
Veronique Kus
Ranch Hand

Joined: Jun 13, 2010
Posts: 41
OK I made a very minor change to the code thanks to which the second letter of the word on the curve isn't drawn in the same place as the first letter, which was the case in the previous version. However, I still have serious problems with:
a) transforming the string when you invert the start&end point of the curve. The String is still drawn upside down when you drag the right end of the curve to be the left end.
b) the string still disappears randomly during repositioning of the curve.



Oh and the "rotate" parameter which I introduced to the constructor says whether the string should be transformed (but I don't know how) because now the line is going the other way round (as in a) above the code).

I'm really hoping for some help... I'm doing what I can but I am really struggling and this code is an important part of an important project wich I have to hand in very soon :-(
Veronique Kus
Ranch Hand

Joined: Jun 13, 2010
Posts: 41
OK, solved the string rotation problem! Now there's the most difficult thing left - the dissapearing string... it disappears/appears when I reposition the end points or the control points and I have no idea why.......

The newest version of the string-drawing class with one problem remaining:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Drawing text along a line
 
Similar Threads
java 2d
Wanted: How to use Strategy Pattern with Shapes?
Wanted: Java2D example of zooming and panning with scroll bars
Moving and resizing a rectangle around
Fundamentals stuff