I use the snapshot() method on the Shape object in order to convert it into ImageView and nest it into Label. The problem is, that when I take a snapshot of the Shape object it is closed into square field with white background. Is there a way to make it transparent? I use the code below in order to convert given Shape into an ImageView object:
WritableImage snapshot = Shape.snapshot(new SnapshotParameters(), null);
ImageView imageView = new ImageView(snapshot);
Label label = new Label();
label.setGraphic(imageView);
Pane.getChildren().add(label);
Here you go
SnapshotParameters parameters = new SnapshotParameters();
parameters.setFill(Color.TRANSPARENT);
WritableImage snapshot = shape.snapshot(parameters, null);
...
Related
I'm writing a chess app and I've been able to resize my pieces to match the square size, that itself depends on screen size.
Problem is, it relies on XML. And I need to be able to do it dynamically (adding or removing pieces on the screen after each move)
This is working :
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(sq_size, sq_size);
ImageView klt60 = (ImageView)findViewById(R.id.klt60);
klt60.setImageResource(R.drawable.klt60); // setting the image in the layout
klt60.setLayoutParams(layoutParams); // allows resizing
But this is not : if I declare my imageView without using XML, it just won't resize, and also it won't move when I play a move with that piece.
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(sq_size, sq_size);
ImageView klt60 = new ImageView(this); // Create a new image (several pawns to display, for example)
klt60.setImageResource(R.drawable.klt60); // setting the image in the layout
klt60.setLayoutParams(layoutParams); // allows resizing
My view just extends extends AppCompatActivity. Thanks for help
I am trying to create a textfield that has an image part within itself, similar to this.
imageView.fitHeightProperty().bind(Bindings.createDoubleBinding(
() -> textField.getHeight() -
textField.getPadding().getTop() -
textField.getPadding().getBottom(),
textField.heightProperty(), textField.paddingProperty()));
imageView.fitWidthProperty().bind(imageView.fitHeightProperty());
textField.paddingProperty().bind(Bindings.createObjectBinding(
() -> new Insets(textField.getPadding().getTop(),
textField.getPadding().getRight(),
textField.getPadding().getBottom(),
textField.getPadding().getRight() * 2 + imageView.getFitWidth()),
imageView.fitWidthProperty(), textField.paddingProperty()));
My current approach is to use a StackPane to hold the TextField, then also add an ImageView as the StackPane's child. The ImageView needs to know how resize itself, so I have bound its fitHeight to the TextField's height, with consideration of the TextField's padding.
The ImageView's fitWidth is then bound to its own fitHeight.
Lastly, I need to make my TextField's text offset to the right (because the image is blocking it), so I once again did another binding that is dependent on the ImageView's fitWidth.
This ends up with circular dependency, which causes the stack to overflow. Is there any other way to do it without hard coding the TextField's left padding?
Here is a little exemple, i've used (StackPane/TextField/ImageView) but instead of the Image i advise you to draw your own shape with SVG to have the full control on it (MouseHover/Focused...), i've also used a CSS to achieve this :
Java :
private StackPane sp = new StackPane();
private TextField tf = new TextField();
private ImageView iv;
//Init StackPane
sp.getStylesheets().add(getClass().getResource("style.css").toExternalForm());
sp.getStyleClass().add("identifiant");
sp.setPrefSize(200, 40);
sp.setLayoutX(100);
sp.setLayoutY(100);
//Init ImageView
iv = new ImageView(getClass().getResource("Img.png").toString());
StackPane.setAlignment(iv, Pos.CENTER_LEFT);
StackPane.setMargin(iv, new Insets(0, 0, 0, 10));
//Init TextField
StackPane.setAlignment(tf, Pos.CENTER_LEFT);
StackPane.setMargin(tf, new Insets(2, 5, 0, 30));
//Add all content
sp.getChildren().addAll(iv,tf);
Css :
.identifiant{
-fx-background-color:#45444a;
-fx-effect:innershadow(three-pass-box , rgba(0,0,0) , 6, 0.0 , 0 , 0);
-fx-background-radius:8px;
}
.text-field{
-fx-background-insets:0;
-fx-background-color:#45444a;
-fx-text-fill:white;
}
Hope this will help you !
Attempting to use a JavaFX StackPane's snapshot method to capture a "Background" element. I'm specifically interested in a node's background (as the name suggests).
If I setup my scene with a StackPane, and the background literally being an ImageView underneath what I'm trying to display, then everything works fine. But if I use the more intuitive setBackground function of a node, and try to capture a snapshot, it (the background) doesn't show up.
For example this works as expected, if you do getBackgroundNode().snapshot(new SnapshotParameters(), null):
private Node getBackgroundNode() {
ImageView background = new ImageView(new Image("https://www.yamaha-motor.ca/images/pages/products/units/MC/action/2016_FZ_07_2_l.jpg"));
Rectangle2D viewport = new Rectangle2D(0, 0, 1080, 720);
background.setViewport(viewport);
return background;
}
However, the below method doesn't show me the background when I call the snapshot method.
private Node getBackgroundNode() {
Image background = new Image("https://www.yamaha-motor.ca/images/pages/products/units/mc/action/2016_fz_07_2_l.jpg");
StackPane stack = new StackPane();
stack.setBackground(new Background(new BackgroundImage(background, null, null, null, null)));
stack.setPrefSize(1080, 720);
return stack;
}
Is this simply a feature of the snapshot method? I took a look at SnapshotParameters and there aren't any obvious settings that would allow me to capture the background.
The size of a StackPane is usually not set until it has been layouted. Therefore you create a snapshot of a Node of size (0, 0). A properly layouted Node includes the background. You could set the size of the StackPane by calling resize before taking the snapshot:
Node backgroundNode = getBackgroundNode();
backgroundNode.resize(1080, 720);
backgroundNode.snapshot(new SnapshotParameters(), null);
I'm using JavaFX2.2. Is it actually possible to get the ImageView used as a parameter in the constructor for an UI-control such as Button or Label after the UI-control has been created?
I want to change the image of a control. I could probably also do it with CSS, but I'm not sure of how to use jar-resources there.
Example:
Label label = new Label("", new ImageView(new Image(getClass().getResourceAsStream("test.png"))));
label.getImageView().setImage(...); // there is no such method getImageView()
((ImageView) label.getGraphic()).setImage(...)
I need a button with foreground transparent image and background color. So have use this code. The background color is going outside of the image. I need the button with the same size of the image.
Depending on the user interaction i have to change the foreground image and background color. I want add the image and background color separately so that i can change one of them at minimum cost. I have to use a lot of button in this UI so it will be done in java code.
layout = new TableLayout(this);
layout.setLayoutParams(new TableLayout.LayoutParams(8,7));
TableRow row2 = new TableRow(this);
buttonPlayer1 = new ImageButton(this);
buttonPlayer1.setImageDrawable(getResources().getDrawable(R.drawable.blankc4));
buttonPlayer1.setBackgroundColor(Color.GREEN);
row2.addView(buttonPlayer1);
layout.addView(row2);
If your only problem is that button background color is going outsize image and you need button size to be same as image size then get image Height and Width using getHeight() & getWidth() methods on image object and use these values to make the Button size same as Image size using setHeight() & setWidth() methods respectively on button object.
Sample Code with LinearLayout:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout btnLO = new LinearLayout(this);
btnLO.setOrientation(LinearLayout.VERTICAL);
ImageButton i1 = new ImageButton(this);
i1.setBackgroundColor(color.background_dark);
i1.setImageDrawable(getResources().getDrawable(R.drawable.rana));
btnLO.addView(i1, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
// btnLO.setGravity(Gravity.LEFT | Gravity.CENTER_HORIZONTAL);
this.addContentView(btnLO, new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
}