Showing a PNG file with transparent background using javafx - javafx

Am actually working on a splash Screen unsing javaFX, everythink works fine but I want to show a png image with a transparent background on the splash screen, but am unable to do it, can someone please tell me if it's possible to do it from JavaFX Scene Builder or not ?

I'm in a bit of a rush, but below is a quick example to show you how it can be done by setting the StageStyle to Transparent and the scene fill to the color "transparent".
#Override
public void start(Stage aStage) throws Exception {
Pane root = new Pane();
ImageView img = new ImageView();
img.setImage(new Image(getClass().getResource("pathToYourPngLocatedInYourResourcesFolder.png").toExternalForm()));
root.getChildren().add(img);
Scene scene = new Scene(root, 500, 500);
scene.setFill(Color.TRANSPARENT);
aStage.initStyle(StageStyle.TRANSPARENT);
aStage.setScene(scene);
aStage.show();
}
Let me know how it works out :)

Related

Setting an imageview as an icon on a button

I was trying to write a JavaFX application; I wanted to put images in my icons and make them transparent, so that you would click on the ImageView without seeing the button.
Here i have the scenebuilder were i got the imageview and in the button in style i said: -fx-background-color transparent
Then we have here the preview of what is going to show
And lastly, this is the application when i run it on netbeans
i wanted to put images in my icons and make them transparent so you clicked on the imageview and you didn't saw the button.
Just call btn.setBackground(Background.EMPTY); to remove the default background of the button.
eg:
#Override
public void start(Stage primaryStage) {
ImageView iv = new ImageView("https://image.flaticon.com/icons/png/512/33/33949.png");
iv.setPreserveRatio(true);
iv.setFitWidth(60);
Button btn = new Button("", iv);
btn.setBackground(Background.EMPTY);
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
Produces:

How to make non-modal stage appear always on top of JavaFx fullscreen stage

I have
a primary stage which the user can configure to be in fullscreen mode
secondary stages (tool windows) which the user can open. These windows should be always on top the the primary stage (regardless of whether its in fullscreen mode or not).
The latter does not work, even if I use setAlwaysOnTop(true) for the secondary stages they will disappear behind the primary stage once the user clicks on the primary stage.
This only happens when the primary stage is in full screen mode, everything works fine if the primary stage is not in fullscreen mode.
How can I enable this concept of tools windows in front of a fullscreen stage? Example code:
public class Test extends Application {
#Override
public void start(Stage stage) {
VBox vbox = new VBox();
Scene scene = new Scene(vbox);
stage.setScene(scene);
Button button1 = new Button("New Tool Window");
button1.setOnAction((e) -> {
Stage toolStage = new Stage();
Scene toolScene = new Scene(new Label("Am I on top?"), 300, 250);
toolStage.setScene(toolScene);
toolStage.initOwner(stage);
toolStage.setAlwaysOnTop(true);
toolStage.show();
});
Button button2 = new Button("Close");
button2.setOnAction((e) -> System.exit(0));
vbox.getChildren().addAll(button1, button2);
stage.show();
stage.setFullScreen(true);
}
public static void main(String[] args) {
launch(args);
}
}
Update 8/20/2016: Confirmed as a bug: JDK-8164210
A way to bypass this limitation is to:
Deactivate fullscreen mode
Create a keyCombination for psuedo fullscreen
Set the stage style undecorated and not resizable
Se the screen to the size of the user screen and position it at 0,0.
It is easy to create your own border for minimizing and closing the program as shown here:
JavaFX Stage.setMaximized only works once on Mac OSX (10.9.5)
And here:
JavaFX 8 Taskbar Icon Listener
you need to set initmodality after set initowner
toolStage.initOwner(stage);
toolStage.initModality(Modality.APPLICATION_MODAL);

JavaFX is there any color gradient editor control?

For an editor that can map a Noise Function (-1 to 1 values) to Colors I need some Control that lets me define a Color Gradient, so something like
Value
- 0 is black
- 0.3 is yellow
- 0.8 is red
- 1 is white
so the whole gradient goes from black to white and thats editable,
is there anything like that built into JavaFX or do I have to write my own Control?
Basically smth like this:
Thanks in advance
Actually, Scene Builder has a powerful gradient editor that allows inserting multiple stops:
The control is called PaintPicker, and it is part of the Scene Builder Kit that you can download from here.
Once you have the jar, you can use the component.
This is a short snippet to show how to easily add it to your scene:
#Override
public void start(Stage primaryStage) {
VBox root = new VBox();
root.setAlignment(Pos.CENTER);
root.setPadding(new Insets(10));
PaintPickerController controller;
final FXMLLoader loader = new FXMLLoader();
loader.setLocation(PaintPicker.class.getResource("PaintPicker.fxml"));
try {
final VBox picker = loader.load();
controller = loader.getController();
controller.paintProperty().addListener((obs, ov, nv) -> System.out.println("Paint: " + nv));
root.getChildren().add(picker);
} catch (IOException ex) {
throw new IllegalStateException(ex);
}
Scene scene = new Scene(root, 320, 600);
primaryStage.setTitle("SceneBuilder PaintPicker");
primaryStage.setScene(scene);
primaryStage.show();
}
With the listener, you will get immediately the gradient:
Paint: linear-gradient(from 60.096% 38.461% to 47.115% 45.192%,
reflect, 0xda7777ff 0.0%, 0x226621ff 28.667%, 0xf2ff1cff 49.333%,
0xff1c5fff 73.0%, 0xffffffff 100.0%)
I think you are looking for a ColorPicker.
More information on how to use it is on JavaFX tutorials page.

How to get better ImageView resize quality?

Images scaled in JavaFX using setFitWidth()/setFitHeight() methods have significantly inferior quality compared to images prescaled in graphics editor, in my case Photoshop.
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Image Resize Test");
StackPane root = new StackPane();
root.setStyle("-fx-background-color: gray;");
ImageView imageView = new ImageView("save.png");
//ImageView imageView = new ImageView("save_small.png");
imageView.setPreserveRatio(true);
imageView.setFitHeight(15);
root.getChildren().add(imageView);
primaryStage.setScene(new Scene(root, 600, 600));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Application screenshots:
.
Image on the left is scaled in JavaFX, and the image on the right is prescaled in Photoshop. The first image has white border clearly visible only on its right side, while the second image (resized in Photoshop) has faint border visible on all sides, which better represents the original image that has white border all around it.
I tried setting imageView.setSmooth(true) and SceneAntialiasing.BALANCED, but results are the same.
These are PNG images used in this example:
Scaled (15x15) PNG image (resized in Photoshop):
Original (500x509) PNG image:

Make a BorderPane region go over another

I've been trying for days and days now to get a BorderPane region go over another region...
The problem is as follow: My app is set in a BorderPane root, With:
A header in its TOP region
A menu in its LEFT region
The content, depending on the page, in it's CENTER
And an optional panel on its RIGHT region
That right region is the problem. It should appear/disappear when clicking on a "notification button" that is in the TOP region. So far so good. The thing is that the app doesn't use the RIGHT region, so I'm trying to make the RIGHT region that contains an AnchorPane go over the CENTER region. The normal state of the app is without the RIGHT region and I don't want to resize the whole app when opening the noitifications. Tried several things, such as:
When clicking the notification button, send the CENTER part toBack() and set the RIGHT width to the 300 wanted pixels
Sending the RIGHT region toFront()
Sending the whole BorderPane toFront()
None of them work, as they all either not show, or resize the center part which I don't want. I'd like the RIGHT to float above the CENTER region when the notification menu is showing.... Is there any way to do that? Or maybe another idea to trigger a container that would show above the CENTER part? Of course, I go design the panel in every CENTER pane and make it visible or not, but my app is about 15 different center windows so it would be really bad in terms of modifications...
I think you should not be trying to make the borderpane do this for you or you will end up with behavior you do not want like the center NOT resizing when the application is resized while the panel is visible.
Remember that JavaFX is really 3D. How about you try to wrap the BorderPane inside of an AnchorPane, GridPane or ScrollPane (whichever makes sense) instead of trying to get the right insert to do your thing. e.g. add an ScrollPane (your Slider) to the containing AnchorPane and bring that to the front and anchor it's top, right and bottom.
This should give you a right-aligned ScrollPane on top of your borderpane.
Then of course if you want it to be fancy with an animated slide you can try this out : https://gist.github.com/jewelsea/1437374
or this:
http://blog.physalix.com/javafx2-borderpane-which-slides-in-and-out-on-command/
Here is a very rough example to show the idea:
public class JavaFXApplication2 extends Application {
ScrollPane slider;
AnchorPane root;
#Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("Slide in");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
root.getChildren().add(slider);
AnchorPane.setRightAnchor(slider, 0.);
AnchorPane.setTopAnchor(slider, 0.);
AnchorPane.setBottomAnchor(slider, 0.);
slider.toFront();
}
});
Label l = new Label();
l.setText("Test Label to Show inside content");
Label l2 = new Label();
l2.setText("Peek-a-Boo");
slider = new ScrollPane();
slider.setStyle("-fx-border-color: orangered;");
slider.setContent(l2);
root = new AnchorPane();
root.getChildren().add(btn);
root.getChildren().add(l);
AnchorPane.setRightAnchor(l, 0.);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}

Resources