Thursday, July 30, 2015

Detect mouse event on JavaFX LineChart Series Node


To detect mouse event on JavaFX LineChart Series Node - here is a simple example to change mouse cursor when mouse move over the lines.

                seriesR.getNode().setOnMouseEntered(onMouseEnteredSeriesListener);
                seriesR.getNode().setOnMouseExited(onMouseExitedSeriesListener);
    ...

    
    //Lambda expression
    EventHandler<MouseEvent> onMouseEnteredSeriesListener = 
            (MouseEvent event) -> {
                ((Node)(event.getSource())).setCursor(Cursor.HAND);
    };
    
    /* traditional expression
    EventHandler<MouseEvent> onMouseEnteredSeriesListener = 
            new EventHandler<MouseEvent>(){

        @Override
        public void handle(MouseEvent event) {
            ((Node)(event.getSource())).setCursor(Cursor.HAND);

        }
        
    };
    */
    
    //Lambda expression
    EventHandler<MouseEvent> onMouseExitedSeriesListener = 
            (MouseEvent event) -> {
                ((Node)(event.getSource())).setCursor(Cursor.DEFAULT);
    };
    
    /* traditional expression
    EventHandler<MouseEvent> onMouseExitedSeriesListener = 
            new EventHandler<MouseEvent>(){

        @Override
        public void handle(MouseEvent event) {
            ((Node)(event.getSource())).setCursor(Cursor.DEFAULT);
        }
        
    };
    */

Modify the example code of "Display brightness histogram on JavaFX LineChart" to change mouse cursor when move over lines in LineCharts.


package javafx_imagehistogram;

import java.awt.Color;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Cursor;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.PixelReader;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 * @web http://java-buddy.blogspot.com
 */
public class JavaFX_ImageHistogram extends Application {

    String defaultImage = "http://goo.gl/kYEQl";

    @Override
    public void start(Stage primaryStage) {

        Label labelInfo = new Label();
        labelInfo.setText(
                "java.version: " + System.getProperty("java.version") + "\n"
                + "javafx.runtime.version: " + System.getProperty("javafx.runtime.version")
        );

        TextField textSrc = new TextField();
        textSrc.setText(defaultImage);
        Button btnDo = new Button("Do Histogram");
        ImageView imageView = new ImageView();

        final CategoryAxis xAxis = new CategoryAxis();
        final NumberAxis yAxis = new NumberAxis();
        final LineChart<String, Number> chartHistogram
                = new LineChart<>(xAxis, yAxis);
        chartHistogram.setCreateSymbols(false);
        
        final CategoryAxis xAxis_brightness = new CategoryAxis();
        final NumberAxis yAxis_brightness = new NumberAxis();
        final LineChart<String, Number> brightnessHistogram
                = new LineChart<>(xAxis_brightness, yAxis_brightness);
        brightnessHistogram.setCreateSymbols(false);
        
        VBox vBoxHistogram = new VBox();
        vBoxHistogram.getChildren().addAll(chartHistogram, brightnessHistogram);
        ScrollPane scrollPane = new ScrollPane();
        scrollPane.setContent(vBoxHistogram);

        btnDo.setOnAction((ActionEvent event) -> {

            String imageSrc = textSrc.getText();
            Image image = new Image(imageSrc);
            imageView.setImage(image);
            chartHistogram.getData().clear();
            brightnessHistogram.getData().clear();

            ImageHistogram imageHistogram = new ImageHistogram(image);
            if(imageHistogram.isSuccess()){
                
                XYChart.Series seriesR = imageHistogram.getSeriesRed();
                XYChart.Series seriesG = imageHistogram.getSeriesGreen();
                XYChart.Series seriesB = imageHistogram.getSeriesBlue();
                XYChart.Series seriesBr = imageHistogram.getSeriesBrightness();
                
                chartHistogram.getData().addAll(
                    seriesR, seriesG, seriesB);
                
                brightnessHistogram.getData().add(seriesBr);
                
                seriesR.getNode().setOnMouseEntered(onMouseEnteredSeriesListener);
                seriesR.getNode().setOnMouseExited(onMouseExitedSeriesListener);
                seriesG.getNode().setOnMouseEntered(onMouseEnteredSeriesListener);
                seriesG.getNode().setOnMouseExited(onMouseExitedSeriesListener);
                seriesB.getNode().setOnMouseEntered(onMouseEnteredSeriesListener);
                seriesB.getNode().setOnMouseExited(onMouseExitedSeriesListener);
                seriesBr.getNode().setOnMouseEntered(onMouseEnteredSeriesListener);
                seriesBr.getNode().setOnMouseExited(onMouseExitedSeriesListener);
            }
        });

        HBox hBox = new HBox();
        hBox.getChildren().addAll(imageView, scrollPane);

        VBox vBox = new VBox();
        vBox.getChildren().addAll(labelInfo, textSrc, btnDo, hBox);

        StackPane root = new StackPane();
        root.getChildren().add(vBox);

        Scene scene = new Scene(root, 1000, 500);

        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
    //Lambda expression
    EventHandler<MouseEvent> onMouseEnteredSeriesListener = 
            (MouseEvent event) -> {
                ((Node)(event.getSource())).setCursor(Cursor.HAND);
    };
    
    /* traditional expression
    EventHandler<MouseEvent> onMouseEnteredSeriesListener = 
            new EventHandler<MouseEvent>(){

        @Override
        public void handle(MouseEvent event) {
            ((Node)(event.getSource())).setCursor(Cursor.HAND);

        }
        
    };
    */
    
    //Lambda expression
    EventHandler<MouseEvent> onMouseExitedSeriesListener = 
            (MouseEvent event) -> {
                ((Node)(event.getSource())).setCursor(Cursor.DEFAULT);
    };
    
    /* traditional expression
    EventHandler<MouseEvent> onMouseExitedSeriesListener = 
            new EventHandler<MouseEvent>(){

        @Override
        public void handle(MouseEvent event) {
            ((Node)(event.getSource())).setCursor(Cursor.DEFAULT);
        }
        
    };
    */

    class ImageHistogram {

        private Image image;

        private long alpha[] = new long[256];
        private long red[] = new long[256];
        private long green[] = new long[256];
        private long blue[] = new long[256];
        
        private long brightness[] = new long[256];

        XYChart.Series seriesAlpha;
        XYChart.Series seriesRed;
        XYChart.Series seriesGreen;
        XYChart.Series seriesBlue;
        
        XYChart.Series seriesBrightness;

        private boolean success;

        ImageHistogram(Image src) {
            image = src;
            success = false;

            //init
            for (int i = 0; i < 256; i++) {
                alpha[i] = red[i] = green[i] = blue[i] = 0;
                brightness[i] = 0;
            }

            PixelReader pixelReader = image.getPixelReader();
            if (pixelReader == null) {
                return;
            }

            //count pixels
            for (int y = 0; y < image.getHeight(); y++) {
                for (int x = 0; x < image.getWidth(); x++) {
                    int argb = pixelReader.getArgb(x, y);
                    int a = (0xff & (argb >> 24));
                    int r = (0xff & (argb >> 16));
                    int g = (0xff & (argb >> 8));
                    int b = (0xff & argb);

                    alpha[a]++;
                    red[r]++;
                    green[g]++;
                    blue[b]++;
                    
                    //Convert RGB to HSB (or HSV)
                    float[] hsb = new float[3];
                    Color.RGBtoHSB(r, g, b, hsb);
                    brightness[(int)(hsb[2]*255)]++;
                }
            }

            seriesAlpha = new XYChart.Series();
            seriesRed = new XYChart.Series();
            seriesGreen = new XYChart.Series();
            seriesBlue = new XYChart.Series();
            seriesBrightness = new XYChart.Series();
            seriesAlpha.setName("alpha");
            seriesRed.setName("red");
            seriesGreen.setName("green");
            seriesBlue.setName("blue");
            seriesBrightness.setName("Brightness");

            //copy alpha[], red[], green[], blue[], brightness
            //to seriesAlpha, seriesRed, seriesGreen, seriesBlue, seriesBrightness
            for (int i = 0; i < 256; i++) {
                seriesAlpha.getData().add(new XYChart.Data(String.valueOf(i), alpha[i]));
                seriesRed.getData().add(new XYChart.Data(String.valueOf(i), red[i]));
                seriesGreen.getData().add(new XYChart.Data(String.valueOf(i), green[i]));
                seriesBlue.getData().add(new XYChart.Data(String.valueOf(i), blue[i]));
                
                seriesBrightness.getData().add(new XYChart.Data(String.valueOf(i), brightness[i]));
            }

            success = true;
        }

        public boolean isSuccess() {
            return success;
        }

        public XYChart.Series getSeriesAlpha() {
            return seriesAlpha;
        }

        public XYChart.Series getSeriesRed() {
            return seriesRed;
        }

        public XYChart.Series getSeriesGreen() {
            return seriesGreen;
        }

        public XYChart.Series getSeriesBlue() {
            return seriesBlue;
        }
        
        public XYChart.Series getSeriesBrightness() {
            return seriesBrightness;
        }
    }

}

Tuesday, July 28, 2015

Display brightness histogram on JavaFX LineChart


The java.awt.Color.RGBtoHSB(int r, int g, int b, float[] hsbvals) method converts RGB components to an equivalent set of values for hue, saturation, and brightness that are the three components of the HSB model. We can easy modify last post "Display image's Histogram on JavaFX LineChart" to display brightness histogram.


remark: In the video, photos from http://www.ultimate-photo-tips.com/histogram-examples.html are used as sample to generate histogram.

Example code:
package javafx_imagehistogram;

import java.awt.Color;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.PixelReader;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 * @web http://java-buddy.blogspot.com
 */
public class JavaFX_ImageHistogram extends Application {

