JavaFX: Button Sample

There is a button sample from Oracle website and I tested running it just to see its output.

If you have like to use images for your buttons, you can refer to this example.

Button Sample
package buttonsample;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.effect.DropShadow;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class ButtonSample extends Application {

    private static final Color color = Color.web("#464646");
    Button button3 = new Button("Decline");
    DropShadow shadow = new DropShadow();
    Label label = new Label();

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

    @Override
    public void start(Stage stage) {
        Scene scene = new Scene(new Group());
        stage.setTitle("Button Sample");
        stage.setWidth(400);
        stage.setHeight(300);

        label.setFont(Font.font("Times New Roman", 22));
        label.setTextFill(color);

        Image imageDecline = new Image(getClass().getResourceAsStream("orange.png"));
        Image imageAccept = new Image(getClass().getResourceAsStream("green.png"));

        VBox vbox = new VBox();
        vbox.setLayoutX(20);
        vbox.setLayoutY(20);
        HBox hbox1 = new HBox();
        HBox hbox2 = new HBox();

        Button button1 = new Button("Accept", new ImageView(imageAccept));
        button1.setStyle("-fx-font: 22 arial; -fx-base: #b6e7c9;");
        
        button1.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                label.setText("Accepted");
            }
        });

        Button button2 = new Button("Accept");
        button2.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                label.setText("Accepted");
            }
        });

        button3.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                label.setText("Declined");
            }
        });

        button3.addEventHandler(MouseEvent.MOUSE_ENTERED,
                new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(MouseEvent e) {
                        button3.setEffect(shadow);
                    }
                });

        button3.addEventHandler(MouseEvent.MOUSE_EXITED,
                new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(MouseEvent e) {
                        button3.setEffect(null);
                    }
                });

        hbox1.getChildren().add(button2);
        hbox1.getChildren().add(button3);
        hbox1.getChildren().add(label);
        hbox1.setSpacing(10);
        hbox1.setAlignment(Pos.BOTTOM_CENTER);

        Button button4 = new Button();
        button4.setGraphic(new ImageView(imageAccept));
        button4.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                label.setText("Accepted");
            }
        });

        Button button5 = new Button();
        button5.setGraphic(new ImageView(imageDecline));
        button5.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                label.setText("Declined");
            }
        });

        hbox2.getChildren().add(button4);
        hbox2.getChildren().add(button5);
        hbox2.setSpacing(25);

        vbox.getChildren().add(button1);
        vbox.getChildren().add(hbox1);
        vbox.getChildren().add(hbox2);
        vbox.setSpacing(10);
        ((Group) scene.getRoot()).getChildren().add(vbox);

        stage.setScene(scene);
        stage.show();
    }
}

Below screenshot is the output from running the program.

buttonsample

The green.png and orange.png graphic files are as below.

green orange

Drawing line, circle and rectangle

This is a relatively simple JavaFX program that draws out a line, a circle and a rectangle.

package javafxapplication7;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class JavaFXApplication7 extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        initUI(primaryStage);
    }

    public void initUI(Stage stage) {
        Pane root = new Pane();

        Rectangle rect = new Rectangle(50, 100, 100, 100);
        rect.setFill(Color.CADETBLUE);

        Line line = new Line(90, 40, 300, 40);
        line.setStroke(Color.BLACK);

        Circle circle = new Circle(230, 230, 30);
        circle.setFill(Color.CHOCOLATE);

        root.getChildren().addAll(rect, line, circle);

        Scene scene = new Scene(root, 400, 300, Color.WHITESMOKE);

        stage.setTitle("Absolute layout");
        stage.setScene(scene);
        stage.show();
    }

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

drawing

JavaFX: Blending of 2 images

Blending of 2 images.

If you look at line 36, we are using the difference mode:

top.setBlendMode(BlendMode.DIFFERENCE);

The DIFFERENCE mode is defined as: The darker of the color components from the two inputs are subtracted from the lighter ones to produce the resulting color.

So in the screenshot below, the image in the middle is the blended image.

