JavaFX: Generating grid lines using group

This entry is part 51 of 55 in the series JavaFX

Using  group, we can create 2 separate set of lines making them to look like grids.

Here the grids are formed by 2 groups of horizontal and vertical lines.

package drawing;

import javafx.application.*;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.shape.*;
import javafx.scene.paint.*;

public class Drawing extends Application {

    @Override
    public void start(Stage stage) {

        Group group = new Group();

        // The background grid
        for (int i = 0; i < 400; i += 20) {
            Line hline = new Line(i, 0, i, 360);
            hline.setStroke(Color.LIGHTGRAY);
            Line vline = new Line(0, i, 400, i);
            vline.setStroke(Color.DARKBLUE);
            group.getChildren().addAll(hline, vline);
        }

        // Create the scene and the stage
        Scene scene = new Scene(group, 400, 360);
        stage.setScene(scene);
        stage.setTitle("Grid Lines");
        stage.show();
    }

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

}

gridlines

JavaFX: Adding circle to line group

This entry is part 52 of 55 in the series JavaFX

For purpose of demonstration, we see how easy it is to add another drawing to the line group.

The main line that we are using is

group.getChildren().add(circle);

This put the circle to be in the same group as the line group.

package drawing;

import javafx.application.*;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.shape.*;
import javafx.scene.paint.*;

public class Drawing extends Application {

    @Override
    public void start(Stage stage) {

        Group group = new Group();

        // The background grid
        for (int i = 0; i < 400; i += 20) {
            Line hline = new Line(i, 0, i, 360);
            hline.setStroke(Color.LIGHTGRAY);
            Line vline = new Line(0, i, 400, i);
            vline.setStroke(Color.DARKBLUE);
            group.getChildren().addAll(hline, vline);
        }
        
        Circle circle = new Circle(200, 180, 75);
        circle.setStroke(Color.BLACK);
        circle.setFill(null);
        circle.setStrokeWidth(3);
        group.getChildren().add(circle);

        // Create the scene and the stage
        Scene scene = new Scene(group, 400, 360);
        stage.setScene(scene);
        stage.setTitle("Grid Lines and Circle");
        stage.show();
    }

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

}

group1

JavaFX: Media player

This entry is part 53 of 55 in the series JavaFX

As of now, I believe that the native player provided by Java supports only a few types of encoding such as MP3 and flv files.

Here I work out an example where it will simply play an MP3 file that has a URL link.

package mediaplay;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;


public class MediaPlay extends Application {

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

    @Override
    public void start(Stage stage) throws Exception {
        
        String path = "http://www.noiseaddicts.com/samples/13.mp3";
        
        Media media = new Media(path);       
        StackPane root = new StackPane();

        MediaPlayer mp = new MediaPlayer(media);       
        mp.setAutoPlay(true);
        MediaView mv = new MediaView(mp);
        
        Label lb = new Label("Playing");
        lb.setFont(Font.font("Times New Roman", 52));
        lb.setTextFill(Color.BLUE);
        
        BorderPane pane = new BorderPane();
        pane.setCenter(lb);

        root.getChildren().addAll(mv, pane);
        Scene scene = new Scene(root, 400, 400);
        stage.setScene(scene);
        stage.show();

        mp.play();
    }
}

media

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

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