Change the object's properties using animation - java

import javafx.animation.FillTransition;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.ParallelTransition;
import javafx.animation.Timeline;
import javafx.scene.Scene;
import javafx.scene.SceneAntialiasing;
import javafx.scene.effect.BoxBlur;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;
public class NewFXMain extends javafx.application.Application {
#Override
public void start(Stage primaryStage) {
var pane = new Pane();
var rectangle = new Rectangle(300, 100, 400, 400);
var text = new Text(100, 100, "Java");
rectangle.setOnMouseEntered((event) -> {
var fill = new FillTransition(Duration.millis(1000), rectangle, (Color) rectangle.getFill(), Color.GREEN);
var timeline = new Timeline(new KeyFrame(Duration.millis(1000), new KeyValue(text.effectProperty(), new BoxBlur(4, 4, 4))));
var transition = new ParallelTransition(fill, timeline);
transition.play();
});
pane.getChildren().addAll(rectangle, text);
Scene scene = new Scene(pane, 1000, 720, true, SceneAntialiasing.BALANCED);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
This code should create an animation to change the color of the rectangle and blur the text.
However, while FillTransition works perfectly well, BoxBlur effect occurs without animation after 1 second.
How do I write effectProperty correctly to create an animation?
Please help.
Thank you

None of the Effect classes, as of JavaFX 12, implement Interpolatable. This means the animation can't gradually go from no effect to an end effect, thus your animation jumps from null to BoxBlur. You should animate the properties of the BoxBlur instance, rather than the effect property itself.
Here's an example:
import javafx.animation.FillTransition;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.ParallelTransition;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.effect.BoxBlur;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;
public final class App extends Application {
#Override
public void start(Stage primaryStage) {
var rectangle = new Rectangle(150, 100);
var text = new Text("Hello, World!");
text.setFill(Color.WHITE);
var effect = new BoxBlur(0, 0, 1);
text.setEffect(effect);
var animation = new ParallelTransition(
new FillTransition(Duration.seconds(1), rectangle, Color.BLACK, Color.GREEN),
new Timeline(
new KeyFrame(Duration.ZERO,
new KeyValue(effect.widthProperty(), 0),
new KeyValue(effect.heightProperty(), 0)
),
new KeyFrame(Duration.seconds(1),
new KeyValue(effect.widthProperty(), 10),
new KeyValue(effect.heightProperty(), 10)
)
)
);
rectangle.setOnMouseEntered(event -> {
event.consume();
animation.setRate(1);
animation.play();
});
rectangle.setOnMouseExited(event -> {
event.consume();
animation.setRate(-1);
animation.play();
});
primaryStage.setScene(new Scene(new StackPane(rectangle, text), 500, 300));
primaryStage.show();
}
}

Related

Background not showing javaFX

I am trying to create a start screen in javaFX and add a background to it to go behind the button and the text. It does not seem to work for me however, as the background doe not appear for my app. Would like to know what is wrong. Here is my code:
package calendar;
import java.io.IOException;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class Calendar extends Application {
#Override
public void start(Stage primaryStage) throws IOException {
primaryStage.setTitle("JavaFX Welcome");
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(25, 25, 25, 25));
Text scenetitle = new Text("Get Started");
scenetitle.setFont(Font.font("Bookman", FontWeight.BOLD, 30));
grid.add(scenetitle, 0, 0, 2, 1);
Button start = new Button("Start");
HBox hbStart = new HBox(10);
hbStart.setAlignment(Pos.CENTER);
hbStart.getChildren().add(start);
grid.add(hbStart, 1, 10);
final Text actiontarget = new Text();
grid.add(actiontarget, 1, 6);
start.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent e) {
}
});
Scene scene = new Scene(grid, 800, 600);
primaryStage.setScene(scene);
scene.getStylesheets().add
(Calendar.class.getResource("background.css").toExternalForm());
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
The code for the css file is as follows:
.root { -fx-background-image: url("background.jpeg"); }
Any help would be appreciated. I am fairly certain that this is not a problem with me misplacing or misnaming the file.

Display images depending on a timer [duplicate]