    String defaultImage = "http://goo.gl/kYEQl";

    @Override
    public void start(Stage primaryStage) {

        Label labelInfo = new Label();
        labelInfo.setText(
                "java.version: " + System.getProperty("java.version") + "\n"
                + "javafx.runtime.version: " + System.getProperty("javafx.runtime.version")
        );

        TextField textSrc = new TextField();
        textSrc.setText(defaultImage);
        Button btnDo = new Button("Do Histogram");
        ImageView imageView = new ImageView();

        final CategoryAxis xAxis = new CategoryAxis();
        final NumberAxis yAxis = new NumberAxis();
        final LineChart<String, Number> chartHistogram
                = new LineChart<>(xAxis, yAxis);
        chartHistogram.setCreateSymbols(false);
        
        final CategoryAxis xAxis_brightness = new CategoryAxis();
        final NumberAxis yAxis_brightness = new NumberAxis();
        final LineChart<String, Number> brightnessHistogram
                = new LineChart<>(xAxis_brightness, yAxis_brightness);
        brightnessHistogram.setCreateSymbols(false);
        
        VBox vBoxHistogram = new VBox();
        vBoxHistogram.getChildren().addAll(chartHistogram, brightnessHistogram);
        ScrollPane scrollPane = new ScrollPane();
        scrollPane.setContent(vBoxHistogram);

        btnDo.setOnAction((ActionEvent event) -> {

            String imageSrc = textSrc.getText();
            Image image = new Image(imageSrc);
            imageView.setImage(image);
            chartHistogram.getData().clear();
            brightnessHistogram.getData().clear();

            ImageHistogram imageHistogram = new ImageHistogram(image);
            if(imageHistogram.isSuccess()){
                chartHistogram.getData().addAll(
                    //imageHistogram.getSeriesAlpha(),
                    imageHistogram.getSeriesRed(),
                    imageHistogram.getSeriesGreen(),
                    imageHistogram.getSeriesBlue());
                
                brightnessHistogram.getData().add(
                    imageHistogram.getSeriesBrightness());
            }
        });

        HBox hBox = new HBox();
        hBox.getChildren().addAll(imageView, scrollPane);

        VBox vBox = new VBox();
        vBox.getChildren().addAll(labelInfo, textSrc, btnDo, hBox);

        StackPane root = new StackPane();
        root.getChildren().add(vBox);

        Scene scene = new Scene(root, 1000, 500);

        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

    class ImageHistogram {

        private Image image;

        private long alpha[] = new long[256];
        private long red[] = new long[256];
        private long green[] = new long[256];
        private long blue[] = new long[256];
        
        private long brightness[] = new long[256];

        XYChart.Series seriesAlpha;
        XYChart.Series seriesRed;
        XYChart.Series seriesGreen;
        XYChart.Series seriesBlue;
        
        XYChart.Series seriesBrightness;

        private boolean success;

        ImageHistogram(Image src) {
            image = src;
            success = false;

            //init
            for (int i = 0; i < 256; i++) {
                alpha[i] = red[i] = green[i] = blue[i] = 0;
                brightness[i] = 0;
            }

            PixelReader pixelReader = image.getPixelReader();
            if (pixelReader == null) {
                return;
            }

            //count pixels
            for (int y = 0; y < image.getHeight(); y++) {
                for (int x = 0; x < image.getWidth(); x++) {
                    int argb = pixelReader.getArgb(x, y);
                    int a = (0xff & (argb >> 24));
                    int r = (0xff & (argb >> 16));
                    int g = (0xff & (argb >> 8));
                    int b = (0xff & argb);

                    alpha[a]++;
                    red[r]++;
                    green[g]++;
                    blue[b]++;
                    
                    //Convert RGB to HSB (or HSV)
                    float[] hsb = new float[3];
                    Color.RGBtoHSB(r, g, b, hsb);
                    brightness[(int)(hsb[2]*255)]++;
                }
            }

            seriesAlpha = new XYChart.Series();
            seriesRed = new XYChart.Series();
            seriesGreen = new XYChart.Series();
            seriesBlue = new XYChart.Series();
            seriesBrightness = new XYChart.Series();
            seriesAlpha.setName("alpha");
            seriesRed.setName("red");
            seriesGreen.setName("green");
            seriesBlue.setName("blue");
            seriesBrightness.setName("Brightness");

            //copy alpha[], red[], green[], blue[], brightness
            //to seriesAlpha, seriesRed, seriesGreen, seriesBlue, seriesBrightness
            for (int i = 0; i < 256; i++) {
                seriesAlpha.getData().add(new XYChart.Data(String.valueOf(i), alpha[i]));
                seriesRed.getData().add(new XYChart.Data(String.valueOf(i), red[i]));
                seriesGreen.getData().add(new XYChart.Data(String.valueOf(i), green[i]));
                seriesBlue.getData().add(new XYChart.Data(String.valueOf(i), blue[i]));
                
                seriesBrightness.getData().add(new XYChart.Data(String.valueOf(i), brightness[i]));
            }

            success = true;
        }

        public boolean isSuccess() {
            return success;
        }

        public XYChart.Series getSeriesAlpha() {
            return seriesAlpha;
        }

        public XYChart.Series getSeriesRed() {
            return seriesRed;
        }

        public XYChart.Series getSeriesGreen() {
            return seriesGreen;
        }

        public XYChart.Series getSeriesBlue() {
            return seriesBlue;
        }
        
        public XYChart.Series getSeriesBrightness() {
            return seriesBrightness;
        }
    }

}



- Detect mouse event on JavaFX LineChart Series Node

Saturday, July 25, 2015

Display image's Histogram (RGB) on JavaFX LineChart


Example to display Histogram of on-line images on JavaFX LineChart


remark: In the video, photos from http://www.ultimate-photo-tips.com/histogram-examples.html are used as sample to generate histogram.

Example code:
package javafx_imagehistogram;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.PixelReader;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 * @web http://java-buddy.blogspot.com
 */
public class JavaFX_ImageHistogram extends Application {

