Button btn=new Button("Click Me");
Button btn2=new Button("Click");
btn2.setOnAction(e->System.exit(0));
btn.setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent action){
System.out.println(5);
}
});
btn2.relocate(0, 0);
StackPane root=new StackPane();
root.getChildren().add(btn);
root.getChildren().add(btn2);
Scene sene=new Scene(root,500,265);
primaryStage.setScene(sene);
primaryStage.show();
I want to move button and using above code but I am unable to move my button?
What is the problem in code and is their any other way to do it???
The answer is :
http://docs.oracle.com/javafx/2/animations/jfxpub-animations.htm
JavaFX has ready libraries for animations.
An example (the rectangle will be faded):
final Rectangle rect1 = new Rectangle(10, 10, 100, 100);
rect1.setArcHeight(20);
rect1.setArcWidth(20);
rect1.setFill(Color.RED);
...
FadeTransition ft = new FadeTransition(Duration.millis(3000), rect1);
ft.setFromValue(1.0); ft.setToValue(0.1);
ft.setCycleCount(Timeline.INDEFINITE);
ft.setAutoReverse(true);
ft.play();
In your code you maybe need TranslateTranstition or just use translateX and translateY methods
Related
This is for homework but I like to do extra than what's assigned. The goal is if we click in the pane it will show the coordinates. Got it but I don't like how I have to click inside the pane is there a way to make it where if I click in the scene it will display in the pane? (here is what I got).
#Override
public void start(Stage primaryStage) {
Pane pane = new Pane();
Text text = new Text(50, 50, "Hello");
pane.getChildren().addAll(text);
text.setOnMouseClicked(e -> {
text.setText(e.getX() + ", " + e.getY() );
});
Scene scene = new Scene(pane, 300, 200);
primaryStage.setTitle("MouseClicker");
primaryStage.setScene(scene);
primaryStage.show();
I created scene, animations, list view, my problem is how to make animations play when I click on them in list view, and also, I need to create so more then one animation can play in one time.
Here is my code:
Group group = new Group();
Circle circle = new Circle(50, 300, 50);
circle.setFill(Color.RED);
TranslateTransition translate = new TranslateTransition();
translate.setByX(400);
translate.setDuration(Duration.millis(1000));
translate.setCycleCount(500);
translate.setAutoReverse(true);
translate.setNode(circle);
FadeTransition fade = new FadeTransition();
fade.setDuration(Duration.millis(1000));
fade.setFromValue(10);
fade.setToValue(0.1);
fade.setCycleCount(500);
fade.setAutoReverse(true);
fade.setNode(circle);
ScaleTransition transition = new ScaleTransition();
transition.setByX(1);
transition.setByY(1);
transition.setDuration(Duration.millis(1000));
transition.setCycleCount(500);
transition.setAutoReverse(true);
transition.setNode(circle);
ListView listView = new ListView();
listView.setPrefWidth(120);
listView.setPrefHeight(90);
listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
listView.getItems().add("Translate Transition");
listView.getItems().add("Fade Transition");
listView.getItems().add("Scale Transition");
group.getChildren().addAll(circle, listView);
Scene scene = new Scene(group, 600, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
So, I have problem just with:
How to start animation(can be more then one animation in same time) when I click on some animation in list view.
You can add a listener to your listview that listens when items are selected. Something like this:
listView.getSelectionModel().selectedItemProperty().addListener((obs, ov, nv) -> {
if(nv != null && "Translate Transition".equals(nv)){
translate.play();
}
//Etc...
});
I want to change the color of a button to it's invert color and back as a transition.
I was able to use Timeline with ColorAdjust to do this with the brightness, but I didn't found a effect class to transition the color of a button with Timeline.
Hope someone out there is able to help me?
Thanks!
A inversion effect can be achieved by using a Blend effect with BlendMode.DIFFERENCE and a topInput that is black for no inversion and white for completely inverted colors. Example:
#Override
public void start(Stage primaryStage) {
Blend blendEffect = new Blend(BlendMode.DIFFERENCE);
ColorInput input = new ColorInput();
blendEffect.setTopInput(input);
Button btn = new Button("Inversion Animation");
input.widthProperty().bind(btn.widthProperty());
input.heightProperty().bind(btn.heightProperty());
btn.setEffect(blendEffect);
btn.setStyle("-fx-body-color: orange;");
DoubleProperty brightness = new SimpleDoubleProperty(0);
input.paintProperty().bind(Bindings.createObjectBinding(() -> Color.BLACK.interpolate(Color.WHITE, brightness.get()), brightness));
Timeline timeline = new Timeline(
new KeyFrame(Duration.ZERO, new KeyValue(brightness, 0d)),
new KeyFrame(Duration.seconds(1), new KeyValue(brightness, 1d))
);
timeline.setOnFinished(evt -> timeline.setRate(-timeline.getRate()));
btn.setOnAction((ActionEvent event) -> {
timeline.play();
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 500, 500);
primaryStage.setScene(scene);
primaryStage.show();
}
My code creates a window and lays it out exactly how I want... initially. However, if I maximise the window, the top and bottom parts of the border pane do not remain in the centre. They drift off to the top left and bottom left corners.
I tried to disable the maximise window option, but again it messes up the look of the page, with the top and bottom parts moving.
Here is my code:
#Override
public void start(Stage startWindow) {
startWindow.setTitle("QuizApp");
BorderPane borderPane = new BorderPane();
borderPane.setTop(addHorizontalBoxWithMessage());
borderPane.setCenter(addImageView());
borderPane.setBottom(addHorizontalBoxWithButton());
Scene scene = new Scene(borderPane, 750, 663);
startWindow.setScene(scene);
scene.getStylesheets().add(StartWindow.class.getResource("application.css").toExternalForm());
// startWindow.resizableProperty().setValue(Boolean.FALSE);
startWindow.show();
}
public HBox addHorizontalBoxWithMessage() {
HBox hBox = new HBox();
hBox.setId("hBox");
hBox.setMinWidth(750);
hBox.setMinHeight(50);
hBox.setMaxWidth(750);
hBox.setMaxHeight(50);
hBox.setPadding(new Insets(0, 10, 0, 10));
hBox.setSpacing(10);
hBox.setAlignment(Pos.CENTER);
Text message = new Text("Welcome to the QuizApp!");
message.setId("message");
hBox.getChildren().add(message);
return hBox;
}
public ImageView addImageView() {
Image image = new Image(getClass().getResourceAsStream("quiz.jpg"));
ImageView imageView = new ImageView();
imageView.setImage(image);
imageView.setFitWidth(750);
imageView.setFitHeight(563);
return imageView;
}
public HBox addHorizontalBoxWithButton() {
HBox hBox = new HBox();
hBox.setId("hBox");
hBox.setMinWidth(750);
hBox.setMinHeight(50);
hBox.setMaxWidth(750);
hBox.setMaxHeight(50);
hBox.setPadding(new Insets(10, 10, 10, 10));
hBox.setSpacing(10);
hBox.setAlignment(Pos.CENTER);
Button registerButton = new Button("Register");
registerButton.setPrefSize(100, 30);
Button loginButton = new Button("Login");
loginButton.setPrefSize(100, 30);
hBox.getChildren().add(registerButton);
hBox.getChildren().add(loginButton);
return hBox;
}
I only started teaching myself JavaFX last night but can't seem to figure out where I am going wrong, or find a solution to my problem.
Thanks in advance for any advice.
In your code replace : hBox.setMaxWidth(750);
with:
hBox.setMaxWidth(Region.USE_COMPUTED_SIZE);
The problem was that after resizing your hbox, the width was still 750 px long
Another easy option is to use JavaFX Scene Builder to figure out how GUI components works.
In javaFX, I want make a imageview that can change border when I click.
When click once, then imageview has a border.
When click again, then imageview doesn't have a border.
How can I make that?
Thanks in advance!
Well you will need:
a PseudoClass for toggling the CSS state
a wrapping Region, because the ImageView itself does neither support a background nor a border.
A simple working example (the toggling of the PseudoClass is done with the help of a BooleanProperty, which is common practice and makes the management of its state easier):
#Override
public void start(Stage primaryStage) {
PseudoClass imageViewBorder = PseudoClass.getPseudoClass("border");
ImageView imageview = new ImageView(
new Image("http://upload.wikimedia.org/wikipedia/commons/1/16/Appearance_of_sky_for_weather_forecast,_Dhaka,_Bangladesh.JPG"));
BorderPane imageViewWrapper = new BorderPane(imageview);
imageViewWrapper.getStyleClass().add("image-view-wrapper");
BooleanProperty imageViewBorderActive = new SimpleBooleanProperty() {
#Override
protected void invalidated() {
imageViewWrapper.pseudoClassStateChanged(imageViewBorder, get());
}
};
imageview.setOnMouseClicked(ev -> imageViewBorderActive
.set(!imageViewBorderActive.get()));
BorderPane root = new BorderPane(imageViewWrapper);
root.setPadding(new Insets(15));
Scene scene = new Scene(root, 700, 400);
scene.getStylesheets().add(
getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
}
and the necessary CSS:
.image-view-wrapper:border {
-fx-border-color: black;
-fx-border-style: solid;
-fx-border-width: 5;
}