I'm writing simple blackjack app using JavaFX. I have a problem adding card image to HBox, when Player gets next card. I was trying to write this part of code in "get Card" button controller, but it didn't work(NullPointerException):
#FXML
private void getCard() {
playerCard = table.getNextCard();
player.setCards(playerCard);
Image img = new Image(TableViewController.class.getResourceAsStream(cardColor(playerCard)));
ImageView imgView = new ImageView(img);
showPlayerCards.getChildren().add(imgView);
}
cardColor() change name of the card to name of the file.
I guess, that adding image must be in "initialize" method, but I don't know, how to do this for HBox..
Related
I am currently using Scene Builder and to design my FXML UI in Java. However when loading the scene, I would like to loop through a user's devices and display them as buttons inside a scroll pane:
public DeviceSelectorController() {
for (Device device : OrderBird.getInstance().getProfileManager().getActiveProfile().getDevices()) {
Label label = new Label(device.getName());
Button button = new Button(device.getName());
listView.getItems().add(device.getName());
}
}
I cannot do this through Scene Builder as far as I know because I cannot access the cached HashMap.
I am new in java.
I am trying to build a three bead game.
I am having problems to place 6 movable objects inside a jpg file.
I have tried using JPanel but the objects doesn't show.
Not sure if I understood you correctly, but I suppose you want to display background (a JPG image) and then programatically move three beads on it. If that's the case, there's Simple tutorial on how to work with images in JavaFX. To just display image, you need to do the following:
public void start(Stage stage) throws FileNotFoundException {
//Creating an image
Image image = new Image(new FileInputStream("file path"));
//Setting the image view 1
ImageView imageView1 = new ImageView(image);
//Creating a Group object
Group root = new Group(imageView1);
//Creating a scene object
Scene scene = new Scene(root, 600, 400);
//Adding scene to the stage
stage.setScene(scene);
//Displaying the contents of the stage
stage.show();
}
To move beads, you need to keep their state in your application and re-draw it periodically. The tutorial I've mentioned describes basics of pixel manipulation so you may want to check that too.
I'm using JavaFX for school and I need to display a picture on a button when I click on it. My problem is that the image is bigger than the button and then everything is horrible. I have seen multiples post on how to fit a image to a button and I came with that code
Image imageOk = new Image(getClass().getResourceAsStream("/TP2/ressources/flag.png"));
ImageView img = new ImageView(imageOk);
img.setFitWidth(100);
img.setFitHeight(100);
btn.setGraphic(img);
When I create the button I use setPrefSize(50, 50);
You can use the fitWidthProperty and fitHeightProperty of the ImageView and bind them to the widthProperty and heightProperty of the button:
img.fitWidthProperty().bind(btn.widthProperty());
img.fitHeightProperty().bind(btn.heightProperty());
This will cause the ImageView to have the same size as the button every time.
This can cause the image to be stretched. To prevent this you can use setPreserveRatio on the image:
img.setPreserveRatio(true);
Here is the complete example code:
Button btn = new Button();
btn.setPrefSize(50, 50);
Image imageOk = new Image(getClass().getResourceAsStream("yes.png"));
ImageView img = new ImageView(imageOk);
img.setPreserveRatio(true);
img.fitWidthProperty().bind(btn.widthProperty());
img.fitHeightProperty().bind(btn.heightProperty());
btn.setGraphic(img);
There is plenty of online resource on how to add a graphic to a JavaFX button.
However I would like to know if there is a way to add a second (or any number, but more than 2 images probably woudn't make much sense in most situations) image.
My use case : i have a button with a square icon on the left, followed by a text label. The image is a representation of some real-life concept that button is linked with (could e.g. a car or a person). I would like to add a small icon to the right of some buttons, a "right chevron" to indicate the nature of the interaction.
I was thinking maybe to use a HBox with the full width as the graphic node of the button, and add the 2 images to it, but I don't think it is possible to put the text on top of the graphic node.
Any idea ?
Create your own custom node with icons and text and set it as graphic. Of course you don't show the button text because it's already in your custom graphic node.
Here's a simple example. You need to provide the files icon1.png and icon2.png.
public class ButtonWithMultipleIcons extends Application {
#Override
public void start(Stage primaryStage) {
try {
Group group = new Group();
Button button = new Button();
button.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
HBox hBox = new HBox();
ImageView icon1 = new ImageView( getClass().getResource( "icon1.png").toExternalForm());
ImageView icon2 = new ImageView( getClass().getResource( "icon2.png").toExternalForm());
Label label = new Label("Text");
//make the button grow if you want the right icon to always be on the right of the button :
label.setMaxWidth(Long.MAX_VALUE);
HBox.setHgrow(label, Priority.ALWAYS);
hBox.getChildren().addAll( icon1, label, icon2);
button.setGraphic(hBox);
group.getChildren().add( button);
Scene scene = new Scene(group,400,400);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
You can try using html formatting in your button and adding an image with the tag before and after your text.
You can find more information about it in here:http://docs.oracle.com/javase/tutorial/uiswing/components/html.html
I had the same thing to get along with. You describing the solution with using a HBox. I think this would also be the best way.
Do the following:
Create a new CustomButton with your own specific layout maybe using a HBox and extending Button class from JavaFX. Style it your way you need it and you can add as many images you want to have in your custom button. Maybe it can look like this:
public class CustomButton extends Button
{
private HBox hbox;
private ImageView image1;
private ImageView image2;
public CustomButton()
{
super();
// Here add your specific images to global or local variables and then add all the children (images) to your HBox layout and this one to the button.
}
}
The problem ist, that JavaFX provides normal buttons and not such deeply customized components. If you want to style it, you can use CSS anyway.
I want to create a button that changes when the user hovers it, or clicking it.
I created the following variable
Button buttonPlay = new Button();
I don't know what to do now, how to load the images? How to write text into the button? How to implement the events / effects (hover, click)?
It would be very helpful if someone could write some example code for a button.
A button is simply an actor in libgdx. To render an actor you use a stage that contains all the actors of the screen, renders them and updates them. I assume you want a button with text, so you should use the class TextButton and add it to a stage. A TextButton takes a string to render and a ButtonStyle, in this case a TextButtonStyle, which is basically a class that contains all the information about the button (font, drawable to render while not pressed, drawable to render while pressed etc).
public class ButtonExample extends Game{
Stage stage;
TextButton button;
TextButtonStyle textButtonStyle;
BitmapFont font;
Skin skin;
TextureAtlas buttonAtlas;
#Override
public void create() {
stage = new Stage();
Gdx.input.setInputProcessor(stage);
font = new BitmapFont();
skin = new Skin();
buttonAtlas = new TextureAtlas(Gdx.files.internal("buttons/buttons.pack"));
skin.addRegions(buttonAtlas);
textButtonStyle = new TextButtonStyle();
textButtonStyle.font = font;
textButtonStyle.up = skin.getDrawable("up-button");
textButtonStyle.down = skin.getDrawable("down-button");
textButtonStyle.checked = skin.getDrawable("checked-button");
button = new TextButton("Button1", textButtonStyle);
stage.addActor(button);
}
#Override
public void render() {
super.render();
stage.draw();
}
}
So whats happening here? I am creating a stage, a font and a textureatlas with all the textures for the buttons in "buttons.pack". Then I initialize an empty TextButtonStyle and
and i add the font and the textures for the up, down and checked states. font, up, down and checked are all static variables of type Drawable so you can really pass it any kind of Drawable (texture, 9-patch etc). Then simply add the button to the Stage.
Now in order to do something when the button is actually clicked, you have to add a listener to the button, a ChangeListener.
button.addListener(new ChangeListener() {
#Override
public void changed (ChangeEvent event, Actor actor) {
System.out.println("Button Pressed");
}
});
Of course instead of adding the button directly to the Stage you should add it to a Table and add the Table to the Stage but I didn't want to make this post too confusing. Here is a good tutorial on tables in libgdx.
This is how you do it without adding any skin
TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle();
textButtonStyle.font = yourCustomFont;
textButtonStyle.fontColor = Color.WHITE;
stage.add(new TextButton("Custom Btn ", textButtonStyle));
buttons.pack is the file generated from the libgdx texture packer, texture packer is the tool which can used to generate the texture atlas , That is multiple images you can loaded to the GUI using a single file . it also help to save some memory please refer this link`https://code.google.com/p/libgdx-texturepacker-gui/downloads/list,
https://github.com/libgdx/libgdx/wiki/Texture-packer