    String defaultImage = "http://goo.gl/kYEQl";

    @Override
    public void start(Stage primaryStage) {

        Label labelInfo = new Label();
        labelInfo.setText(
                "java.version: " + System.getProperty("java.version") + "\n"
                + "javafx.runtime.version: " + System.getProperty("javafx.runtime.version")
        );

        TextField textSrc = new TextField();
        textSrc.setText(defaultImage);
        Button btnDo = new Button("Do Histogram");
        ImageView imageView = new ImageView();

        final CategoryAxis xAxis = new CategoryAxis();
        final NumberAxis yAxis = new NumberAxis();
        final LineChart<String, Number> chartHistogram
                = new LineChart<>(xAxis, yAxis);
        chartHistogram.setCreateSymbols(false);

        btnDo.setOnAction((ActionEvent event) -> {

            String imageSrc = textSrc.getText();
            Image image = new Image(imageSrc);
            imageView.setImage(image);
            chartHistogram.getData().clear();

            ImageHistogram imageHistogram = new ImageHistogram(image);
            if(imageHistogram.isSuccess()){
                chartHistogram.getData().addAll(
                    //imageHistogram.getSeriesAlpha(),
                    imageHistogram.getSeriesRed(),
                    imageHistogram.getSeriesGreen(),
                    imageHistogram.getSeriesBlue());
            }
        });

        HBox hBox = new HBox();
        hBox.getChildren().addAll(imageView, chartHistogram);

        VBox vBox = new VBox();
        vBox.getChildren().addAll(labelInfo, textSrc, btnDo, hBox);

        StackPane root = new StackPane();
        root.getChildren().add(vBox);

        Scene scene = new Scene(root, 1000, 500);

        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

    class ImageHistogram {

        private Image image;

        private long alpha[] = new long[256];
        private long red[] = new long[256];
        private long green[] = new long[256];
        private long blue[] = new long[256];

        XYChart.Series seriesAlpha;
        XYChart.Series seriesRed;
        XYChart.Series seriesGreen;
        XYChart.Series seriesBlue;

        private boolean success;

        ImageHistogram(Image src) {
            image = src;
            success = false;

            //init
            for (int i = 0; i < 256; i++) {
                alpha[i] = red[i] = green[i] = blue[i] = 0;
            }

            PixelReader pixelReader = image.getPixelReader();
            if (pixelReader == null) {
                return;
            }

            //count pixels
            for (int y = 0; y < image.getHeight(); y++) {
                for (int x = 0; x < image.getWidth(); x++) {
                    int argb = pixelReader.getArgb(x, y);
                    int a = (0xff & (argb >> 24));
                    int r = (0xff & (argb >> 16));
                    int g = (0xff & (argb >> 8));
                    int b = (0xff & argb);

                    alpha[a]++;
                    red[r]++;
                    green[g]++;
                    blue[b]++;

                }
            }

            seriesAlpha = new XYChart.Series();
            seriesRed = new XYChart.Series();
            seriesGreen = new XYChart.Series();
            seriesBlue = new XYChart.Series();
            seriesAlpha.setName("alpha");
            seriesRed.setName("red");
            seriesGreen.setName("green");
            seriesBlue.setName("blue");

            //copy alpha[], red[], green[], blue[]
            //to seriesAlpha, seriesRed, seriesGreen, seriesBlue
            for (int i = 0; i < 256; i++) {
                seriesAlpha.getData().add(new XYChart.Data(String.valueOf(i), alpha[i]));
                seriesRed.getData().add(new XYChart.Data(String.valueOf(i), red[i]));
                seriesGreen.getData().add(new XYChart.Data(String.valueOf(i), green[i]));
                seriesBlue.getData().add(new XYChart.Data(String.valueOf(i), blue[i]));
            }

            success = true;
        }

        public boolean isSuccess() {
            return success;
        }

        public XYChart.Series getSeriesAlpha() {
            return seriesAlpha;
        }

        public XYChart.Series getSeriesRed() {
            return seriesRed;
        }

        public XYChart.Series getSeriesGreen() {
            return seriesGreen;
        }

        public XYChart.Series getSeriesBlue() {
            return seriesBlue;
        }
    }

}



- Display brightness histogram on JavaFX LineChart

JavaFX animating BarChart and PieChart


JavaFX example to show both animating BarChart and PieChart:


package javafx_histogram;

import java.util.Random;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.PieChart;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 * @web http://java-buddy.blogspot.com
 */
public class JavaFX_Histogram extends Application {

