how to resize button in a animation javafx - javafx

I'm using an animation in javafx so when I click on button it move with an animation but I want to also resize it (It should become smaller and smaller).
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
/*Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));*/
AnchorPane root=new AnchorPane();
Timeline timeline = new Timeline();
Button button = new Button();
timeline.getKeyFrames().addAll(
new KeyFrame(Duration.ZERO,
new KeyValue(button.translateXProperty(), 500),
new KeyValue(button.translateYProperty(), 500)),
new KeyFrame(Duration.seconds(.5), // set end position at 40s
new KeyValue(button.translateXProperty(), 200),
new KeyValue(button.translateYProperty(), 200)));
timeline.play();
root.getChildren().addAll(button);
primaryStage.show();
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 800, 800));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

One possibility is to animate the node's scaleProperties:
timeline.getKeyFrames().addAll(
new KeyFrame(Duration.ZERO,
new KeyValue(button.translateXProperty(), 500),
new KeyValue(button.translateYProperty(), 500),
new KeyValue(button.scaleXProperty(), 1),
new KeyValue(button.scaleYProperty(), 1)),
new KeyFrame(Duration.seconds(.5), // set end position at 40s
new KeyValue(button.translateXProperty(), 200),
new KeyValue(button.translateYProperty(), 200),
new KeyValue(button.scaleXProperty(), .4),
new KeyValue(button.scaleYProperty(), .4)));
timeline.play();

Related

Events for two overlay shapes

