JavaFX ImageView Transition - javafx

I'm trying to create image gallery and use some image animations. Problem is with ImageView. I would like to play() RotateTransition from some method and call this method any time but it's not working at all. There should be some issue with threads but even if it is called from new thread nothing is happening. Is there any solution how to work with ImageView and Transitions generally?
public class ImageGallery extends ImageView{
RotateTransition rt;
public ImageGallery() {
setImage(new Image("/img/01.jpg"));
setPreserveRatio(true);
rt = new RotateTransition(Duration.millis(800), this);
rt.setByAngle(90);
//this works but not what I need
//fitWidthProperty().addListener(e -> rt.play());
}
public void rotateRight(){
rt.play(); //nothing
//run later is not working too
//Platform.runLater(new ViewTransition(this));
}
}
Thanks

As per user comments in the question, adding a MCVE
Main.java
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
ImageGallery gallery = new ImageGallery();
VBox box= new VBox(gallery);
box.setAlignment(Pos.CENTER);
Scene scene = new Scene(box, 400, 400);
primaryStage.setScene(scene);
primaryStage.show();
gallery.rotateRight();
}
public static void main(String[] args){
launch(args);
}
}
ImageGallery.java
import javafx.animation.RotateTransition;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.util.Duration;
public class ImageGallery extends ImageView{
RotateTransition rt;
public ImageGallery() {
setImage(new Image("http://jaxenter.com/wp-content/uploads/2013/03/javafx.1.png"));
setPreserveRatio(true);
rt = new RotateTransition(Duration.millis(800), this);
rt.setByAngle(90);
}
public void rotateRight(){
rt.play();
}
}

Related

use vlcj-javafx-demo develop a player, but it looks some components UI not update correctly when set full screen

