JavaFX 2 -fx-effect breaks opacity - javafx-2

Has anyone else found that adding -fx-effect in a style prevents opacity working?
Here is a simple example
public class TestGUI extends Application {
#Override
public void start(final Stage primaryStage) {
Line line = LineBuilder.create()
.startX(150)
.startY(0)
.endX(150)
.endY(250)
.build();
Button btn = ButtonBuilder.create()
.text("Open countdown!")
// this breaks the opacity!
.style("-fx-effect: dropshadow(three-pass-box, grey, 5, 0.5, 2, 5);")
.opacity(0.6)
.build();
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("Button clicked");
}
});
StackPane root = new StackPane();
root.getChildren().addAll(line, btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Test Application");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Take out the style clause and you can see the line through the button.
Is this a bug or am I missing something.

The shadow is actually a shadow of the translucent node and translucent itself but because you are layering a translucent node on top of a tranlucent shadow, the overall result is still translucent, but much more opaque than if no shadow had been applied to the node. Similar to layering two 50 percent opaque nodes. The intersected area of the two layered nodes will be 75 percent opaque.
In your sample, you set the opacity to 0.6, so the combined opacity of the node + shadow is 0.6 + 0.4 * 0.6 = 0.84. Plus the shadow is a darker color than the effected node to begin with. This makes it difficult to see the line behind the effected node - but you can still just see it because the node + it's effect is not fully opaque. To show what is going on more clearly, I set the opacity of your sample to 0.2, making the combined opacity 0.36. You can see the result in the screenshot below where the line behind the effected node is still clearly visible.
Generally, shadows and opaque nodes visually don't mix and look all that good together

Related

When to use translate and when relocate - What is the difference between translate and layout coordinates?

When to use translate and when relocate in order to move a node? In the end of the day it seems they do the same thing (visually); move the node; the first by doing a translation on the origin (the x, y stays the same), the second by changing the x, y coords.
So suppose i want to move a node in a specific point in the screen.. should i use node.relocate(x,y) or node.setTranslateX(x), node.setTranslateY(y)?
To demostrate what I mean I have made a sample program you can play with:
A rectangle on the screen, whose position is determined by 4 sliders (2 of them controlling the layout x, y the other two controlling the translate x, y).
/* imports are missing */
public class TransReloc extends Application{
#Override
public void start(Stage primaryStage) throws Exception {
Group root = new Group();
Rectangle rect = new Rectangle(100, 50, Color.BLUE);
root.getChildren().add(rect);
VBox controlGroup = new VBox();
Slider relocX = new Slider(-100, 100, 0 );
Slider relocY = new Slider(-100, 100, 0 );
Slider transX = new Slider(-100, 100, 0 );
Slider transY = new Slider(-100, 100, 0 );
rect.layoutXProperty().bind(relocX.valueProperty());
rect.layoutYProperty().bind(relocY.valueProperty());
rect.translateXProperty().bind(transX.valueProperty());
rect.translateYProperty().bind(transY.valueProperty());
controlGroup.getChildren().addAll(relocX, relocY, transX, transY);
root.getChildren().add(controlGroup);
controlGroup.relocate(0, 300);
Scene scene = new Scene(root, 300, 400, Color.ALICEBLUE);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
layout coordinates are used by Layout Managers like StackPane or VBox to control their children location. Group (and Pane) leaves children layout to developer thus there is no difference from translate functionality.
So generally you should only change translate coordinates for fine location tuning and leave layout to layout managers (actually you can't change layoutX, layoutY for nodes inside non Group/Pane layout managers)
As an example try to run next code and resize window to see how StackPane recalculates layoutBounds
public void start(Stage primaryStage) throws Exception {
StackPane root = new StackPane();
Rectangle rect = new Rectangle(100, 50, Color.BLUE);
root.getChildren().add(rect);
Scene scene = new Scene(root, 300, 300, Color.ALICEBLUE);
rect.layoutXProperty().addListener( (e) -> {
System.out.println(rect.getLayoutX() + ":" + rect.getLayoutY());
});
primaryStage.setScene(scene);
primaryStage.show();
}
Another difference is that when you call Node.getBoundsInLocal(), will calculate the LayoutX, LayoutY and all the applied Effects. But, Node.getBoundsInParent() will get calculated with LayoutX, LayoutY, all applied Effects plus all transformations (rotation, translation and scaling). So you can use LayoutX/Y properties as a main position and use translateX/Y as a second or an alternative way to move the node. And the other difference is discussed above, I mean not to copy from Sergey Grinev.

JavaFX custom layout of nodes [duplicate]

This question already has answers here:
Get the height of a node in JavaFX (generate a layout pass)
(2 answers)
Closed 8 years ago.
I am developing an application for which it is necessary to layout nodes besides each other (or on top of each other etc.). However, this layout is only an initial placement and the user is able to move these nodes arbitrarily. How is this done in the correct way in JavaFX? I will explain my problem with a simplified example:
Assume I have 2 rectangles and want to place rect2 to the right of rect1.
// create first rectangle at position x= 5, y=5
rect1 = rectangle(5,5);
// create second rectangle to the right of rect1
rect2 = rectangle(5+rect1.width(), 5);
In this scenario JavaFX will not yet have determined the width of rect1 and it will be zero. Intuitively, I would perform a call that lets JavaFX draw rect1 and thus determine its width and afterwards add rect2. See the following example:
// create first rectangle at position x= 5, y=5
rect1 = rectangle(5,5);
// let JavaFX draw rect1 (width will be calculated and set)
draw();
// create second rectangle to the right of rect1
rect2 = rectangle(5+rect1.width(), 5);
Unfortunately I haven't found a method that does what I want. My current workaround makes use of Platform.runLater() but this does not work properly all the time. If my understanding of bindings is correct, bindings are also not suitable for this problem. I only want to initially layout the nodes, so I would have to remove the binding after the initial layout (or else rect2 would move if rect1 is moved).
Thanks in advance for any help.
EDIT: Here is a minimal working example. The width of the button is 0. I tried calling root.layout() to force a layout pass etc. but it does not seem to work.
public class Test extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
Button btn = new Button();
StackPane root = new StackPane();
Scene scene = new Scene(root, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
btn.setText("Say 'Hello World'");
root.getChildren().add(btn);
// prints out 0
System.out.println(btn.getWidth());
}
}
Given the example from above, if I set the scene of the stage to null and then reset it, the button width will be set correctly when calling System.out.println(). It seems that this forces a layout pass on the whole stage? However, this just seems to be another workaround, in particular I have performance concerns.
#Override
public void start(Stage primaryStage) {
Button btn = new Button();
StackPane root = new StackPane();
Scene scene = new Scene(root, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
btn.setText("Say 'Hello World'");
root.getChildren().add(btn);
// reset scene
primaryStage.setScene(null);
primaryStage.setScene(scene);
// prints out 107.0
System.out.println(btn.getWidth());
}

A simple JavaFX 2.x animation task

I have done animations in FlashProfessional (CS 6), which sucks by the way (crashes constantly without saving last modifications, produces enormous files, works inconsistently and so on). I have hard time to figure out how the following simple task can be done in JavaFX 2.x (probably because my background is in Flash): a rectangle that exists in a constant location from t=0 to t=100, and after that it is deleted from the Scene.
In Flash, I could create a keyframe at t=0 in which I draw a rectangle. Then I create second keyframe at t=100 in which the rectangle is deleted. That simple.
In JavaFX, why I can’t just write timeline.getKeyFrames().addAll(new KeyFrame(new Duration(100), new Rectangle(10, 10, 25, 25))); or like.
Please help me and provide the code. I’m lost in these KeyValues and Java properties, why I need those anyway…
To achieve that task you can just remove rectangle after 100 ms. JavaFX unlike Flash is not built about keyframes -- they are optional functionality and used only if you need real animation, e.g. scaling of an object. See next tutorial for more info: http://docs.oracle.com/javafx/2/animations/jfxpub-animations.htm
And demonstrating code:
public void start(Stage primaryStage) {
final Rectangle rect1 = new Rectangle(10, 70, 50, 50);
final Rectangle rect2 = new Rectangle(10, 150, 50, 50);
Button btn = new Button("Play");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent t) {
// this "timeline" just call a handler after 500 ms which hides rectangle
TimelineBuilder.create().keyFrames(new KeyFrame(Duration.millis(500), new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent t) {
rect1.setVisible(false);
}
})).build().play();
// this timeline hides rectangle 2 with animation
// changing scaleXProperty() from 1 (default) to 0
TimelineBuilder.create().keyFrames(
new KeyFrame(
Duration.millis(500),
new KeyValue(rect2.scaleXProperty(), 0))
).build().play();
}
});
Pane root = new Pane();
root.getChildren().addAll(rect1, rect2, btn);
Scene scene = new Scene(root, 300, 300);
primaryStage.setScene(scene);
primaryStage.show();
}