    int group[] = new int[10];

    @Override
    public void start(Stage primaryStage) {

        prepareData();

        Label labelInfo = new Label();
        labelInfo.setText(
                "java.version: " + System.getProperty("java.version") + "\n"
                + "javafx.runtime.version: " + System.getProperty("javafx.runtime.version")
        );

        //barChart1 with setAnimated(false)
        VBox vBoxBarChart1 = new VBox();
        final CategoryAxis xAxis1 = new CategoryAxis();
        final NumberAxis yAxis1 = new NumberAxis();
        final BarChart<String, Number> barChart1
                = new BarChart<>(xAxis1, yAxis1);
        barChart1.setCategoryGap(0);
        barChart1.setBarGap(0);
        barChart1.setMaxSize(500, 450);

        XYChart.Series series1 = new XYChart.Series();
        series1.getData().add(new XYChart.Data("0-10", group[0]));
        series1.getData().add(new XYChart.Data("10-20", group[1]));
        series1.getData().add(new XYChart.Data("20-30", group[2]));
        series1.getData().add(new XYChart.Data("30-40", group[3]));
        series1.getData().add(new XYChart.Data("40-50", group[4]));
        series1.getData().add(new XYChart.Data("50-60", group[5]));
        series1.getData().add(new XYChart.Data("60-70", group[6]));
        series1.getData().add(new XYChart.Data("70-80", group[7]));
        series1.getData().add(new XYChart.Data("80-90", group[8]));
        series1.getData().add(new XYChart.Data("90-100", group[9]));

        barChart1.getData().addAll(series1);
        Label labelAnimated1 = new Label();
        barChart1.setAnimated(false);
        vBoxBarChart1.getChildren().addAll(barChart1, labelAnimated1);
        //- End of barChart1

        //PieChart2 with setAnimated(false)
        VBox vBoxPieChart2 = new VBox();

        ObservableList<PieChart.Data> pieChartData
                = FXCollections.observableArrayList(
                        new PieChart.Data("0-10", group[0]),
                        new PieChart.Data("10-20", group[1]),
                        new PieChart.Data("20-30", group[2]),
                        new PieChart.Data("30-40", group[3]),
                        new PieChart.Data("40-50", group[4]),
                        new PieChart.Data("50-60", group[5]),
                        new PieChart.Data("60-70", group[6]),
                        new PieChart.Data("70-80", group[7]),
                        new PieChart.Data("80-90", group[8]),
                        new PieChart.Data("90-100", group[9])
                );
        
        final PieChart pieChart2
                = new PieChart(pieChartData);
        pieChart2.setMaxSize(500, 450);

        Label labelAnimated2 = new Label();
        pieChart2.setAnimated(false);
        vBoxPieChart2.getChildren().addAll(pieChart2, labelAnimated2);
        //- End of pieChart2

        HBox hBoxCharts = new HBox();
        hBoxCharts.getChildren().addAll(vBoxBarChart1, vBoxPieChart2);

        Label labelCnt = new Label();

        VBox vBox = new VBox();
        vBox.getChildren().addAll(labelInfo, hBoxCharts, labelCnt);

        StackPane root = new StackPane();
        root.getChildren().add(vBox);

        Scene scene = new Scene(root, 1000, 500);

        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();

        Random random = new Random();

        //Apply Animating Data in Charts
        //ref: http://docs.oracle.com/javafx/2/charts/bar-chart.htm
        //"Animating Data in Charts" section
        Timeline timeline = new Timeline();
        timeline.getKeyFrames().add(
                new KeyFrame(Duration.millis(200), (ActionEvent actionEvent) -> {

                    int data = random.nextInt(100);
                    /*
                    final int mean = 50;
                    final int standardDeviation = 10;
                    int data = mean + (int) (random.nextGaussian() * standardDeviation);
                    */

                    if (data <= 10) {
                        group[0]++;
                        series1.getData().set(0, new XYChart.Data("0-10", group[0]));
                        pieChartData.set(0, new PieChart.Data("0-10", group[0]));
                    } else if (data <= 20) {
                        group[1]++;
                        series1.getData().set(1, new XYChart.Data("10-20", group[1]));
                        pieChartData.set(1, new PieChart.Data("10-20", group[1]));
                    } else if (data <= 30) {
                        group[2]++;
                        series1.getData().set(2, new XYChart.Data("20-30", group[2]));
                        pieChartData.set(2, new PieChart.Data("20-30", group[2]));
                    } else if (data <= 40) {
                        group[3]++;
                        series1.getData().set(3, new XYChart.Data("30-40", group[3]));
                        pieChartData.set(3, new PieChart.Data("30-40", group[3]));
                    } else if (data <= 50) {
                        group[4]++;
                        series1.getData().set(4, new XYChart.Data("40-50", group[4]));
                        pieChartData.set(4, new PieChart.Data("40-50", group[4]));
                    } else if (data <= 60) {
                        group[5]++;
                        series1.getData().set(5, new XYChart.Data("50-60", group[5]));
                        pieChartData.set(5, new PieChart.Data("50-60", group[5]));
                    } else if (data <= 70) {
                        group[6]++;
                        series1.getData().set(6, new XYChart.Data("60-70", group[6]));
                        pieChartData.set(6, new PieChart.Data("60-70", group[6]));
                    } else if (data <= 80) {
                        group[7]++;
                        series1.getData().set(7, new XYChart.Data("70-80", group[7]));
                        pieChartData.set(7, new PieChart.Data("70-80", group[7]));
                    } else if (data <= 90) {
                        group[8]++;
                        series1.getData().set(8, new XYChart.Data("80-90", group[8]));
                        pieChartData.set(8, new PieChart.Data("80-90", group[8]));
                    } else if (data <= 100) {
                        group[9]++;
                        series1.getData().set(9, new XYChart.Data("10-100", group[9]));
                        pieChartData.set(9, new PieChart.Data("10-100", group[9]));
                    }

                    labelAnimated1.setText(
                            "barChart1.getAnimated() = " + barChart1.getAnimated());
                    labelAnimated2.setText(
                            "barChart2.getAnimated() = " + pieChart2.getAnimated());

                    String s = "";
                    for (int i = 0; i < 10; i++) {
                        s += " " + group[i];
                    }
                    labelCnt.setText(s);
                }));

        timeline.setCycleCount(1000);
        timeline.setAutoReverse(true);  //!?
        timeline.play();

    }

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