My problem:
I have a two overlay shapes, for example, two rectangles: A and B.
When B overlay A rectangle.
I add setOnMouseMoved handlers for both and i see that events handles only by top figure.
Code example:
public class MainExample extends Application {
#Override
public void start(Stage stage) {
stage.setMinWidth(700);
stage.setMaxWidth(700);
stage.setMinHeight(800);
stage.setMaxHeight(800);
StackPane root = new StackPane();
var a = new Rectangle(300, 300, Color.BLUE);
var b = new Rectangle(200, 200);
root.getChildren().addAll(a, b);
b.setOnMouseMoved(mouseEvent -> {
System.out.println("Mouse moved b!");
});
a.setOnMouseMoved(mouseEvent -> System.out.println("Mouse moved a!"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
What i want:
In the area of overlapping figures mouse moved events handles both of figures: A and B.
Thanx everyone for help!
I replace both mouse events to just one in stackpane node . The event checks if mouse coords is inside in both bounds (node a and node b) . When the mouse is in an area that intersetcs both rectangles the event wil thrigger both print statments
public class MainExample extends Application {
#Override
public void start(Stage stage) {
stage.setMinWidth(700);
stage.setMaxWidth(700);
stage.setMinHeight(800);
stage.setMaxHeight(800);
StackPane root = new StackPane();
var a = new Rectangle(300, 300, Color.BLUE);
var b = new Rectangle(200, 200);
root.getChildren().addAll(a, b);
root.setOnMouseMoved(mouseEvent -> {
if(b.getBoundsInParent().contains(mouseEvent.getSceneX(),mouseEvent.getSceneY())){
System.out.println("Mouse moved b!");}
if(a.getBoundsInParent().contains(mouseEvent.getSceneX(),mouseEvent.getSceneY())){
System.out.println("Mouse moved a!");}
});
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}

Border Pane issues

I want to make a border pane with two HBoxes on top and bottom and a GridPane in the center... I wrote what I needed, attached the labels but I can't run the code
Exception in Application start method
java.lang.reflect.InvocationTargetException is what I get as an error... the code is below, any help is welcome :) thanks
public class labelBorder extends Application {
#Override
public void start(Stage primaryStage) {
BorderPane bp = new BorderPane();
bp.setPrefSize(400, 400);
HBox hb1 = new HBox();
Label lb1 = new Label("");
lb1.setPrefWidth(200);
lb1.setBorder(new Border(new BorderStroke(Color.AQUAMARINE,BorderStrokeStyle.SOLID,null,new BorderWidths(5))));
Label lb2 = new Label("");
lb2.setPrefWidth(200);
lb2.setBorder(new Border(new BorderStroke(Color.BLUEVIOLET,BorderStrokeStyle.SOLID,null,new BorderWidths(5))));
HBox hb2 = new HBox();
URI foto = Paths.get("D:\\Barca.jpg").toUri();
Label lb3 = new Label();
lb3.setGraphic(new ImageView(foto.toString()));
lb3.autosize();
GridPane gp = new GridPane();
Label lb4 = new Label("");
Label lb5 = new Label("");
Label lb6 = new Label("");
Label lb7 = new Label("");
gp.add(lb4, 0, 0);
gp.add(lb5, 0, 1);
gp.add(lb6, 1, 0);
gp.add(lb7, 1, 1);
gp.getChildren().addAll(lb4,lb5,lb6,lb7);
hb1.getChildren().addAll(lb1,lb2);
hb2.getChildren().addAll(lb3);
bp.setTop(hb1);
bp.setCenter(gp);
bp.setBottom(hb2);
bp.getChildren().addAll(hb1,hb2,gp);
Scene scene = new Scene(bp);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
You should NOT add the duplicate children controls to your parent layout(pane). Your code must be throwing,
java.lang.IllegalArgumentException: Children: duplicate children added
To overcome your issue remove these lines,
gp.getChildren().addAll(lb4,lb5,lb6,lb7);
And,
bp.getChildren().addAll(hb1,hb2,gp);
As those controls have already been added to your corresponding layout.
Note: Adding children with pane.getChildren().addAll(...) on BorderPane is irrelevant and will be ignored while rendering the added controls.

How do I make my buttons all the same size?

Sorry, I have just begun learning javaFX and cannot figure out how to get all of my icons on buttons the same size. I tried a couple things but could not get it working, all help is appreciated. Thank you!
Wont let me post my question unless I add more details but I cant think of anything else to put so just ignore this whole paragraph as I ramble on so I can post my question and continue coding my game.
public class Main extends Application {
Stage window;
Button button;
Scene scene1, scene2;
public static final int ROCK = 0;
public static final int PAPER = 1;
public static final int SCISSORS = 2;
public static int userChoice;
public static void main(String [] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception
{
window = primaryStage;
//Layout 1
VBox layout = new VBox(20);
Label label = new Label("Rock Paper Scissors");
Button myButton = new Button("Start");
myButton.setOnAction(e -> window.setScene(scene2));
Button exit = new Button("Exit");
exit.setOnAction(e -> System.exit(0));
layout.getChildren().addAll(label, myButton, exit);
layout.setAlignment(Pos.CENTER);
scene1 = new Scene(layout, 300, 300);
//Layout 2
BorderPane border = new BorderPane();
VBox layout1 = new VBox(10);
Label label1 = new Label("Choose One");
layout1.setAlignment(Pos.CENTER);
//Layout 3
HBox layout2 = new HBox(10);
//Rock Image Button
Image rockIm = new Image(getClass().getResourceAsStream("Rock2.png"));
Button rock = new Button();
rock.setGraphic(new ImageView(rockIm));
rock.setOnAction(e -> userChoice = ROCK);
//Paper Image Button
Image paperIm = new
Image(getClass().getResourceAsStream("Paper2.png"));
Button paper = new Button();
paper.setGraphic(new ImageView(paperIm));
paper.setOnAction(e -> userChoice = PAPER);
//Scissor Image Button
Image scissorIm = new
Image(getClass().getResourceAsStream("Scissor2.png"));
Button scissors = new Button();
scissors.setGraphic(new ImageView(scissorIm));
scissors.setOnAction(e -> userChoice = SCISSORS);
Button quit = new Button("Return");
quit.setOnAction(e -> window.setScene(scene1));
layout2.getChildren().addAll(rock, paper, scissors, quit);
layout2.setAlignment(Pos.CENTER);
scene2 = new Scene(layout2, 300, 300);
window.setTitle("Rock Paper Scissors");
window.setScene(scene1);
window.show();
}
}

How to create own window rather then use default window

I've been working on javafx and i want to remove default windows and create a window with my style
It's quite easy to create a window in javafx. To create your own window you need to modify the style of your stage which can be done using initStyle() method.
public class Test extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
Scene scene = new Scene(createParent(), Color.TRANSPARENT);
primaryStage.initStyle(StageStyle.TRANSPARENT);
// primaryStage.initStyle(StageStyle.UNDECORATED);
// primaryStage.initStyle(StageStyle.DECORATED);
primaryStage.setTitle("My Own Window");
primaryStage.setScene(scene);
primaryStage.show();
}
private Parent createParent() {
Pane rootPane = new Pane();
rootPane.setPrefSize(1000,400);
Button btn = new Button("RandomButton");
btn.setOnAction(e -> Platform.exit());
rootPane.getChildren().add(btn);
return rootPane;
}
}

JAVAFX: How can I put this into one window instead of two?

I've been trying to make a toolbar inside a window with a checkers game, what happens now is, Checkers game opening in a separate window and so is the toolbar, what am I doing wrong? How can I make this code open in one window with both functions?
#Override
public void start(Stage primaryStage) throws Exception {
Stage toolStage = new Stage();
Button btnNewGame = new Button("New Game");
Button btnConcede = new Button("Concede");
Button btnNetwork = new Button("Network");
ToolBar toolBar = new ToolBar();
toolBar.getItems().addAll( new Separator(), btnNewGame, btnConcede, btnNetwork);
BorderPane pane = new BorderPane();
pane.setTop(toolBar);
Scene toolScene = new Scene(pane, 600, 400);
toolStage.setScene(toolScene);
toolStage.show();
Scene scene = new Scene(createContent());
primaryStage.setTitle("Dam spill - OBJ2000 Eksamen 2016");
primaryStage.setScene(scene);
primaryStage.show();
}
You're creating a new Scene+Stage fot the toolbar, show it and then show the content in the primaryStage instead of adding both toolbar and content as parts of the same scene, e.g. by adding the content as center node of the BorderPane:
#Override
public void start(Stage primaryStage) throws Exception {
Button btnNewGame = new Button("New Game");
Button btnConcede = new Button("Concede");
Button btnNetwork = new Button("Network");
ToolBar toolBar = new ToolBar();
toolBar.getItems().addAll( new Separator(), btnNewGame, btnConcede, btnNetwork);
BorderPane pane = new BorderPane();
pane.setTop(toolBar);
pane.setCenter(createContent());
Scene scene = new Scene(pane, 600, 400);
primaryStage.setTitle("Dam spill - OBJ2000 Eksamen 2016");
primaryStage.setScene(scene);
primaryStage.show();
}

Resources