I try to use vlcj-javafx-demo to develop a video player, and I put the progress bar(Slider) on the StackPane over the video layer. In the beginning, it looks work well, but when I set maximum or full screen the app, it looks some components UI did not update correctly. How can I correct it?
Thanks a lot!
normally:
[1]: https://i.stack.imgur.com/bbE51.png
normally:
[2]: https://i.stack.imgur.com/Plsb1.png
the red color is the sence background color.
the code :
package my.javafx.myplayer;
import javafx.animation.FadeTransition;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.DoubleProperty;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.Slider;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Duration;
import uk.co.caprica.vlcj.factory.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.base.MediaPlayer;
import uk.co.caprica.vlcj.player.base.MediaPlayerEventAdapter;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;
import java.util.ArrayList;
import java.util.List;
import static uk.co.caprica.vlcj.javafx.videosurface.ImageViewVideoSurfaceFactory.videoSurfaceForImageView;
/**
*
*/
public class VlcjJavaFxApplication extends Application {
private final MediaPlayerFactory mediaPlayerFactory;
private final EmbeddedMediaPlayer embeddedMediaPlayer;
private ImageView videoImageView;
Slider progress=new Slider();
public VlcjJavaFxApplication() {
this.mediaPlayerFactory = new MediaPlayerFactory();
this.embeddedMediaPlayer = mediaPlayerFactory.mediaPlayers().newEmbeddedMediaPlayer();
this.embeddedMediaPlayer.events().addMediaPlayerEventListener(new MediaPlayerEventAdapter() {
#Override
public void mediaPlayerReady(uk.co.caprica.vlcj.player.base.MediaPlayer mediaPlayer) {
Platform.runLater(()->{
progress.setValue(0);
progress.setMax(embeddedMediaPlayer.media().info().duration());
progress.setMin(0);
});
}
#Override
public void playing(uk.co.caprica.vlcj.player.base.MediaPlayer mediaPlayer) {
}
#Override
public void paused(uk.co.caprica.vlcj.player.base.MediaPlayer mediaPlayer) {
}
#Override
public void stopped(uk.co.caprica.vlcj.player.base.MediaPlayer mediaPlayer) {
}
#Override
public void timeChanged(uk.co.caprica.vlcj.player.base.MediaPlayer mediaPlayer, long newTime) {
Platform.runLater(()->{
progress.setValue(newTime);
});
}
});
}
#Override
public void init() {
this.videoImageView = new ImageView();
this.videoImageView.setPreserveRatio(true);
embeddedMediaPlayer.videoSurface().set(videoSurfaceForImageView(this.videoImageView));
}
#Override
public final void start(Stage primaryStage) throws Exception {
List<String> params = new ArrayList<String>();
params.add("/Users/baixq/Downloads/妙味课堂xhtml+css2/妙味课堂-XHTMLCSS2整站视频教程-4.avi");
if (params.size() != 1) {
System.out.println("Specify a single MRL");
System.exit(-1);
}
StackPane root=new StackPane();
//BorderPane root = new BorderPane();
root.setStyle("-fx-background-color: black;");
videoImageView.fitWidthProperty().bind(root.widthProperty());
videoImageView.fitHeightProperty().bind(root.heightProperty());
root.widthProperty().addListener((observableValue, oldValue, newValue) -> {
// If you need to know about resizes
});
root.heightProperty().addListener((observableValue, oldValue, newValue) -> {
// If you need to know about resizes
});
Scene scene = new Scene(root, 1200, 675, Color.RED);
primaryStage.setTitle("vlcj JavaFX");
primaryStage.setScene(scene);
AnchorPane contrlBox=new AnchorPane();//操作面板上的控制模块
contrlBox.prefWidthProperty().bind(root.widthProperty());
contrlBox.prefHeightProperty().bind(root.heightProperty().multiply(0.1));
BorderPane controlBar=new BorderPane();
controlBar.setStyle("-fx-background-color: #130c0e;");
controlBar.prefWidthProperty().bind(root.widthProperty());
controlBar.prefHeightProperty().bind(root.heightProperty().multiply(0.1));
controlBar.setCenter(progress);
Button fullScreen=new Button("全屏");
controlBar.setRight(fullScreen);
contrlBox.getChildren().add(controlBar);
contrlBox.setBottomAnchor(controlBar, 0.0);
root.getChildren().addAll(videoImageView,contrlBox);
primaryStage.show();
embeddedMediaPlayer.media().play(params.get(0));
fullScreen.setOnAction(event->{
primaryStage.setFullScreen(true);
});
root.setOnMouseEntered(event->{
Platform.runLater(()->{
FadeTransition ft = new FadeTransition(Duration.millis(500), contrlBox);
ft.setFromValue(0.0);
ft.setToValue(1);
//ft.setCycleCount(Timeline.INDEFINITE);
ft.setAutoReverse(false);
ft.play();
contrlBox.setVisible(true);
});
});
root.setOnMouseExited(event->{
Platform.runLater(()->{
FadeTransition ft = new FadeTransition(Duration.millis(500), contrlBox);
ft.setFromValue(1);
ft.setToValue(0.0);
ft.setAutoReverse(false);
ft.play();
contrlBox.setVisible(false);
});
});
//embeddedMediaPlayer.controls().setPosition(0.4f);
}
#Override
public final void stop() {
embeddedMediaPlayer.controls().stop();
embeddedMediaPlayer.release();
mediaPlayerFactory.release();
}
public static void main(String[] args) {
launch(args);
}
}
You appear to be using a Linux OS, try passing one or more of these system properties when you start your JVM:
-Dprism.dirtyopts=false
-Dprism.forceUploadingPainter=true
This is mentioned under "Linux notes" here: https://github.com/caprica/vlcj-javafx-demo/tree/vlcj-5.x
I have seen similar painting glitches on Linux before and in all cases using these properties, at least for me, clears the issue with only a small hit to performance - even when doing something like a full-screen grid of nine concurrent media players, where each one had an animating video controls overlay.

Error with handle(Event) in EventHandler