    //generate dummy random data
    private void prepareData() {
        for (int i = 0; i < 10; i++) {
            group[i] = 0;
        }
    }

}

Tuesday, July 21, 2015

Wrong report by animating JavaFX BarChart with setAnimated(true)


Last post show "Apply Animaton in JavaFX Charts with Timeline", and mentioned the BarChart show wrong graph if setAnimated(true). In order to show the behavior, the example is modified to create two BarCharts to show the same data. The BarChart on left side with setAnimated(false) show the correct graph, on the right side setAnimated(true) show the wrong graph.


package javafx_histogram;

import java.util.Random;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 * @web http://java-buddy.blogspot.com
 */
public class JavaFX_Histogram extends Application {

    int group[] = new int[10];

    @Override
    public void start(Stage primaryStage) {

        prepareData();

        Label labelInfo = new Label();
        labelInfo.setText(
                "java.version: " + System.getProperty("java.version") + "\n"
                + "javafx.runtime.version: " + System.getProperty("javafx.runtime.version")
        );

        //barChart1 with setAnimated(false)
        VBox vBoxBarChart1 = new VBox();
        final CategoryAxis xAxis1 = new CategoryAxis();
        final NumberAxis yAxis1 = new NumberAxis();
        final BarChart<String, Number> barChart1
                = new BarChart<>(xAxis1, yAxis1);
        barChart1.setCategoryGap(0);
        barChart1.setBarGap(0);
        barChart1.setMaxSize(500, 450);

        XYChart.Series series1 = new XYChart.Series();
        series1.setName("Animated = false");
        series1.getData().add(new XYChart.Data("0-10", group[0]));
        series1.getData().add(new XYChart.Data("10-20", group[1]));
        series1.getData().add(new XYChart.Data("20-30", group[2]));
        series1.getData().add(new XYChart.Data("30-40", group[3]));
        series1.getData().add(new XYChart.Data("40-50", group[4]));

        series1.getData().add(new XYChart.Data("50-60", group[5]));
        series1.getData().add(new XYChart.Data("60-70", group[6]));
        series1.getData().add(new XYChart.Data("70-80", group[7]));
        series1.getData().add(new XYChart.Data("80-90", group[8]));
        series1.getData().add(new XYChart.Data("90-100", group[9]));

        barChart1.getData().addAll(series1);
        Label labelAnimated1 = new Label();
        barChart1.setAnimated(false);
        vBoxBarChart1.getChildren().addAll(barChart1, labelAnimated1);
        //- End of barChart1
        
        //barChart2 with setAnimated(true)
        VBox vBoxBarChart2 = new VBox();
        final CategoryAxis xAxis2 = new CategoryAxis();
        final NumberAxis yAxis2 = new NumberAxis();
        final BarChart<String, Number> barChart2
                = new BarChart<>(xAxis2, yAxis2);
        barChart2.setCategoryGap(0);
        barChart2.setBarGap(0);
        barChart2.setMaxSize(500, 450);

        XYChart.Series series2 = new XYChart.Series();
        series2.setName("Animated = true");
        series2.getData().add(new XYChart.Data("0-10", group[0]));
        series2.getData().add(new XYChart.Data("10-20", group[1]));
        series2.getData().add(new XYChart.Data("20-30", group[2]));
        series2.getData().add(new XYChart.Data("30-40", group[3]));
        series2.getData().add(new XYChart.Data("40-50", group[4]));

        series2.getData().add(new XYChart.Data("50-60", group[5]));
        series2.getData().add(new XYChart.Data("60-70", group[6]));
        series2.getData().add(new XYChart.Data("70-80", group[7]));
        series2.getData().add(new XYChart.Data("80-90", group[8]));
        series2.getData().add(new XYChart.Data("90-100", group[9]));

        barChart2.getData().addAll(series2);
        Label labelAnimated2 = new Label();
        barChart2.setAnimated(true);
        vBoxBarChart2.getChildren().addAll(barChart2, labelAnimated2);
        //- End of barChart2
        
        HBox hBoxCharts = new HBox();
        hBoxCharts.getChildren().addAll(vBoxBarChart1, vBoxBarChart2);
        
        Label labelCnt = new Label();

        VBox vBox = new VBox();
        vBox.getChildren().addAll(labelInfo, hBoxCharts, labelCnt);

        StackPane root = new StackPane();
        root.getChildren().add(vBox);

        Scene scene = new Scene(root, 1000, 500);

        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();

        Random random = new Random();
        
        //Apply Animating Data in Charts
        //ref: http://docs.oracle.com/javafx/2/charts/bar-chart.htm
        //"Animating Data in Charts" section
        
        Timeline timeline = new Timeline();
        timeline.getKeyFrames().add(
                new KeyFrame(Duration.millis(200), (ActionEvent actionEvent) -> {

            //int data = random.nextInt(100);
            final int mean = 50;
            final int standardDeviation = 10;
            int data = mean + (int)(random.nextGaussian() * standardDeviation);
            
            if (data <= 10) {
                group[0]++;
                series1.getData().set(0, new XYChart.Data("0-10", group[0]));
                series2.getData().set(0, new XYChart.Data("0-10", group[0]));
            } else if (data <= 20) {
                group[1]++;
                series1.getData().set(1, new XYChart.Data("10-20", group[1]));
                series2.getData().set(1, new XYChart.Data("10-20", group[1]));
            } else if (data <= 30) {
                group[2]++;
                series1.getData().set(2, new XYChart.Data("20-30", group[2]));
                series2.getData().set(2, new XYChart.Data("20-30", group[2]));
            } else if (data <= 40) {
                group[3]++;
                series1.getData().set(3, new XYChart.Data("30-40", group[3]));
                series2.getData().set(3, new XYChart.Data("30-40", group[3]));
            } else if (data <= 50) {
                group[4]++;
                series1.getData().set(4, new XYChart.Data("40-50", group[4]));
                series2.getData().set(4, new XYChart.Data("40-50", group[4]));
            } else if (data <= 60) {
                group[5]++;
                series1.getData().set(5, new XYChart.Data("50-60", group[5]));
                series2.getData().set(5, new XYChart.Data("50-60", group[5]));
            } else if (data <= 70) {
                group[6]++;
                series1.getData().set(6, new XYChart.Data("60-70", group[6]));
                series2.getData().set(6, new XYChart.Data("60-70", group[6]));
            } else if (data <= 80) {
                group[7]++;
                series1.getData().set(7, new XYChart.Data("70-80", group[7]));
                series2.getData().set(7, new XYChart.Data("70-80", group[7]));
            } else if (data <= 90) {
                group[8]++;
                series1.getData().set(8, new XYChart.Data("80-90", group[8]));
                series2.getData().set(8, new XYChart.Data("80-90", group[8]));
            } else if (data <= 100) {
                group[9]++;
                series1.getData().set(9, new XYChart.Data("10-100", group[9]));
                series2.getData().set(9, new XYChart.Data("10-100", group[9]));
            }

            labelAnimated1.setText(
                    "barChart1.getAnimated() = " + barChart1.getAnimated());
            labelAnimated2.setText(
                    "barChart2.getAnimated() = " + barChart2.getAnimated());
            
            String s = "";
            for(int i=0; i<10; i++){
                s+= " " + group[i];
            }
            labelCnt.setText(s);
        }));

        timeline.setCycleCount(1000);
        timeline.setAutoReverse(true);  //!?
        timeline.play();

    }

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

