-
1 Feb 2012 5:18 PM #1
How do i add Text or inner sprites in RectangleSprite
How do i add Text or inner sprites in RectangleSprite
Hi,
I would like to add "text" or Text Sprite in side a RectangleSprite rectangle. How do i perform this?
-
2 Feb 2012 1:48 AM #2
Aha, the kind of question I love, even more as I hadn't an opportunity to play much with the drawing API.
But your question is a bit vague. I suppose you won't place the text inside the rectangle, but above it. But what position the text will have? The most likely is centred, but there are other possibilities. Do you allow overflow? Do you want to adjust the rectangle to the size of the text? Etc.
-
2 Feb 2012 2:49 AM #3
My attempt:
The problem is that at construction time, the bounding box of text is not known. It is known only after it has been rendered on screen. So it is quite hard to center text in the rectangle, or do similar alignments.Code:DrawComponent draw = new DrawComponent(400, 400); RectangleSprite rect = new RectangleSprite(300, 100, 20, 20); rect.setFill(new Color("#FFC")); draw.addSprite(rect); TextSprite text = new TextSprite("I fit in a rectangle"); text.setFont("Verdana"); text.setFontSize(18); text.setTranslation(rect.getX() + 40, rect.getY() + 40); System.out.println(text.getBBox()); text.setFill(new Color("#88F")); draw.addSprite(text); layout.add(draw);
I am not sure of how to handle this problem, except by waiting for rendering and adding elements after.
I tried that when I had to add lines or areas on a chart, fitting between the axes: I can do it only on an event (eg. clicking on a button). I don't know if there is an event generated when rendering is finished...
-
2 Feb 2012 3:32 AM #4
Actually, the drawing API is quite simple, enough for charts and some animations, but it is not a full scenegraph, far from it. I don't know if we can change the z-order of sprites*, there is no possibility to nest the sprites (which is perhaps what you were looking for), etc.
That's not JavaFX...
* Actually, Sprite as a zIndex field. Beta 2 still has an inconsistency, with a getzIndex and a setZIndex...
-
7 Feb 2012 5:02 PM #5
If your component was already attached you could call redrawSurfaceForced to attach the surface and then add text and call text.redraw before getting the bound box. This is the limitation of using the SVG text bounding box calculations. The text element needs to be attached to the DOM.
In regards to zIndex that is a known issue and will be seen to. That is one of the rare cases where VML has built in functionality that SVG lacks. Also, thanks for pointing out that inconsistency. I just fixed it in SVN.
-
9 Feb 2012 6:55 PM #6
Alright z-index sorting is now fixed for SVG. This change is in SVN and will be in the next release.


Reply With Quote