Add timer for images in javafx - java

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);
}
}

Related

Disable ImageView Anti-Aliasing in JavaFX [duplicate]

This question already has answers here:
JavaFX ImageView without any smoothing
(4 answers)
Closed 1 year ago.
I made an ImageView in JavaFX and load a pixel art into it. And I scaled up, but I can't clearly see the pixels of the art. The problem should be the anti-aliasing. So I need to see clearly the pixels like on the second link. How can I turn anti-aliasing off?
I tried with img1.setSmooth(false);
What I made in JavaFX:
How it supposed to look like: //preview made in piskel
Here's my code:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
#Override public void start(Stage primaryStage) {
AnchorPane root = new AnchorPane();
StackPane hb = new StackPane();
Image image = new Image("pixelart.png");
ImageView img = new ImageView(image);
img.setFitWidth(500);
img.setFitHeight(500);
img.setSmooth(false);
hb.getChildren().add(img);
root.getChildren().addAll(hb);
Scene scene = new Scene(root,800,400);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {launch(args);}
}
pixelart.png:
One approach is to turn smoothing off when you construct the Image. Set the last argument of the constructor, smooth, to false.
Image image = new Image("pixelart.png", 400, 400, true, false);
As tested:
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;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
StackPane root = new StackPane();
Image image = new Image("pixelart.png", 400, 400, true, false);
ImageView view = new ImageView(image);
root.getChildren().add(view);
Scene scene = new Scene(root, 450, 450);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

Change the object's properties using animation

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();
}
}

Why is my Button Image setGraphic method not being displayed?

I have the image files in my bin folder but when I run my program all I have is my button options that are not being replaced with my images. I think it has something to do with the file: in front of the images but I'm not sure. The images are not actually a moving gif (dunno why its called gif).
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Project4 extends Application {
public static void main(String[] args) {
launch(args);
}
public void start(Stage primaryStage) throws Exception {
HBox root = new HBox();
Button btnKings;
Button btnDucks;
Button btnSharks;
Button btnBlues;
btnKings = new Button("Kings");
btnDucks = new Button("Ducks");
btnSharks = new Button("Sharks");
btnBlues = new Button("Blues");
Image imgKings = new Image("file:kings.jpg");
btnKings.setGraphic(new ImageView(imgKings));
Image imgDucks = new Image("file:ducks.gif");
btnDucks.setGraphic(new ImageView(imgDucks));
Image imgSharks = new Image("file:sharks.gif");
btnSharks.setGraphic(new ImageView(imgSharks));
Image imgBlues = new Image("file:blues.gif");
btnBlues.setGraphic(new ImageView(imgBlues));
HandleButtonClick clickEvent = new HandleButtonClick();
btnKings.setOnAction(clickEvent);
btnDucks.setOnAction(new HandleButtonClick("You clicked the ducKS!"));
btnKings.setOnAction(new HandleButtonClick("You clicked the Kings"));
btnSharks.setOnAction(new HandleButtonClick("You clicked the sharks"));
btnBlues.setOnAction(new HandleButtonClick("You clicked the blues"));
root.getChildren().add(btnKings);
root.getChildren().add(btnDucks);
root.getChildren().add(btnSharks);
root.getChildren().add(btnBlues);
Scene scene = new Scene(root, 960, 130);
primaryStage.setTitle("HockeyButtons");
primaryStage.setScene(scene);
primaryStage.show();
}
}
I cant find out how to replace the words buttons with the pictures
Nghia Duong
If you want only images to be shown on the button but text then you should create Button without text.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Project4 extends Application {
public static void main(String[] args) {
launch(args);
}
public void start(Stage primaryStage) throws Exception {
HBox root = new HBox();
Button btnKings;
Button btnDucks;
Button btnSharks;
Button btnBlues;
btnKings = new Button();
btnDucks = new Button();
btnSharks = new Button();
btnBlues = new Button();
Image imgKings = new Image("kings.jpg");
btnKings.setGraphic(new ImageView(imgKings));
Image imgDucks = new Image("ducks.gif");
btnDucks.setGraphic(new ImageView(imgDucks));
Image imgSharks = new Image("sharks.gif");
btnSharks.setGraphic(new ImageView(imgSharks));
Image imgBlues = new Image("blues.gif");
btnBlues.setGraphic(new ImageView(imgBlues));
root.getChildren().add(btnKings);
root.getChildren().add(btnDucks);
root.getChildren().add(btnSharks);
root.getChildren().add(btnBlues);
Scene scene = new Scene(root, 960, 130);
primaryStage.setTitle("HockeyButtons");
primaryStage.setScene(scene);
primaryStage.show();
}
}
And here you are showing the path of the image incorrectly.
Image imgKings = new Image("file:kings.jpg");
You'd better show absolute path or if the images are in classpath folder then by:
String absolutePathToIcon =
getClass().getResource("kings.jpg").toExternalForm();
Image imgKings = new Image(absolutePathToIcon);

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);
}
}

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);
}
}

Categories

Resources