PrefWidth/Height calculations only works if stage is shown

Scott Palmer swpalmer at gmail.com
Tue Sep 24 17:40:12 PDT 2013


[replying back to the list]

Ah.. well the trick here is that I need to apply a scale transform when
taking the snapshot.  I want to get an image that is a particular size and
so I first need to know the bounds of the node so I can calculate the scale
value.   If I don't apply the transform while taking the snapshot the image
may be far too large and trigger an out-of-memory error.

So I need the CSS and layout to happen BEFORE I take the snapshot so that I
have the correct information to calculate the needed scale.

Here is the full code:
Parent p = n.getParent();
Scene s = new Scene(p);
s.getStyleSheets().add(knownGoodStyleSheetURL);
n.impl_processCSS(true);
n.layout();
Bounds newBounds = n.getLayoutBounds();
double maxDim = Math.max(newBounds.getWidth(), newBounds.getHeight());
double scale = desiredSize / maxDim;
SnapshotParameters snapParams = new SnapshotParameters();
snapParams.setFill(Color.TRANSPARENT);
snapParams.setTransform(new Scale(scale, scale));
WritableImage theImage = n.snapshot(snapParams,null);

I get the correct scale for the snapshot to make an image of the desired
size, but the CSS is not applied properly.

I don't think this should be so hard to do :-)

Scott



On Tue, Sep 24, 2013 at 7:56 PM, John Smith <John_Smith at symantec.com> wrote:

> **Ø  **I have a need to apply the CSS so when I 'snapshot' a node that
> isn't in my scene, it looks correct.****
>
> ** **
>
> A snapshot applies css and performs a layout pass automatically before it
> takes a snapshot.****
>
> You can create a new dummy Scene (just for the snapshot) containing your
> node (like Kevin does), then set the required stylesheets on the new Scene.
> ****
>
> I don’t understand what the rest of your code does or why you manually
> call impl_processCSS(true) or layout();****
>
> ** **
>
>     // create a dummy scene so layout and CSS will work
>     Scene dummyScene = new Scene(new Group(node));****
>
>     dummyScene.getStyleSheets().addAll(…);
>     node.shapshot(...);
>
> ****
>
> ** **
>
> *From:* Scott Palmer [mailto:swpalmer at gmail.com]
> *Sent:* Tuesday, September 24, 2013 4:35 PM
> *To:* John Smith
> *Cc:* Richard Bair (richard.bair at oracle.com); openjfx-dev at openjdk.java.netList
>
> *Subject:* Re: PrefWidth/Height calculations only works if stage is shown*
> ***
>
> ** **
>
> On Wed, Aug 28, 2013 at 3:18 PM, John Smith <John_Smith at symantec.com>
> wrote:****
>
> I hadn't seen the applyCSS conversation, but looks like it was contained
> deep in a node validation thread:
> http://mail.openjdk.java.net/pipermail/openjfx-dev/2013-July/008549.html
>
> Looks like the request is already covered in RT-21206 as pointed out by
> David.
> Looks like the feature will show up some time post-Java8, which is why
> there is nothing on Node now.
>
> For now, I stick with Kevin's node snapshot workaround:
>     // create a dummy scene so layout and CSS will work
>     new Scene(new Group(node));
>     node.shapshot(...)
>
> Thanks for the info!****
>
> ** **
>
> I have a need to apply the CSS so when I 'snapshot' a node that isn't in
> my scene, it looks correct.****
>
> I'm doing this:****
>
>             Parent p = n.getParent();  // The node I want tosnapshot isn't
> the root of the graph****
>
>             Scene s = KayakFXUtils.createDarkScene(p,1000,1000);****
>
>             n.impl_processCSS(true);****
>
>             n.layout();****
>
>  ****
>
> I get a reasonable size for the node, but the CSS isn't applied.****
>
> ** **
>
> I get a ton of warnings like this:****
>
> ** **
>
> WARNING: com.sun.javafx.css.StyleHelper calculateValue Could not resolve
> '-fx-text-background-color' while resolving lookups for '-fx-text-fill'
> from rule '*.label' in stylesheet
> jar:file:/C:/Program%20Files%20(x86)/Java/jdk1.7.0_40/jre/lib/jfxrt.jar!/com/sun/javafx/scene/control/skin/caspian/caspian.bss
> ****
>
> ** **
>
> Ideas?****
>
> ** **
>


More information about the openjfx-dev mailing list