I'm new new to javafx and I would like just to print the the coordinates of the mouse click.
I've seen examples but I've got an error in the EventHandler line that I don't understand, it says:
anonymous javafxapplication2.JavaFXApplication2$1 is not abstract and does not override abstract method handle(Event) in EventHandler
What's the problem? Thanks!
package javafxapplication2;
import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
public class JavaFXApplication2 extends Application {
#Override
public void start(Stage primaryStage) {
Line line = new Line();
Group root = new Group(line);
Scene scene = new Scene(root, 800, 800);
scene.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler() {
#Override
public void handle(MouseEvent event) {
System.out.println(event.getX());
System.out.println(event.getY());
}
}
primaryStage.setTitle("Disegna linee");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
From looking at the documentation here on javafx handlers I believe your problem is that you need to provide the type to the new eventHandler like
scene.addEventHandler(MouseEvent.MOUSE_CLICKED,
new EventHandler<MouseEvent>() { // Was missing the <MouseEvent>
#Override
public void handle(MouseEvent event) { ... };
});

javafx fxml bring window to front form itself

I need to bring to front JavaFX FXML window from itself. Something like this:
procedure (boolean close)
{
if(close)
current_window.toFront();
}
How should I get this window(scene) ?
Try this
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage stage) {
Text text = new Text("!");
text.setFont(new Font(40));
VBox box = new VBox();
box.getChildren().add(text);
final Scene scene = new Scene(box,300, 250);
scene.setFill(null);
stage.setScene(scene);
stage.show();
stage.toFront();
}
public static void main(String[] args) {
launch(args);
}
}
If you have access to any of the nodes, you can use the following
((Stage)node.getScene().getWindow()).toFront();

Javafx: Change scene in setOnAction

I'm building a JavaFX application with multiple Scenes. I have a problem with scope of variable when changing scenes within setOnAction event. This is my code:
Stage myStage;
public Scene logInScene(){
... all the buttons / textFields
createAccountButton.setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent t){
**this.getStage().allScene(createAccountPane1);**
}
}
}
public Stage getStage(){
return this.myStage;
}
public void allScene(Pane p){
this.myStage.setScene(p);
}
I'm getting an error within the setOnAction function. "Cannot Find Symbol" getStage(). I know this must be a scope problem and it doesn't recognize any variables / functions outside of that scope. How do I make it so that I can change within? I've tried passing through the variable but that will just make my code messy and I wish there was a simpler way. Thanks guys!
Your code works as long as you keep consistency:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Test extends Application{
private Stage stage;
#Override
public void start(Stage primaryStage) throws Exception {
stage = primaryStage;
Scene scene = logInScene();
primaryStage.setScene(scene);
primaryStage.show();
}
public Scene logInScene(){
Pane root = new Pane();
Button createAccountButton = new Button("create account");
createAccountButton.setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent t){
stage.setScene(CreateAccountScene());
}
});
root.getChildren().add(createAccountButton);
return new Scene(root);
}
protected Scene CreateAccountScene() {
VBox root = new VBox();
Label userLabel = new Label("Insert the username:");
final TextField userField = new TextField();
Button createAccountButton = new Button("create account");
createAccountButton.setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent t){
System.out.println("Account for user " + userField.getText() + " was created succesfully");
}
});
root.getChildren().addAll(userLabel,userField,createAccountButton);
return new Scene(root);
}
public static void main(String[] args) {
launch(args);
}
}
This question has already been solved, but I think it's worth clarifying that your line fails because the this keyword refers to the anonymous EventHandler you are implementing. In Java, you reference the outer class instance with OuterClass.this. So OuterClass.this.getStage().allScene(createAccountPane1); will work.
If you are looking for a prettier solution, some coders like to define a local variable that points to the outer class instance:
final OuterClass self = this;
createAccountButton.setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent t){
self.getStage().allScene(createAccountPane1);
}
}

Removing object from scene in javafx

I am digging the documentation to see if there's a remove method, I just get this link whenever I google
http://www.coderanch.com/t/580998/JavaFX/java/remove-node
there's a simple remove option
Eg : .getChildren().remove(object)
It does not seem to work for me !
The code which you've provided works fine with me.
Add circle with ALT+Click, and remove them by simply clicking on them.
The reason I've used the ALT key for adding the circles is because in the below code, both the scene and the circles handle mouse clicks. Thus, the code has to know from where the event is coming from. This is just an example, of course.
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class ChangeListenerSample extends Application {
public static void main(final String[] args) {
launch(args);
}
#Override
public void start(final Stage primaryStage) throws Exception {
final Group root = new Group();
primaryStage.setResizable(false);
final Scene scene = new Scene(root, 400,80);
primaryStage.setScene(scene);
scene.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
#Override public void handle(final MouseEvent event)
{
if (!event.isAltDown())
return;
final Circle circle = new Circle(event.getSceneX(), event.getSceneY(),30);
circle.setFill(Color.YELLOW);
root.getChildren().add(circle);
circle.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
#Override public void handle(final MouseEvent arg0)
{
root.getChildren().remove(circle);
}
});
}
});
primaryStage.show();
}
}

Resources