    //generate dummy random data
    private void prepareData() {
        for (int i = 0; i < 10; i++) {
            group[i] = 0;
        }
    }

}

Apply Animaton in JavaFX Charts with Timeline

This example show how to animating data in JavaFX BarChart with Timeline, to generate Gaussian Random number distribution.


Example code:
package javafx_histogram;

import java.util.Random;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 * @web http://java-buddy.blogspot.com
 */
public class JavaFX_Histogram extends Application {

    int group[] = new int[10];

    @Override
    public void start(Stage primaryStage) {

        prepareData();

        Label labelInfo = new Label();
        labelInfo.setText(
                "java.version: " + System.getProperty("java.version") + "\n"
                + "javafx.runtime.version: " + System.getProperty("javafx.runtime.version")
        );

        final CategoryAxis xAxis = new CategoryAxis();
        final NumberAxis yAxis = new NumberAxis();
        final BarChart<String, Number> barChart
                = new BarChart<>(xAxis, yAxis);
        barChart.setCategoryGap(0);
        barChart.setBarGap(0);

        xAxis.setLabel("Range");
        yAxis.setLabel("Population");

        XYChart.Series series1 = new XYChart.Series();
        series1.setName("Histogram");
        series1.getData().add(new XYChart.Data("0-10", group[0]));
        series1.getData().add(new XYChart.Data("10-20", group[1]));
        series1.getData().add(new XYChart.Data("20-30", group[2]));
        series1.getData().add(new XYChart.Data("30-40", group[3]));
        series1.getData().add(new XYChart.Data("40-50", group[4]));

        series1.getData().add(new XYChart.Data("50-60", group[5]));
        series1.getData().add(new XYChart.Data("60-70", group[6]));
        series1.getData().add(new XYChart.Data("70-80", group[7]));
        series1.getData().add(new XYChart.Data("80-90", group[8]));
        series1.getData().add(new XYChart.Data("90-100", group[9]));

        barChart.getData().addAll(series1);
        
        Label labelCnt = new Label();
        Label labelAnimated = new Label();

        VBox vBox = new VBox();
        vBox.getChildren().addAll(labelInfo, barChart, labelCnt, labelAnimated);

        StackPane root = new StackPane();
        root.getChildren().add(vBox);

        Scene scene = new Scene(root, 800, 500);

        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();

        Random random = new Random();
        
        //Apply Animating Data in Charts
        //ref: http://docs.oracle.com/javafx/2/charts/bar-chart.htm
        //"Animating Data in Charts" section
        
        Timeline timeline = new Timeline();
        timeline.getKeyFrames().add(
                new KeyFrame(Duration.millis(200), (ActionEvent actionEvent) -> {

            //int data = random.nextInt(100);
            final int mean = 50;
            final int standardDeviation = 10;
            int data = mean + (int)(random.nextGaussian() * standardDeviation);
            
            if (data <= 10) {
                group[0]++;
                series1.getData().set(0, new XYChart.Data("0-10", group[0]));
            } else if (data <= 20) {
                group[1]++;
                series1.getData().set(1, new XYChart.Data("10-20", group[1]));
            } else if (data <= 30) {
                group[2]++;
                series1.getData().set(2, new XYChart.Data("20-30", group[2]));
            } else if (data <= 40) {
                group[3]++;
                series1.getData().set(3, new XYChart.Data("30-40", group[3]));
            } else if (data <= 50) {
                group[4]++;
                series1.getData().set(4, new XYChart.Data("40-50", group[4]));
            } else if (data <= 60) {
                group[5]++;
                series1.getData().set(5, new XYChart.Data("50-60", group[5]));
            } else if (data <= 70) {
                group[6]++;
                series1.getData().set(6, new XYChart.Data("60-70", group[6]));
            } else if (data <= 80) {
                group[7]++;
                series1.getData().set(7, new XYChart.Data("70-80", group[7]));
            } else if (data <= 90) {
                group[8]++;
                series1.getData().set(8, new XYChart.Data("80-90", group[8]));
            } else if (data <= 100) {
                group[9]++;
                series1.getData().set(9, new XYChart.Data("10-100", group[9]));
            }
            
            String s = "";
            for(int i=0; i<10; i++){
                s+= " " + group[i];
            }
            
            labelCnt.setText(s);
            labelAnimated.setText("barChart.getAnimated() = " + barChart.getAnimated());
        }));

        timeline.setCycleCount(1000);
        timeline.setAutoReverse(true);  //!?
        timeline.play();

        barChart.setAnimated(false);
        
    }

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

