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
Related
I have created an ImageButton in my game. It has a listener and I have also added a texture, it works just fine but I couldn't find how to add a pressing effect to my buttons. Is there any premade libGDX method, or an easy way to have a pressing effect for my buttons?
You can use ImageButtonStyle to create a styling for your button, that automatically changes it's texture. This LibGDX Tutorial explains the use of buttons and button styles.
Basically you need to do something like this (from the tutorial):
ImageButton button3 = new ImageButton(mySkin);
button3.getStyle().imageUp = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("switch_off.png"))));
button3.getStyle().imageDown = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("switch_on.png"))));
If the button is handled by a Stage, like the following, it should be handled automatically:
public class MyGdxGame extends ApplicationAdapter {
private Stage stage;
#Override
public void create () {
stage = new Stage(new ScreenViewport());
Gdx.input.setInputProcessor(stage);
// TODO create the button
// add the button to the stage
stage.addActor(button3);
}
#Override
public void render () {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act();
stage.draw();
}
}
I am trying to set an image to a button but I can't get a working result.
My button is located in gridpane.
Image mine = new Image(new File("..some path").toURI().toString());
ImageView im = new ImageView(mine);
im.setFitHeight(btn.getHeight());
im.setFitWidth(btn.getWidth());
btn.setGraphic(im);
How can I do this?
I'm trying add to panel, but sometimes button was removed and I didnt get button with image.
private static void setFlag(Button btn,int x,int y,GridPane gridPane){
Image mine = new Image(new File("some path..").toURI().toString());
ImageView im = new ImageView(mine);
im.setFitHeight(btn.getHeight());
im.setFitWidth(btn.getWidth());
btn.setGraphic(im);
gridPane.add(btn, y, x);
}
This method is called when a button is pressed. Before that I used another method, where I used label and this method worked.
private static void setBombCell(Button btn, int x,int y,GridPane gridPane){
Image mine = new Image(new File("..").toURI().toString());
ImageView im = new ImageView(mine);
Label label = new Label();
label.setMaxWidth(Double.MAX_VALUE);
label.setMaxHeight(Double.MAX_VALUE);
GridPane.setHgrow(label, Priority.ALWAYS);
GridPane.setVgrow(label, Priority.ALWAYS);
im.setFitHeight(btn.getHeight());
im.setFitWidth(btn.getWidth());
label.setGraphic(im);
gridPane.add(label, y, x);
}
I solved this problem. I used method button.setStyle("") for this.
I have been trying to find a way to build two Ellipse object locationed next to each other that can be clicked and go to other screens by clicking one of them.
I don't know where to start. Right now I know only hope to build 2 regular buttons.
And how do we create it?
Just use a Circle as shape for the buttons. By default the shape will be scaled to the Button size, which results in ellipse shaped Buttons.
Button btn = new Button("Say 'Hello World'");
// circular shape scaled to Button dimensions = ellipse
btn.setShape(new Circle(1));
// increase distance from edges to text
btn.setPadding(new Insets(8));
What you can do is have a label
Label label = new Label();
label.setId("button");
And then add a image ellipse or what ever that you want to your label.
Image image = new Image(getClass().getResourceAsStream("ellipse.png"));
label.setGraphic(new ImageView(image));
Then add a mouse click event to the label
label1.setOnMousePressed(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent e) {
// write code to change screen
} });
elipse.addEventFilter(MouseEvent.MOUSE_CLICKED, e -> {
//Add scene change here.
});
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..
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.