Showing posts with label JavaFX. Show all posts
Showing posts with label JavaFX. Show all posts

Monday, March 31, 2014

JavaFX - Add video to the screen

To add a Video to your JavaFX screen, first create a Media using the video file, then create a MediaPlayer using the Media, and create a MeidaView using the created MediaPlayer or set the MediaPlayer of your MediaView to the created MediaPlayer. Finally add the MediaView to your scene.

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;

public class VedioTest extends Application {
 
    @Override
    public void start(Stage primaryStage) {
        //Creating the Media
        //Either use absolute path of the audio clip or it should be in the same place
        //where the Class used to load it is located
        //Here the Kalimba.mp3 should be in the same directory as the VedioTest
        Media media = new Media(VedioTest.class.getResource("TestVideo.mp4").toString());
     
        //Creating the MediaPlayer
        final MediaPlayer mediaPlayer = new MediaPlayer(media);
//        Make the video automatically play forever
//        mediaPlayer.setAutoPlay(true);
//        mediaPlayer.setCycleCount(MediaPlayer.INDEFINITE);
     
        //Creating the MediaView
        MediaView mediaView = new MediaView(mediaPlayer);
     
        Button btn = new Button();
        btn.setText("Play Video");
        btn.setOnAction(new EventHandler<ActionEvent>() {
         
            @Override
            public void handle(ActionEvent event) {
                //Call one of the methods of MediaPlayer such as
                //play(), pause(), and stop()
                mediaPlayer.play();
            }
        });
     
        //Add the MediaView to the scene
        Pane root = new Pane();
        btn.relocate(50, 200);
        root.getChildren().addAll(mediaView, btn);
//        root.getChildren().add(btn);
     
        Scene scene = new Scene(root, 300, 250);
     
        primaryStage.setTitle("Video!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
 
----------------------------------------------------------------------------------------------------------  

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

Friday, March 14, 2014

JavaFX - AudioClip

To add an AudioClip to your JavaFX screen, create an AudioClip and call its method play().

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.media.AudioClip;
import javafx.stage.Stage;

public class AudioClipTest extends Application {
 
    @Override
    public void start(Stage primaryStage) {
        //Creating the AudioClip
        //Either use absolute path of the audio clip or it should be in the same place
        //where the Class used to load it is located
        //Here the Kalimba.mp3 should be in the same directory as the AudioClipTest
        final AudioClip audioClip = new AudioClip(AudioClipTest.class.getResource("Kalimba.mp3").toString());
     
        Button btn = new Button();
        btn.setText("Song: Kalimba");
        btn.setOnAction(new EventHandler<ActionEvent>() {
         
            @Override
            public void handle(ActionEvent event) {
                //calling the AudioClip's play method
                audioClip.play();
            }
        });
     
        StackPane root = new StackPane();
        root.getChildren().add(btn);
     
        Scene scene = new Scene(root, 300, 250);
     
        primaryStage.setTitle("My favorate songs!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
 
--------------------------------------------------------------------------------------------------------------  

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

Tuesday, February 25, 2014

JavaFX using cascading style sheet (CSS) to change the appearance of your GUI

Using CSS to control the appearance of the user interface in JavaFX is quite similar to using CSS to control the user interface in HTML.


Style Definitions

Create the <your file name>.css file and put it in the main directory of your JavaFX application. Each style definitions in the CSS file has a name also known as the selector, followed by the curly braces {} that defines the declaration block inside which are the rules of the style definition. Each rule is a property with a value assigned to it. If the selector of the style definition is associated with a class name, it is preceded by a dot, and is known as a class style selector. For example,

.button{
       -fx-font: 18px "Serif";
       -fx-text-fill: white;
       -fx-alignment: LEFT;
       -fx-padding: 10;
       -fx-foreground-color: #00FF99;
       -fx-background-color: #CCFF99;
}

 If the selector of a style definition is associated with a JavaFX node ID, the selector is preceded by the # sign, and is known as an ID style selector. the node ID can be set by calling the node's setID() method. For example, if you have set the node ID of your Save button to save-button, the style definition may look like this.

#save-button{
       -fx-font-size: 14px;
       -fx-font-family: "Courier New";
       -fx-text-fill: rgb(203, 128, 99);
       -fx-alignment: CENTER;
       -fx-padding: 12;
}


.root Style Class

The .root style class defines the appearance of the root node of the scene. Since all nodes in the scene are descendants of the root node, rules in the .root style class are also applied to all the other nodes in the scene. A style rule assigned to a particular node overrides the corresponding rule in the .root style class. The common rules that consistently apply to other nodes are also included in the .root style class. They can be referenced by other style definitions to ensure UI consistency. For example in the following .root style class, the -fx-base is such a property that can be used by other style definitions.

.root {
       -fx-focused-base: blue;
       -fx-background: rgb(225, 212, 205);
}

.text-field.focused {
      -fx-color: -fx-focused-base;
}


Assign a class style definition to your control

Button saveButton = new Button("Save");
saveButton.getStyleClass().add( "<the class selector of your style definition>" );


Assing an ID style definition to your control

Button saveButton = new Button("Save");
saveButton.setId( "<the ID selector of your style definition>" );


Inline styles in the code

Instead of using the style definitions in a separated .css file, you can add your styles directly in the code.

Button saveButton = new Button("Save");
saveButton.setStyle( "-fx-font-size: 14px; -fx-font-family: "Courier"; -fx-alignment: CENTER;" );
          
--------------------------------------------------------------


                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

Wednesday, January 15, 2014

Integrating JavaFX code to a Swing package

To take advantage of JavaFX such as smooth animation, web views, audio and video playback, and styles based on Cascading Style Sheets (CSS) in your already built Java Swing project, you can either create a JFXPanel to add to your Swing JFrame, or create a JavaFX Application in your Swing package. You need to add these jars (deploy.jar, jfxrt.jar, plugin.jar, javaws.jar) from your JAVA_HOME\jdk folder\jre\lib directory to your project libraries or class path.

1. Creating a JFXPanel and add it to the JFrame. 


import java.awt.BorderLayout;
import java.awt.Dimension;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class NewFXSwingMain extends JFrame {
 
    private final int JFXPANEL_WIDTH_INT = 300;
    private final int JFXPANEL_HEIGHT_INT = 250;
    private JFXPanel fxContainer;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new NewFXSwingMain();
            }
        });
    }
 
    public NewFXSwingMain() {
        fxContainer = new JFXPanel();
        fxContainer.setPreferredSize(new Dimension(JFXPANEL_WIDTH_INT, JFXPANEL_HEIGHT_INT));
     
        // create JavaFX scene
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                createScene();
            }
        });
     
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Hello");
        add(fxContainer, BorderLayout.CENTER);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }
 
    private void createScene() {
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });
         //choose one of the panes from javafx.scene.layout
        StackPane root = new StackPane();
        root.getChildren().add(btn);
        fxContainer.setScene(new Scene(root));
    }
}

2. Creating a JavaFX class in your Swing package.


import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class NewFXMain extends Application {
 
    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });
     
        StackPane root = new StackPane();
        root.getChildren().add(btn);
     
        Scene scene = new Scene(root, 300, 250);
     
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
 
-------------------------------------------------------------------------------------------------------------  

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.
-------------------------------------------------------------------------------------------------------------------------

References:

1.JavaFX for Swing Developers