    //generate dummy random data
    private void prepareData() {
        for (int i = 0; i < 10; i++) {
            group[i] = 0;
        }
    }

}


The animation part refer to the "Animating Data in Charts" section in JavaFX Documentation.
- Actually, I don't know what the purpose of timeline.setAutoReverse(true) in this case.
- If the property animated set true, it have unexpected result as shown here:
* The bar show in-correct value.
* The bar show not rounded integer value.


In order to show the behavior, the example is modified to create two BarCharts to show the same data, with setAnimated(false) and setAnimated(true). Refer to next post "Wrong report by animating JavaFX BarChart with setAnimated(true)".

Monday, July 20, 2015

Generate Gaussian ("normally") distributed random data using random.nextGaussian()

The Random.nextGaussian() method returns the next pseudorandom, Gaussian ("normally") distributed double value with mean 0.0 and standard deviation 1.0 from this random number generator's sequence.

            int mean = 50;
            int standardDeviation = 10;
            data = mean + (int)(random.nextGaussian() * standardDeviation);



package javafx_histogram;

import java.util.Random;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 * @web http://java-buddy.blogspot.com
 */
public class JavaFX_Histogram extends Application {
    
    int DATA_SIZE = 1000;
    int data[] = new int[DATA_SIZE];
    int group[] = new int[10];
    
    @Override
    public void start(Stage primaryStage) {
        
        prepareData();
        groupData();
        
        Label labelInfo = new Label();
        labelInfo.setText(
            "java.version: " + System.getProperty("java.version") + "\n" +
            "javafx.runtime.version: " + System.getProperty("javafx.runtime.version")
        );
        
        final CategoryAxis xAxis = new CategoryAxis();
        final NumberAxis yAxis = new NumberAxis();
        final BarChart<String,Number> barChart = 
            new BarChart<>(xAxis,yAxis);
        barChart.setCategoryGap(0);
        barChart.setBarGap(0);
        
        xAxis.setLabel("Range");       
        yAxis.setLabel("Population");
        
        XYChart.Series series1 = new XYChart.Series();
        series1.setName("Histogram");       
        series1.getData().add(new XYChart.Data("0-10", group[0]));
        series1.getData().add(new XYChart.Data("10-20", group[1]));
        series1.getData().add(new XYChart.Data("20-30", group[2]));
        series1.getData().add(new XYChart.Data("30-40", group[3]));
        series1.getData().add(new XYChart.Data("40-50", group[4])); 
        
        series1.getData().add(new XYChart.Data("50-60", group[5]));
        series1.getData().add(new XYChart.Data("60-70", group[6]));
        series1.getData().add(new XYChart.Data("70-80", group[7]));
        series1.getData().add(new XYChart.Data("80-90", group[8]));
        series1.getData().add(new XYChart.Data("90-100", group[9]));
        
        barChart.getData().addAll(series1);
        
        VBox vBox = new VBox();
        vBox.getChildren().addAll(labelInfo, barChart);
        
        StackPane root = new StackPane();
        root.getChildren().add(vBox);
        
        Scene scene = new Scene(root, 800, 450);
        
        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
    
    //generate dummy random data
    private void prepareData(){

        Random random = new Random();
        for(int i=0; i<DATA_SIZE; i++){
            //data[i] = random.nextInt(100);
            
            int mean = 50;
            int standardDeviation = 10;
            data[i] = mean + (int)(random.nextGaussian() * standardDeviation);
            
        }
    }
    
    //count data population in groups
    private void groupData(){
        for(int i=0; i<10; i++){
            group[i]=0;
        }
        for(int i=0; i<DATA_SIZE; i++){
            if(data[i]<=10){
                group[0]++;
            }else if(data[i]<=20){
                group[1]++;
            }else if(data[i]<=30){
                group[2]++;
            }else if(data[i]<=40){
                group[3]++;
            }else if(data[i]<=50){
                group[4]++;
            }else if(data[i]<=60){
                group[5]++;
            }else if(data[i]<=70){
                group[6]++;
            }else if(data[i]<=80){
                group[7]++;
            }else if(data[i]<=90){
                group[8]++;
            }else if(data[i]<=100){
                group[9]++;
            }
        }
    }
    
}



JavaFX BarChart example: plot data population in histogram

Example to plot data population in histogram using JavaFX BarChart.


package javafx_histogram;

import java.util.Random;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 * @web http://java-buddy.blogspot.com
 */
public class JavaFX_Histogram extends Application {
    
    int DATA_SIZE = 1000;
    int data[] = new int[DATA_SIZE];
    int group[] = new int[10];
    