Scaling in JavaFX and ScrollPanes

I've been trying to work with the scaling transform in JavaFX, but haven't quite been able to wrap my head around it. Basically, I have a Pane containing a complex graph and would like to be able to rescale it. The scaling part itself works fine, however, the enclosing scroll pane will not adapt to the graph.
For simplicity's sake, i'll post a short example in which my graph is replaced by a label:
public class TestApp extends Application {
#Override public void start(final Stage stage) throws Exception {
final Label label = new Label("Hello World");
label.getTransforms().setAll(new Scale(0.5, 0.5));
label.setStyle("-fx-background-color:blue");
label.setFont(new Font(200));
final ScrollPane scrollPane = new ScrollPane();
scrollPane.setContent(label);
stage.setScene(new Scene(scrollPane));
stage.setWidth(200);
stage.setHeight(100);
stage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
The label will scale correctly, but the enclosing scroll pane's bars will still accomodate a component of the original size.
I've tried so far:
Playing around with the labels min and pref size
wrapping the label inside a Group (no scrollbars will appear whatsoever)
scaling the enclosing Group rather than the label
What am I missing? What can I do to make the ScrollPane adapt to the content view?
Thanks for your help.
According to the ScrollPane document you might try to wrap a Pane in a Group so the ScrollPane is scroll by visual bound not the actual layout bound.
ScrollPane layout calculations are based on the layoutBounds rather than the
boundsInParent (visual bounds) of the scroll node. If an application wants the
scrolling to be based on the visual bounds of the node (for scaled content etc.),
they need to wrap the scroll node in a Group.
I implemented scaling in a ScrollPane for Graphs and other nodes in
this example of scrollpane viewports, transforms and layout bounds in JavaFX.
The code was implemented when I was first learning JavaFX, so certainly the code could be cleaner and perhaps there are simpler ways to accomplish this (e.g. using a Group as the container for the scaled node as suggested in the ScrollPane documentation).
One key to getting the solution I wanted (ScrollBars only appearing when you are zoomed in and the node is larger than the visible viewport), was this code:
// create a container for the viewable node.
final StackPane nodeContainer = new StackPane();
nodeContainer.getChildren().add(node);
// place the container in the scrollpane and adjust the pane's viewports as required.
final ScrollPane scrollPane = new ScrollPane();
scrollPane.setContent(nodeContainer);
scrollPane.viewportBoundsProperty().addListener(
new ChangeListener<Bounds>() {
#Override public void changed(ObservableValue<? extends Bounds> observableValue, Bounds oldBounds, Bounds newBounds) {
nodeContainer.setPrefSize(
Math.max(node.getBoundsInParent().getMaxX(), newBounds.getWidth()),
Math.max(node.getBoundsInParent().getMaxY(), newBounds.getHeight())
);
}
});
...
// adjust the view layout based on the node scalefactor.
final ToggleButton scale = new ToggleButton("Scale");
scale.setOnAction(new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent actionEvent) {
if (scale.isSelected()) {
node.setScaleX(3); node.setScaleY(3);
} else {
node.setScaleX(1); node.setScaleY(1);
}
// runlater as we want to size the container after a layout pass has been performed on the scaled node.
Platform.runLater(new Runnable() {
#Override public void run() {
nodeContainer.setPrefSize(
Math.max(nodeContainer.getBoundsInParent().getMaxX(), scrollPane.getViewportBounds().getWidth()),
Math.max(nodeContainer.getBoundsInParent().getMaxY(), scrollPane.getViewportBounds().getHeight())
);
}
});
}
});

How to align icon and text inside the big button?

I have quite a big button (minWidth and minHeight were explicitly set to big numbers), and inside that big button there is relatively small icon and some text. Icon and text do not consume all available space, and end up being placed in the center of the button.
I want to put icon and text to the left side of the button. But it seems that I do not understand what all those alignments mean, since setting alignment to BASELINE_LEFT or setting textAlignment to LEFT didn't change anything.
How can I fix it?
Property textAlignment controls alignment for multiline text so it wouldn't help you.
But both
btn.setStyle("-fx-alignment: LEFT;");
or
btn.setAlignment(Pos.BASELINE_LEFT);
should work for you. See example below.
public class ILoveBigButtonsAndICannotLie extends Application {
#Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("Button");
btn.setGraphic(new Rectangle(10,10, Color.RED));
btn.setMinHeight(200);
btn.setMinWidth(250);
//btn.setStyle("-fx-alignment: LEFT;");
btn.setAlignment(Pos.BASELINE_LEFT);
StackPane root = new StackPane();
root.getChildren().add(btn);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
public static void main(String[] args) { launch(); }
}

Resources