package pepsichallenge;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.BlendMode;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

/**
 * Blend a coke can and a pepsi can to find the difference.
 */
public class PepsiChallenge extends Application {

    @Override
    public void start(Stage stage) {
        Image coke = new Image(
                "http://icons.iconarchive.com/icons/michael/coke-pepsi/256/Coca-Cola-Can-icon.png"
        );

        Image pepsi = new Image(
                "http://icons.iconarchive.com/icons/michael/coke-pepsi/256/Pepsi-Can-icon.png"
        );

        ImageView bottom = new ImageView(coke);
        ImageView top = new ImageView(pepsi);
        top.setBlendMode(BlendMode.DIFFERENCE);

        Group blend = new Group(
                bottom,
                top
        );

        HBox layout = new HBox(10);
        layout.getChildren().addAll(
                new ImageView(coke),
                blend,
                new ImageView(pepsi)
        );
        layout.setPadding(new Insets(10));
        stage.setScene(new Scene(layout));
        stage.show();
    }

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

pepsi

JavaFX: Drawing a 3D sphere

This entry is part 55 of 55 in the series JavaFX

Drawing a 3D sphere.

package draw3dsphere;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.SubScene;
import javafx.scene.Group;
import javafx.scene.layout.BorderPane;
import javafx.scene.PerspectiveCamera;
import javafx.scene.PointLight;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Sphere;
import javafx.scene.transform.Translate;

public class Draw3DSphere extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        // 3D parts
        Group root = new Group();

        Sphere sphere = new Sphere(20);
        PhongMaterial material = new PhongMaterial();
        material.setDiffuseColor(Color.RED);
        material.setSpecularColor(Color.RED);
        sphere.setMaterial(material);
        root.getChildren().add(sphere);

        PointLight light = new PointLight(Color.WHITE);
        // JavaFX axis: left-top-near is minus, right-bottom-far is plus
        light.getTransforms().addAll(new Translate(-100, -100, -100));
        root.getChildren().add(light);

        PerspectiveCamera camera = new PerspectiveCamera(true);
        camera.setFieldOfView(30);
        camera.getTransforms().addAll(new Translate(0, 0, -100));

        SubScene subscene = new SubScene(root, 800, 600);
        subscene.setCamera(camera);

        // 2D controls
        BorderPane pane = new BorderPane();
        pane.setCenter(subscene);

        Scene scene = new Scene(pane, 800, 800);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) throws Exception {
        Application.launch(args);
    }
}

sphere

JavaFX: Line and bar chart

This entry is part 54 of 55 in the series JavaFX

When it comes to setting up the bar or line chart, there are a few classes that we can use in JavaFX.  JavaFX provides the line and bar chart objects and in addition, it provides the numberAxis for the x-y axis and also XYChart for plotting the positions of line and bar.

package javafxapplication4;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class JavaFXApplication4 extends Application {


    @Override
    public void start(Stage stage) {

        final NumberAxis xAxis = new NumberAxis();
        final NumberAxis yAxis = new NumberAxis();
        final CategoryAxis cXAxis = new CategoryAxis();

        
        //Declarations of the 2 types of charts
        final BarChart<String, Number> bc = new BarChart<>(cXAxis, yAxis);
        final LineChart<Number, Number> lc = new LineChart<>(xAxis, yAxis);

        //setting up the bar chart
        XYChart.Series series1 = new XYChart.Series();
        series1.getData().add(new XYChart.Data("A", 200));
        series1.getData().add(new XYChart.Data("B", 50));

        //setting up the line chart
        XYChart.Series series2 = new XYChart.Series();
        series2.getData().add(new XYChart.Data(10, 210));
        series2.getData().add(new XYChart.Data(3, 35));

        bc.getData().addAll(series1);
        lc.getData().addAll(series2);

        Pane pane = new Pane();
        pane.getChildren().add(lc);
        pane.getChildren().add(bc);

        bc.setOpacity(0.5);
        
        Scene scene = new Scene(pane, 500, 400);
        stage.setScene(scene);
        stage.show();
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}

chart