    @Override
    public void start(Stage primaryStage) {
        
        prepareData();
        groupData();
        
        Label labelInfo = new Label();
        labelInfo.setText(
            "java.version: " + System.getProperty("java.version") + "\n" +
            "javafx.runtime.version: " + System.getProperty("javafx.runtime.version")
        );
        
        final CategoryAxis xAxis = new CategoryAxis();
        final NumberAxis yAxis = new NumberAxis();
        final BarChart<String,Number> barChart = 
            new BarChart<>(xAxis,yAxis);
        barChart.setCategoryGap(0);
        barChart.setBarGap(0);
        
        xAxis.setLabel("Range");       
        yAxis.setLabel("Population");
        
        XYChart.Series series1 = new XYChart.Series();
        series1.setName("Histogram");       
        series1.getData().add(new XYChart.Data("0-10", group[0]));
        series1.getData().add(new XYChart.Data("10-20", group[1]));
        series1.getData().add(new XYChart.Data("20-30", group[2]));
        series1.getData().add(new XYChart.Data("30-40", group[3]));
        series1.getData().add(new XYChart.Data("40-50", group[4])); 
        
        series1.getData().add(new XYChart.Data("50-60", group[5]));
        series1.getData().add(new XYChart.Data("60-70", group[6]));
        series1.getData().add(new XYChart.Data("70-80", group[7]));
        series1.getData().add(new XYChart.Data("80-90", group[8]));
        series1.getData().add(new XYChart.Data("90-100", group[9]));
        
        barChart.getData().addAll(series1);
        
        VBox vBox = new VBox();
        vBox.getChildren().addAll(labelInfo, barChart);
        
        StackPane root = new StackPane();
        root.getChildren().add(vBox);
        
        Scene scene = new Scene(root, 800, 450);
        
        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
    
    //generate dummy random data
    private void prepareData(){

        Random random = new Random();
        for(int i=0; i<DATA_SIZE; i++){
            data[i] = random.nextInt(100);
        }
    }
    
    //count data population in groups
    private void groupData(){
        for(int i=0; i<10; i++){
            group[i]=0;
        }
        for(int i=0; i<DATA_SIZE; i++){
            if(data[i]<=10){
                group[0]++;
            }else if(data[i]<=20){
                group[1]++;
            }else if(data[i]<=30){
                group[2]++;
            }else if(data[i]<=40){
                group[3]++;
            }else if(data[i]<=50){
                group[4]++;
            }else if(data[i]<=60){
                group[5]++;
            }else if(data[i]<=70){
                group[6]++;
            }else if(data[i]<=80){
                group[7]++;
            }else if(data[i]<=90){
                group[8]++;
            }else if(data[i]<=100){
                group[9]++;
            }
        }
    }
    
}

Saturday, July 18, 2015

Validate and parse JavaFX TextField to various number

Example to Validate and parse JavaFX TextField to various number: Integer, Short, Long, Byte, Float, Double.


package javafx_validatenumber;

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

/**
 * @web http://java-buddy.blogspot.com
 * 
 * Validate and parse JavaFX TextField to various number,
 * Integer, Short, Long, Byte, Float, Double
 */
public class JavaFX_ValidateNumber extends Application {
    
    @Override
    public void start(Stage primaryStage) {
        
        Label labelInfo = new Label();
        labelInfo.setText(
            "java.version: " + System.getProperty("java.version") + "\n" +
            "javafx.runtime.version: " + System.getProperty("javafx.runtime.version")
        );
        
        TextField textIn = new TextField();
        
        Label labelAnswer = new Label();
        
        Button btnIsInteger = new Button("is Integer?");
        btnIsInteger.setOnAction((ActionEvent event) -> {
            try{
                Integer i = Integer.parseInt(textIn.getText());
                labelAnswer.setText("Is Integer: " + i);
            }catch (NumberFormatException e){
                labelAnswer.setText("Not Integer!");
            }
        });
        
        Button btnIsShort = new Button("is Short?");
        btnIsShort.setOnAction((ActionEvent event) -> {
            try{
                Short i = Short.parseShort(textIn.getText());
                labelAnswer.setText("Is Short: " + i);
            }catch (NumberFormatException e){
                labelAnswer.setText("Not Short!");
            }
        });
        
        Button btnIsLong = new Button("is Long?");
        btnIsLong.setOnAction((ActionEvent event) -> {
            try{
                Long i = Long.parseLong(textIn.getText());
                labelAnswer.setText("Is Long: " + i);
            }catch (NumberFormatException e){
                labelAnswer.setText("Not Long!");
            }
        });
        
        Button btnIsByte = new Button("is Byte?");
        btnIsByte.setOnAction((ActionEvent event) -> {
            try{
                Byte i = Byte.parseByte(textIn.getText());
                labelAnswer.setText("Is Byte: " + i);
            }catch (NumberFormatException e){
                labelAnswer.setText("Not Byte!");
            }
        });
        
        Button btnIsFloat = new Button("is Float?");
        btnIsFloat.setOnAction((ActionEvent event) -> {
            try{
                Float i = Float.parseFloat(textIn.getText());
                labelAnswer.setText("Is Float: " + i);
            }catch (NumberFormatException e){
                labelAnswer.setText("Not Float!");
            }
        });
        
        Button btnIsDouble = new Button("is Double?");
        btnIsDouble.setOnAction((ActionEvent event) -> {
            try{
                Double i = Double.parseDouble(textIn.getText());
                labelAnswer.setText("Is Double: " + i);
            }catch (NumberFormatException e){
                labelAnswer.setText("Not Double!");
            }
        });
        
        VBox vBox = new VBox();
        vBox.getChildren().addAll(labelInfo, textIn, btnIsInteger, 
            btnIsShort, btnIsLong, btnIsByte, btnIsFloat, btnIsDouble, 
            labelAnswer);
        
        StackPane root = new StackPane();
        root.getChildren().add(vBox);
        
        Scene scene = new Scene(root, 300, 250);
        
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
}


Saturday, July 4, 2015

javap - Java Class Disassembler

javap is a command line Java Class Disassembler. It disassembles one or more class files. Its output depends on the options used. If no options are used, javap prints out the package, protected, and public fields and methods of the classes passed to it. javap prints its output to stdout.