i want to add timer for my images in javafx for example first ,for about 3 seconds my first image displayed then for about 5 second my second image displayed and after that nothing shown.any idea about that ?
Use a Timeline to update the imageProperty of an ImageView:
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Duration;
public class ImageDisplayTest extends Application {
#Override
public void start(Stage primaryStage) {
Image image1 = new Image("...") ;
Image image2 = new Image("...") ;
ImageView imageView = new ImageView();
Timeline timeline = new Timeline(
new KeyFrame(Duration.ZERO, new KeyValue(imageView.imageProperty(), image1)),
new KeyFrame(Duration.seconds(3), new KeyValue(imageView.imageProperty(), image2)),
new KeyFrame(Duration.seconds(8), new KeyValue(imageView.imageProperty(), null))
);
timeline.play();
StackPane root = new StackPane();
root.getChildren().add(imageView);
primaryStage.setScene(new Scene(root, 800, 600));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

How to make a Button on a JavaFX's browser?

I'm trying to make a "launcher" with javafx.
This is my code :
I'm not shure you have to read all of this code, this code is here.
I'm trying to put a javaFX "play" button (i know how to make a button and how to set up an onclick event but i don't know where to add it :/)
Have you got an idea ? Thx.
package fr.whiteplay.main.launcher;
public class Launcher{
private static WebViewSample launcher;
private static String[] a;
public static void main(String[] args){
launcher = new WebViewSample();
a = args;
}
public static void start(){
launcher.go(a);
}
}
package fr.whiteplay.main.launcher;
import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.VPos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
public class WebViewSample extends Application{
private Browser browser;
private Scene scene;
public void start(Stage stage){
// create the scene
stage.setTitle("WhitePlay");
browser = new Browser();
scene = new Scene(browser, 992, 620, Color.web("#000000"));
stage.setScene(scene);
stage.show();
}
public static void go(String[] args){
launch(args);
}
}
package fr.whiteplay.main.launcher;
import javafx.geometry.HPos;
import javafx.geometry.VPos;
import javafx.scene.Node;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
class Browser extends Region{
final WebView browser = new WebView();
final WebEngine webEngine = browser.getEngine();
public Browser(){
getStyleClass().add("browser");
webEngine.load("http://www.whiteplay.fr/launcher/index.html");
getChildren().add(browser);
}
private Node createSpacer(){
Region spacer = new Region();
HBox.setHgrow(spacer, Priority.ALWAYS);
return spacer;
}
protected void layoutChildren(){
layoutInArea(browser, 0, 0, getWidth(), getHeight(), 0, HPos.CENTER, VPos.CENTER);
}
}
Instead of the browser itself, the scene root must be a structured panel, which contains the browser, the button, and whatever else.
The simplest example is to replace your WebViewSample.start() method with the following:
public void start(Stage stage){
// create the scene
stage.setTitle("WhitePlay");
browser = new Browser();
BorderPane root = new BorderPane();
root.setCenter(browser);
Button button = new Button("Play");
root.setBottom(button);
button.setOnAction(a -> System.out.println("Play"));
scene = new Scene(root, 992, 620, Color.web("#000000"));
stage.setScene(scene);
stage.show();
}
Check this page for further reference on various layouts options, and how to work with them.

Oracles JavaFX example with mouse click position label wrong?

At http://docs.oracle.com/javafx/2/charts/pie-chart.htm Oracle suggests using
caption.setTranslateX(e.getSceneX());
caption.setTranslateY(e.getSceneY());
to place a Label where the mouse was clicked.. But this does not work at all. See this print screen for proof:
In the code for the example you cite, the PieChart and caption Label are both placed directly in a Group which is the root of the scene. The position of the Label before applying transformations is therefore (0,0) (the top left of the Scene), and so translating it by (e.getSceneX(), e.getSceneY()) moves it to the position of the mouse.
If your layout is different, then the same computation will not necessarily work. For a more general solution, put the chart and caption in a Group, and then call sceneToLocal(...) on the Group to translate the scene coordinates to the correct coordinates in the Group:
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Point2D;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class PieChartSample extends Application {
#Override
public void start(Stage stage) {
BorderPane root = new BorderPane();
ObservableList<PieChart.Data> pieChartData =
FXCollections.observableArrayList(
new PieChart.Data("Grapefruit", 13),
new PieChart.Data("Oranges", 25),
new PieChart.Data("Plums", 10),
new PieChart.Data("Pears", 22),
new PieChart.Data("Apples", 30));
final PieChart chart = new PieChart(pieChartData);
chart.setTitle("Imported Fruits");
final Label caption = new Label("");
caption.setTextFill(Color.DARKORANGE);
caption.setStyle("-fx-font: 24 arial;");
Group chartWithCaption = new Group(chart, caption);
for (final PieChart.Data data : chart.getData()) {
data.getNode().addEventHandler(MouseEvent.MOUSE_PRESSED,
new EventHandler<MouseEvent>() {
#Override public void handle(MouseEvent e) {
Point2D locationInScene = new Point2D(e.getSceneX(), e.getSceneY());
Point2D locationInParent = chartWithCaption.sceneToLocal(locationInScene);
caption.relocate(locationInParent.getX(), locationInParent.getY());
caption.setText(String.valueOf(data.getPieValue()) + "%");
}
});
}
root.setCenter(chartWithCaption);
// Just some stuff to change the overall layout:
HBox controls = new HBox(5);
controls.setPadding(new Insets(10));
controls.setAlignment(Pos.CENTER);
controls.getChildren().addAll(new Label("Some other stuff here"), new TextField(), new Button("OK"));
root.setTop(controls);
root.setPadding(new Insets(0, 0, 10, 40));
root.setLeft(new Circle(25, Color.SALMON));
Scene scene = new Scene(root);
stage.setTitle("Imported Fruits");
stage.setWidth(600);
stage.setHeight(500);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}

Add timer for images in javafx

i want to add timer for my images in javafx for example first ,for about 3 seconds my first image displayed then for about 5 second my second image displayed and after that nothing shown.any idea about that ?
Use a Timeline to update the imageProperty of an ImageView:
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Duration;
public class ImageDisplayTest extends Application {
#Override
public void start(Stage primaryStage) {
Image image1 = new Image("...") ;
Image image2 = new Image("...") ;
ImageView imageView = new ImageView();
Timeline timeline = new Timeline(
new KeyFrame(Duration.ZERO, new KeyValue(imageView.imageProperty(), image1)),
new KeyFrame(Duration.seconds(3), new KeyValue(imageView.imageProperty(), image2)),
new KeyFrame(Duration.seconds(8), new KeyValue(imageView.imageProperty(), null))
);
timeline.play();
StackPane root = new StackPane();
root.getChildren().add(imageView);
primaryStage.setScene(new Scene(root, 800, 600));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

Categories

Resources