This discussion is archived
2 Replies Latest reply: Oct 15, 2012 2:18 AM by 798853 RSS

Concurrency in JavaFX & MotionJPG

798853 Newbie
Currently Being Moderated
Hi

I am looking the way to build motion jpeg viewer ,

There is the folder in one server with one image file name only . I can access to this image from URL address , this image will update every 1 second .

I want to make one JavaFX application to display the image by reloading image file and replace the image source .

That mean application need kind of thread or data bind to load image every 1 second and seamlessly update the image viewer .

What I could not understand , how the concurrency will work in JavaFX , What is the differents between timeline method and task , Service , worker .

How I can binding the image source with new one , which help to display new image seamlessly .

In JavaFX is there any facility like JApplet which you have paint method to drawImage into panel ?

What would the best way to do that in JavaFX 2.0 ?

Thanks
Mavaj
  • 1. Re: Concurrency in JavaFX & MotionJPG
    shakir.gusaroff Expert
    Currently Being Moderated
    Hi. I would start by using a Timeline with an EventHandler and a Canvas:
           Canvas canvas = new Canvas(300, 250);
            GraphicsContext gc = canvas.getGraphicsContext2D();
    
    
            final Timeline animation = new Timeline(
                    new KeyFrame(Duration.seconds(1),
                    new EventHandler<ActionEvent>() {
                        @Override
                        public void handle(ActionEvent actionEvent) {
                            gc.drawImage(Image, x, y);
    
    
                        }
                    }));
            animation.setCycleCount(Animation.INDEFINITE);
            animation.play();
  • 2. Re: Concurrency in JavaFX & MotionJPG
    798853 Newbie
    Currently Being Moderated
    Thanks for your help , But I really not able to get the things working , I tried but the issue is , when you search for anything regarding JavaFX it bring so many stuff which is earliest version of javaFX , Today out of date and nothing work properly .

    What I am looking : 1. How I can bind image source ( I need each time to reload image from the source , local or network image ) then display they image in the screen as soon as load image .

    I thought I need use some kind of concurrency programming . But I found out we can do it with time line as well . ( what is the different between these two root ? I other word , what is the different between concurrency package and timeline in JavaFX )

    I get error if I use GraphicsContext gc = canvas.getGraphicsContext2D(); or Canvas canvas = new Canvas(300, 250);

    I just modify this code and timeline works but image will not refresh at all .
    import java.awt.Canvas;
    import javafx.animation.Animation;
    import javafx.animation.KeyFrame;
    import javafx.animation.Timeline;
    import javafx.application.Application;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Group;
    import javafx.scene.Scene;
    import javafx.stage.Stage;
    import javafx.geometry.Rectangle2D;
    import javafx.scene.Node;
    import javafx.scene.effect.SepiaTone;
    import javafx.scene.image.Image;
    import javafx.scene.image.ImageView;
    import javafx.util.Duration;
     
    /**
     * A sample that demonstrates varying degrees of a sepia tone effect.
     *
     * @see javafx.scene.effect.SepiaTone
     * @see javafx.scene.effect.Effect
     * @resource boat.jpg
     */
    public class SepiaToneSample extends Application {
        private final WebCamClass webCamClass = new WebCamClass();
        private static  Image BOAT = new Image("http://191.174.169.198/jpg/image.jpg");
        private void init(Stage primaryStage) {
            Group root = new Group();
            
            primaryStage.setScene(new Scene(root));
            ImageView sample = new ImageView(BOAT);
            final SepiaTone sepiaTone = new SepiaTone();
            sepiaTone.setLevel(0.5d);
            sample.setEffect(sepiaTone);
            root.getChildren().add(sample);
        }
     
        @Override public void start(Stage primaryStage) throws Exception {
            init(primaryStage);
            primaryStage.show();
        }
        public static void main(String[] args) { launch(args); }
    
        private static class WebCamClass {
    
            public WebCamClass() {
              Canvas canvas = new Canvas();
                //GraphicsContext gc = canvas.getGraphicsContext2D();
                final Timeline animation = new Timeline(
                        new KeyFrame(Duration.seconds(1),
                        new EventHandler<ActionEvent>() {
                            @Override
                            public void handle(ActionEvent actionEvent) {
                                //   gc.drawImage(Image, x, y);
                               
                                BOAT = new Image("http://191.174.169.198/jpg/image.jpg");                            
                                System.out.println("done");
                            }
                        }));
                animation.setCycleCount(Animation.INDEFINITE);
                animation.play();
            }
    
        }
        
           
    }

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points